src/EventSubscriber/PressSite/ExceptionSubscriber.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\PressSite;
  3. use App\Service\PressSite\AppContext;
  4. use App\Service\PressSite\DomainAwareManager;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Twig\Environment;
  10. /**
  11.  * Class ExceptionSubscriber.
  12.  */
  13. class ExceptionSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * The twig templating environment.
  17.      *
  18.      * @var \Twig\Environment
  19.      */
  20.     private $twig;
  21.     /**
  22.      * The custom context helper.
  23.      *
  24.      * @var \App\Service\PressSite\AppContext
  25.      */
  26.     private $appContext;
  27.     /**
  28.      * The custom service providing helper domain related methods.
  29.      *
  30.      * @var \App\Service\PressSite\DomainAwareManager
  31.      */
  32.     private $domainAwareManager;
  33.     /**
  34.      * The current environment value.
  35.      *
  36.      * @var string
  37.      */
  38.     private $currentEnvironment;
  39.     /**
  40.      * ExceptionSubscriber constructor.
  41.      *
  42.      * @param \Twig\Environment $twig
  43.      *   The twig templating environment
  44.      * @param \App\Service\PressSite\AppContext $appContext
  45.      *   The custom context helper
  46.      * @param \App\Service\PressSite\DomainAwareManager $domainAwareManager
  47.      *   The domain aware manager
  48.      * @param string $currentEnvironment
  49.      *   The current environment value - either "dev", "test" or "prod"
  50.      */
  51.     public function __construct(
  52.         Environment $twig,
  53.         AppContext $appContext,
  54.         DomainAwareManager $domainAwareManager,
  55.         string $currentEnvironment
  56.     ) {
  57.         $this->twig $twig;
  58.         $this->appContext $appContext;
  59.         $this->domainAwareManager $domainAwareManager;
  60.         $this->currentEnvironment $currentEnvironment;
  61.     }
  62.     public static function getSubscribedEvents()
  63.     {
  64.         return [
  65.             ExceptionEvent::class => 'onKernelException',
  66.         ];
  67.     }
  68.     /**
  69.      * Public callback for the exception event.
  70.      *
  71.      * Responsible for providing friendly "Not Found" page, only for the PR sites.
  72.      *
  73.      * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
  74.      *   The instance of the dispatched event
  75.      */
  76.     public function onKernelException(ExceptionEvent $event): void
  77.     {
  78.         // If the exception is occurring on site other than a PR one, then we do not provide custom template.
  79.         // If the exception is different than the Not Found one, then we do not provide custom template.
  80.         // If the current environment is not a production one, then we keep the default verbose template.
  81.         // If we don't have a valid domain instance (one that would indicate that we are currently on a
  82.         // PR site, then we keep the default template. That case will occur on AWS, because there we use
  83.         // one container with both PR and VOD API feature flags enabled).
  84.         if (
  85.             !$this->appContext->isPressSiteEnabled()
  86.             || !$this->domainAwareManager->getCurrentByHostnameAndLocale()
  87.             || !$event->getThrowable() instanceof NotFoundHttpException
  88.             || 'dev' === $this->currentEnvironment
  89.         ) {
  90.             return;
  91.         }
  92.         $request $event->getRequest();
  93.         // The request locale could fallback to the default one "en" when an exception
  94.         // is being throw, since the route is not recognized, it may not intercept
  95.         // correctly the locale in the address bar.
  96.         if (!$this->domainAwareManager->supportsLanguage($request->getLocale())) {
  97.             $request->setLocale(
  98.                 $this->domainAwareManager
  99.                     ->getCurrentByHostnameAndLocale()
  100.                     ->getLanguageCode()
  101.             );
  102.         }
  103.         $response = new Response();
  104.         $response->setContent($this->twig->render('press_site/exception/error404.html.twig'));
  105.         $event->setResponse($response);
  106.     }
  107. }