<?php
namespace App\EventSubscriber;
use App\Traits\UrlTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Twig\Environment;
class RefererEventSubscriber implements EventSubscriberInterface
{
use UrlTrait;
private Environment $twig;
private RequestStack $requestStack;
public function __construct(Environment $twig, RequestStack $requestStack)
{
$this->twig = $twig;
$this->requestStack = $requestStack;
}
public function onControllerEvent(ControllerEvent $event)
{
$headers = $event->getRequest()->headers;
$referer = $headers->get('referer');
$euroUrl = $_ENV['EUROCAMIONES_URL'] ?? null;
$hostReferer = $this->getHostUrl($referer);
$hostEuroUrl = $this->getHostUrl($euroUrl);
$refererFromEuro = $hostReferer == $hostEuroUrl;
$session = $this->requestStack->getSession();
if (!$session->get('referer_euro')) {
$session->set('referer_euro', $refererFromEuro);
}
$this->twig->addGlobal('referer_euro', $refererFromEuro);
}
public static function getSubscribedEvents(): array
{
return [
ControllerEvent::class => 'onControllerEvent',
];
}
}