vendor/ibexa/activity-log/src/bundle/EventSubscriber/SourceListener/WebRequestEventSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\ActivityLog\EventSubscriber\SourceListener;
  8. use Ibexa\Contracts\ActivityLog\ActivityLogServiceInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. final class WebRequestEventSubscriber implements EventSubscriberInterface
  13. {
  14.     private ActivityLogServiceInterface $activityLogService;
  15.     public function __construct(
  16.         ActivityLogServiceInterface $activityLogService
  17.     ) {
  18.         $this->activityLogService $activityLogService;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             KernelEvents::REQUEST => [
  24.                 'onKernelRequest', -10,
  25.             ],
  26.         ];
  27.     }
  28.     public function onKernelRequest(RequestEvent $event): void
  29.     {
  30.         if (!$event->isMainRequest()) {
  31.             return;
  32.         }
  33.         if ($event->getRequest()->attributes->getBoolean('is_rest_request')) {
  34.             $context $this->activityLogService->prepareContext('rest');
  35.         } elseif ($event->getRequest()->attributes->get('_route') === 'overblog_graphql_endpoint') {
  36.             $context $this->activityLogService->prepareContext('graphql');
  37.         } else {
  38.             $context $this->activityLogService->prepareContext('web');
  39.         }
  40.         $ip $event->getRequest()->getClientIp();
  41.         if ($ip !== null) {
  42.             $context->setIp($ip);
  43.         }
  44.     }
  45. }