src/Storefront/Framework/AffiliateTracking/AffiliateTrackingListener.php line 32

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\AffiliateTracking;
  3. use Shopware\Core\Checkout\Order\SalesChannel\OrderService;
  4. use Shopware\Core\Framework\Log\Package;
  5. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  6. use Shopware\Core\PlatformRequest;
  7. use Shopware\Storefront\Framework\Routing\StorefrontRouteScope;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. /**
  12.  * @internal
  13.  */
  14. #[Package('storefront')]
  15. class AffiliateTrackingListener implements EventSubscriberInterface
  16. {
  17.     final public const AFFILIATE_CODE_KEY OrderService::AFFILIATE_CODE_KEY;
  18.     final public const CAMPAIGN_CODE_KEY OrderService::CAMPAIGN_CODE_KEY;
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             KernelEvents::CONTROLLER => [
  23.                 ['checkAffiliateTracking'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  24.             ],
  25.         ];
  26.     }
  27.     public function checkAffiliateTracking(ControllerEvent $event): void
  28.     {
  29.         $request $event->getRequest();
  30.         /** @var list<string> $scopes */
  31.         $scopes $request->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);
  32.         // Only process storefront routes
  33.         if (!\in_array(StorefrontRouteScope::ID$scopestrue)) {
  34.             return;
  35.         }
  36.         $session $request->getSession();
  37.         $affiliateCode $request->query->get(self::AFFILIATE_CODE_KEY);
  38.         $campaignCode $request->query->get(self::CAMPAIGN_CODE_KEY);
  39.         if ($affiliateCode) {
  40.             $session->set(self::AFFILIATE_CODE_KEY$affiliateCode);
  41.         }
  42.         if ($campaignCode) {
  43.             $session->set(self::CAMPAIGN_CODE_KEY$campaignCode);
  44.         }
  45.     }
  46. }