<?php
namespace App\Action\PressSite\Content;
use App\Action\PressSite\DomainAwareAction;
use App\Contract\PressSite\Language\Switcher\EntityActionInterface;
use App\Service\PressSite\DomainAwareManager;
use App\Service\PressSite\GenericPageHelper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Environment;
/**
* Class GenericPageAction.
*/
class GenericPageAction extends DomainAwareAction implements EntityActionInterface
{
/**
* The custom page helper.
*
* @var \App\Service\PressSite\GenericPageHelper
*/
private $genericPageHelper;
public function __construct(
Environment $twig,
DomainAwareManager $domainAwareManager,
GenericPageHelper $genericPageHelper
) {
parent::__construct($twig, $domainAwareManager);
$this->genericPageHelper = $genericPageHelper;
}
/**
* Responsible for rendering the content of dynamic page.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object
* @param int $id
* The ID of the page entity
* @param string $slug
* The slug of the localized page
*
* @return \Symfony\Component\HttpFoundation\Response
* The response object to return
*/
public function __invoke(Request $request, int $id, string $slug): Response
{
$currentSite = $this->getDomainManager()->getCurrentByHostnameAndLocale();
$language = $currentSite->getLanguageCode();
$this->genericPageHelper->setId($id);
$this->genericPageHelper->setSlug($slug);
$this->genericPageHelper->setLanguageCode($language);
if (!$this->genericPageHelper->isValid()) {
throw new NotFoundHttpException('Page not found');
}
return $this->render('press_site/actions/content/generic_page.html.twig', [
'entity' => $this->genericPageHelper->getPageEntity(),
'localizedEntity' => $this->genericPageHelper->getPageLocalizationEntity(),
]);
}
}