<?php
namespace App\EventSubscriber;
use App\Service\MercureCookieService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Hängt das Mercure-Subscriber-Cookie an die Response, wenn die Controller-Action
* per Request-Attribut `_mercure_topics` erklärt hat, welche Live-Topics diese Seite
* abonnieren darf. Zentralisiert das Cookie-Minten (Phase 3a) — die Actions setzen nur
* eine Zeile `$request->attributes->set('_mercure_topics', [...])`.
*
* Firewall-unabhängig (nur ein Set-Cookie), greift also für Dashboard (default) UND
* QR-Techniker (qrcode) gleichermaßen.
*/
class MercureCookieSubscriber implements EventSubscriberInterface
{
private MercureCookieService $cookieService;
public function __construct(MercureCookieService $cookieService)
{
$this->cookieService = $cookieService;
}
public static function getSubscribedEvents(): array
{
return [KernelEvents::RESPONSE => 'onKernelResponse'];
}
public function onKernelResponse(ResponseEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$request = $event->getRequest();
$topics = $request->attributes->get('_mercure_topics');
if (empty($topics) || !\is_array($topics)) {
return;
}
$event->getResponse()->headers->setCookie(
$this->cookieService->subscriberCookie($request, $topics)
);
}
}