src/EventSubscriber/PressSite/DomainAwareActionSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\PressSite;
  3. use App\Action\PressSite\DomainAwareAction;
  4. use App\Action\PressSite\Localization\LandingPageAction;
  5. use App\Service\PressSite\DomainAwareManager;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. /**
  11.  * Class DomainAwareActionSubscriber.
  12.  */
  13. class DomainAwareActionSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * The helper service responsible for handling domain logic.
  17.      *
  18.      * @var \App\Service\PressSite\DomainAwareManager
  19.      */
  20.     private $domainAwareManager;
  21.     /**
  22.      * BeforeActionSubscriber constructor.
  23.      *
  24.      * @param \App\Service\PressSite\DomainAwareManager $domainAawreManager
  25.      *   The helper service responsible for handling domain logic
  26.      */
  27.     public function __construct(DomainAwareManager $domainAawreManager)
  28.     {
  29.         $this->domainAwareManager $domainAawreManager;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             KernelEvents::CONTROLLER => 'onKernelController',
  35.         ];
  36.     }
  37.     /**
  38.      * Responsible for acting as before-action event listener.
  39.      *
  40.      * This subscriber will perform any mandatory checks before
  41.      * executing given action. For instance, the mandatory
  42.      * "domain_id" query parameter can be checked here when it's
  43.      * needed for multiple actions.
  44.      *
  45.      * @param \Symfony\Component\HttpKernel\Event\ControllerEvent $event
  46.      *   The instance of the dispatched event
  47.      */
  48.     public function onKernelController(ControllerEvent $event): void
  49.     {
  50.         $controller $event->getController();
  51.         $request $event->getRequest();
  52.         if (!$controller instanceof DomainAwareAction) {
  53.             return;
  54.         }
  55.         // Throw exception for page not found if we cannot obtain a valid
  56.         // reference to domain entity based on the current hostname.
  57.         // Required especially for the LandingPageAction which is responsible
  58.         // for redirecting the member to sign-in page with pre-defined locale.
  59.         if (!$this->domainAwareManager->getCurrentByHostnameAndLocale()) {
  60.             $event->setController(function () {
  61.                 throw new NotFoundHttpException('Page not found');
  62.             });
  63.         }
  64.         // This is a special case for the landing page and that's why we have
  65.         // separated the condition check from the one above. At first, we let
  66.         // the DomainAwareManager handle all domain entity references by
  67.         // the current hostname and language/locale requested and afterwards
  68.         // the member gets redirected to the sign-in page with the proper language
  69.         // selected. But, if we load a valid domain, but with language version
  70.         // that is not supported, then we need to thrown an exception. This case
  71.         // should be valid for all DomainAware actions, but the LandingPage.
  72.         $locale $request->getLocale();
  73.         if (!$controller instanceof LandingPageAction && !$this->domainAwareManager->supportsLanguage($locale)) {
  74.             $event->setController(function () {
  75.                 throw new NotFoundHttpException('Page not found');
  76.             });
  77.         }
  78.     }
  79. }