src/EventSubscriber/MercureCookieSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\MercureCookieService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. /**
  8.  * Hängt das Mercure-Subscriber-Cookie an die Response, wenn die Controller-Action
  9.  * per Request-Attribut `_mercure_topics` erklärt hat, welche Live-Topics diese Seite
  10.  * abonnieren darf. Zentralisiert das Cookie-Minten (Phase 3a) — die Actions setzen nur
  11.  * eine Zeile `$request->attributes->set('_mercure_topics', [...])`.
  12.  *
  13.  * Firewall-unabhängig (nur ein Set-Cookie), greift also für Dashboard (default) UND
  14.  * QR-Techniker (qrcode) gleichermaßen.
  15.  */
  16. class MercureCookieSubscriber implements EventSubscriberInterface
  17. {
  18.     private MercureCookieService $cookieService;
  19.     public function __construct(MercureCookieService $cookieService)
  20.     {
  21.         $this->cookieService $cookieService;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [KernelEvents::RESPONSE => 'onKernelResponse'];
  26.     }
  27.     public function onKernelResponse(ResponseEvent $event): void
  28.     {
  29.         if (!$event->isMainRequest()) {
  30.             return;
  31.         }
  32.         $request $event->getRequest();
  33.         $topics $request->attributes->get('_mercure_topics');
  34.         if (empty($topics) || !\is_array($topics)) {
  35.             return;
  36.         }
  37.         $event->getResponse()->headers->setCookie(
  38.             $this->cookieService->subscriberCookie($request$topics)
  39.         );
  40.     }
  41. }