src/EventSubscriber/RefererEventSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Traits\UrlTrait;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Twig\Environment;
  8. class RefererEventSubscriber implements EventSubscriberInterface
  9. {
  10.     use UrlTrait;
  11.     private Environment $twig;
  12.     private RequestStack $requestStack;
  13.     public function __construct(Environment $twigRequestStack $requestStack)
  14.     {
  15.         $this->twig $twig;
  16.         $this->requestStack $requestStack;
  17.     }
  18.     public function onControllerEvent(ControllerEvent $event)
  19.     {
  20.         $headers $event->getRequest()->headers;
  21.         $referer $headers->get('referer');
  22.         $euroUrl $_ENV['EUROCAMIONES_URL'] ?? null;
  23.         $hostReferer $this->getHostUrl($referer);
  24.         $hostEuroUrl $this->getHostUrl($euroUrl);
  25.         $refererFromEuro $hostReferer == $hostEuroUrl;
  26.         $session $this->requestStack->getSession();
  27.         if (!$session->get('referer_euro')) {
  28.             $session->set('referer_euro'$refererFromEuro);
  29.         }
  30.         $this->twig->addGlobal('referer_euro'$refererFromEuro);
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             ControllerEvent::class => 'onControllerEvent',
  36.         ];
  37.     }
  38. }