<?php
namespace App\Action\PressSite\Content;
use App\Action\PressSite\DomainAwareAction;
use App\Contract\PressSite\Language\Switcher\GenericActionInterface;
use App\Service\PressSite\Content\Callback\NewMoviesCallback;
use App\Service\PressSite\Content\Callback\UpcomingMoviesCallback;
use App\Service\PressSite\Content\MovieContentBuilder;
use App\Service\PressSite\DomainAwareManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Environment;
/**
* Class HomepageAction.
*/
class DefaultAction extends DomainAwareAction implements GenericActionInterface
{
/**
* The custom content builder responsible for returning the list with new movies.
*
* @var \App\Service\PressSite\Content\MovieContentBuilder
*/
private $contentBuilder;
public function __construct(
Environment $twig,
DomainAwareManager $domainAwareManager,
MovieContentBuilder $contentBuilder
) {
parent::__construct($twig, $domainAwareManager);
$this->contentBuilder = $contentBuilder;
}
/**
* Responsible for rendering the homepage content.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object
*
* @return \Symfony\Component\HttpFoundation\Response
* The response object to return
*/
public function __invoke(Request $request): Response
{
// Theatrical sites do not have the new movies content page.
if ($this->getDomainManager()->getCurrentByHostnameAndLocale()->isBroadcastType()) {
throw new NotFoundHttpException('Page not found');
}
$limit = 5;
return $this->render('press_site/actions/content/default.html.twig', [
'new_movies_collection' => $this->getNewMovies($limit),
'upcoming_movies_collection' => $this->getUpcomingMovies($limit),
]);
}
/**
* Returns the collection of upcoming movies.
*
* @param int $limit
* The amount of results to return
*
* @return array
* The movies collection
*/
protected function getUpcomingMovies(int $limit): array
{
$this->contentBuilder->reset();
$this->contentBuilder->addCallback(new UpcomingMoviesCallback());
return $this->contentBuilder->getContent(
$limit,
MovieContentBuilder::SORT_ORDER_ASC,
MovieContentBuilder::SORT_BY_DATE_TYPE
);
}
/**
* Returns the collection of new movies.
*
* @param int $limit
* The amount of results to return
*
* @return array
* The movies collection
*/
protected function getNewMovies(int $limit): array
{
$this->contentBuilder->reset();
$this->contentBuilder->addCallback(new NewMoviesCallback());
return $this->contentBuilder->getContent($limit, MovieContentBuilder::SORT_ORDER_DESC);
}
}