src/EventSubscriber/TrackingInfoSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use App\Entity\UserInterface;
  5. use App\Event\ObjectEvent;
  6. use App\Events;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. /**
  15.  * Tracking information used for marketing.
  16.  */
  17. class TrackingInfoSubscriber implements EventSubscriberInterface
  18. {
  19.     public const SESSION_NAME 'app_tracking_info';
  20.     /** @var RequestStack */
  21.     private $requestStack;
  22.     /** @var ManagerRegistry */
  23.     private $managerRegistry;
  24.     /**
  25.      * @param RequestStack    $requestStack
  26.      * @param ManagerRegistry $managerRegistry
  27.      */
  28.     public function __construct(RequestStack $requestStackManagerRegistry $managerRegistry)
  29.     {
  30.         $this->requestStack $requestStack;
  31.         $this->managerRegistry $managerRegistry;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             KernelEvents::REQUEST => 'onKernelRequest',
  40.             Events::USER_CREATED => 'onUserCreated',
  41.         ];
  42.     }
  43.     /**
  44.      * @param RequestEvent $event
  45.      */
  46.     public function onKernelRequest(RequestEvent $event): void
  47.     {
  48.         if (!$event->isMainRequest()) {
  49.             return;
  50.         }
  51.         $request $event->getRequest();
  52.         if ($request->isXmlHttpRequest()) {
  53.             return;
  54.         }
  55.         $session $request->getSession();
  56.         if ($session->has(self::SESSION_NAME)) {
  57.             return;
  58.         }
  59.         $trackingInfo = [
  60.             'referer' => $request->headers->get('Referer'),
  61.             'ipAddress' => $request->getClientIp(),
  62.             'userAgent' => $request->headers->get('User-Agent'),
  63.         ];
  64.         // Loop through all query parameters and add those starting with "utm_"
  65.         foreach ($request->query->all() as $key => $value) {
  66.             if (=== strpos($key'utm_')) {
  67.                 $trackingInfo[$key] = $value;
  68.             }
  69.         }
  70.         // Ignore requests from referrers with the same HTTP host
  71.         if (null !== $trackingInfo['referer'] && === stripos($trackingInfo['referer'], $request->getSchemeAndHttpHost())) {
  72.             return;
  73.         }
  74.         $filteredTrackingInfo array_filter($trackingInfo);
  75.         $session->set(self::SESSION_NAME$filteredTrackingInfo);
  76.     }
  77.     /**
  78.      * @param ObjectEvent $event
  79.      */
  80.     public function onUserCreated(ObjectEvent $event): void
  81.     {
  82.         try {
  83.             /** @var SessionInterface $session */
  84.             $session $this->requestStack->getSession();
  85.         } catch (SessionNotFoundException $e) {
  86.             return;
  87.         }
  88.         if (!$session->has(self::SESSION_NAME)) {
  89.             return;
  90.         }
  91.         $entity $event->getObject();
  92.         $trackingInfo $session->get(self::SESSION_NAME);
  93.         if ($entity instanceof UserInterface && !empty($trackingInfo)) {
  94.             $entity->setTrackingInfo($trackingInfo);
  95.             $this->managerRegistry
  96.                 ->getRepository(User::class)
  97.                 ->update($entity);
  98.         }
  99.         $session->remove(self::SESSION_NAME);
  100.     }
  101. }