src/EventSubscriber/Api/CorrelationIdSubscriber.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Api;
  3. use App\Helper\Api\Logging\CorrelationIdService;
  4. use Exception;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. /**
  10.  * Event subscriber to manage correlation IDs for HTTP requests.
  11.  *
  12.  * Initializes a correlation ID at the start of each request and resets it
  13.  * when the request is complete. Supports propagating correlation IDs from
  14.  * incoming X-Correlation-ID headers.
  15.  *
  16.  * @package API
  17.  * @internal
  18.  */
  19. class CorrelationIdSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * Correlation ID service.
  23.      *
  24.      * @var CorrelationIdService
  25.      */
  26.     protected CorrelationIdService $correlationIdService;
  27.     /**
  28.      * Get subscribed events.
  29.      *
  30.      * @return array<string, array<int, string|int>>
  31.      */
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             KernelEvents::REQUEST => ['onKernelRequest'1024],
  36.             KernelEvents::TERMINATE => ['onKernelTerminate', -1024],
  37.         ];
  38.     }
  39.     /**
  40.      * Constructor.
  41.      *
  42.      * @param CorrelationIdService $correlationIdService Correlation ID service
  43.      */
  44.     public function __construct(CorrelationIdService $correlationIdService)
  45.     {
  46.         $this->correlationIdService $correlationIdService;
  47.     }
  48.     /**
  49.      * Initialize correlation ID on request start.
  50.      *
  51.      * Checks for an existing X-Correlation-ID header and uses it if present,
  52.      * otherwise generates a new correlation ID.
  53.      *
  54.      * @param RequestEvent $event The request event
  55.      * @return void
  56.      * @throws Exception
  57.      */
  58.     public function onKernelRequest(RequestEvent $event): void
  59.     {
  60.         if (!$event->isMainRequest()) {
  61.             return;
  62.         }
  63.         $request $event->getRequest();
  64.         // Check if correlation ID is provided in request headers
  65.         if ($request->headers->has('X-Correlation-ID')) {
  66.             $correlationId $request->headers->get('X-Correlation-ID');
  67.             if (!empty($correlationId)) {
  68.                 $this->correlationIdService->setCorrelationId($correlationId);
  69.                 return;
  70.             }
  71.         }
  72.         // Generate a new correlation ID if not provided
  73.         $this->correlationIdService->getCorrelationId();
  74.     }
  75.     /**
  76.      * Reset correlation ID after request is complete.
  77.      *
  78.      * @param TerminateEvent $event The terminate-event
  79.      * @return void
  80.      */
  81.     public function onKernelTerminate(TerminateEvent $event): void
  82.     {
  83.         if (!$event->isMainRequest()) {
  84.             return;
  85.         }
  86.         $this->correlationIdService->reset();
  87.     }
  88. }