<?php
namespace App\EventSubscriber;
use App\Tracking\FacebookIds;
use Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final readonly class FacebookIdsSubscriber implements EventSubscriberInterface
{
public function __construct(private FacebookIds $ids) {}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onRequest', 10],
KernelEvents::RESPONSE => ['onResponse', -10],
];
}
public function onRequest(RequestEvent $event): void
{
$req = $event->getRequest();
$this->ids->resolve($req);
}
/**
* @throws Exception
*/
public function onResponse(ResponseEvent $event): void
{
$this->ids->attachCookiesToResponse(
$event->getResponse(),
$event->getRequest()->isSecure()
);
}
}