vendor/comalia/gesica_bundle/EventSubscriber/CorrelationIdAuditSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Comalia\GesicaBundle\EventSubscriber;
  4. use Comalia\GesicaBundle\Service\CorrelationIdService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class CorrelationIdAuditSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * kernel.request priority
  15.      * 40 : pour déclencher le subscriber avant le correlationIdProcessor
  16.      */
  17.     const REQUEST_POST_READ 40;
  18.     /**
  19.      * @var CorrelationIdService
  20.      */
  21.     private $correlationIdService;
  22.     /** @var RequestStack */
  23.     private $requestStack;
  24.     /**
  25.      * CorrelationIdAuditSubscriber constructor.
  26.      * @param CorrelationIdService $correlationIdService
  27.      * @param RequestStack $requestStack
  28.      */
  29.     public function __construct(CorrelationIdService $correlationIdServiceRequestStack $requestStack)
  30.     {
  31.         $this->correlationIdService $correlationIdService;
  32.         $this->requestStack $requestStack;
  33.     }
  34.     /**
  35.      * @param RequestEvent $event
  36.      */
  37.     public function onKernelRequest(RequestEvent $event): void
  38.     {
  39.         $request $event->getRequest();
  40.         $session $this->getSession();
  41.         if ($session) {
  42.             if ($request->headers->has('x-correlation-id') === false) {
  43.                 $session->set('x-correlation-id'$this->correlationIdService->getCorrelationId());
  44.                 return;
  45.             }
  46.             $session->set('x-correlation-id'$request->headers->get('x-correlation-id'));
  47.             $request->setSession($session);
  48.         }
  49.     }
  50.     /**
  51.      * @inheritDoc
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             KernelEvents::REQUEST => ['onKernelRequest'self::REQUEST_POST_READ]
  57.         ];
  58.     }
  59.     /**
  60.      * @return SessionInterface
  61.      */
  62.     private function getSession(): ?SessionInterface
  63.     {
  64.         try {
  65.             return $this->requestStack->getSession();
  66.         } catch (SessionNotFoundException $ex) {
  67.             return null;
  68.         }
  69.     }
  70. }