custom/plugins/MadMykey/src/Subscriber/RequestSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Madco\Mykey\Subscriber;
  4. use Madco\Mykey\Constants;
  5. use Madco\Mykey\Struct\CurrentMykeyAffiliate;
  6. use Shopware\Storefront\Framework\AffiliateTracking\AffiliateTrackingListener;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. class RequestSubscriber implements EventSubscriberInterface
  11. {
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [
  15.             KernelEvents::REQUEST => ['onKernelRequest', -1],
  16.         ];
  17.     }
  18.     public function onKernelRequest(RequestEvent $event): void
  19.     {
  20.         if (!$event->isMasterRequest()) {
  21.             return;
  22.         }
  23.         $request $event->getRequest();
  24.         $centerId $request->query->getDigits(Constants::MYKEY_CENTER_ID_QUERY_PARAM'');
  25.         if ('' !== $centerId) {
  26.             $referrer $request->getUri();
  27.             $affiliateRequestAttribute CurrentMykeyAffiliate::fromCenterId(
  28.                 $centerId,
  29.                 $referrer,
  30.                 null,
  31.                 CurrentMykeyAffiliate::AFFILIATE_ORIGIN_AFFILIATE_LINK
  32.             );
  33.             $request->getSession()->set(Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE$affiliateRequestAttribute);
  34.             $request->query->set(AffiliateTrackingListener::AFFILIATE_CODE_KEY$centerId);
  35.         }
  36.     }
  37. }