src/Listener/Cinema/CinemaContextListener.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Listener\Cinema;
  4. use App\Event\Cinema\CinemaContextOnDemandEvent;
  5. use App\WsProxy\WsProxy;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  8. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  9. class CinemaContextListener
  10. {
  11.     private EventDispatcherInterface $eventDispatcher;
  12.     private WsProxy $wsProxy;
  13.     public const EXCLUDED_ENDPOINTS = [
  14.         [
  15.             'method' => 'GET',
  16.             'path' => '/user/preferences'
  17.         ],
  18.         [
  19.             'method' => 'PUT',
  20.             'path' => '/user/preferences'
  21.         ]
  22.     ];
  23.     /**
  24.      * CinemaContextListener constructor.
  25.      * @param EventDispatcherInterface $eventDispatcher
  26.      * @param WsProxy $wsProxy
  27.      */
  28.     public function __construct(EventDispatcherInterface $eventDispatcherWsProxy $wsProxy)
  29.     {
  30.         $this->eventDispatcher $eventDispatcher;
  31.         $this->wsProxy $wsProxy;
  32.     }
  33.     public function onKernelController(\Symfony\Component\HttpKernel\Event\ControllerEvent $event)
  34.     {
  35.         $request $event->getRequest();
  36.         $this->wsProxy->setContextLanguage($request->getLocale());
  37.         $cinemaId null;
  38.         if ($request->attributes->get('cinemaId')) {
  39.             $cinemaId $request->attributes->get('cinemaId');
  40.         }
  41.         if ($request->get('cinemaId')) {
  42.             $cinemaId $request->get('cinemaId');
  43.         }
  44.         if ($cinemaId !== null && !$this->isPathExcluded($request)) {
  45.             $this->eventDispatcher->dispatch(
  46.                 new CinemaContextOnDemandEvent((string) $cinemaId)
  47.             );
  48.         }
  49.     }
  50.     private function isPathExcluded(Request $request): bool
  51.     {
  52.         foreach (self::EXCLUDED_ENDPOINTS as $endpoint) {
  53.             if ($endpoint['method'] === $request->getMethod() && $endpoint['path'] === $request->getPathInfo()) {
  54.                 return true;
  55.             }
  56.         }
  57.         return false;
  58.     }
  59. }