<?php
namespace App\Controller\Website;
use App\Service\Api\NewsApiService;
use DateTime;
use Sulu\Bundle\WebsiteBundle\Controller\DefaultController;
use Sulu\Component\Content\Compat\StructureInterface;
class NewsController extends DefaultController
{
private NewsApiService $newsApiService;
public function __construct(
NewsApiService $newsApiService
) {
$this->newsApiService = $newsApiService;
}
protected function getAttributes($attributes, StructureInterface $structure = null, $preview = false)
{
$attributes = parent::getAttributes($attributes, $structure, $preview);
$attributes['newsFromApi'] = [];
$newsFromApi = $this->newsApiService->getNews();
foreach ($newsFromApi as &$news) {
$news['source'] = 'api';
$news['created'] = new DateTime($news['date']);
$news['url'] = $news['previewUrl'];
$news['thumbnail'] = ["thumbnails" => ["568x400" => $news['img'], "1136x800" => $news['img']]];
unset($news['date'], $news['previewUrl'], $news['img']);
}
$attributes['newsFromApi'] = $newsFromApi;
return $attributes;
}
}