<?php
declare(strict_types=1);
namespace Madco\Mykey\Subscriber;
use Madco\Mykey\Constants;
use Madco\Mykey\Struct\CurrentMykeyAffiliate;
use Shopware\Storefront\Framework\AffiliateTracking\AffiliateTrackingListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class RequestSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', -1],
];
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
$centerId = $request->query->getDigits(Constants::MYKEY_CENTER_ID_QUERY_PARAM, '');
if ('' !== $centerId) {
$referrer = $request->getUri();
$affiliateRequestAttribute = CurrentMykeyAffiliate::fromCenterId(
$centerId,
$referrer,
null,
CurrentMykeyAffiliate::AFFILIATE_ORIGIN_AFFILIATE_LINK
);
$request->getSession()->set(Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE, $affiliateRequestAttribute);
$request->query->set(AffiliateTrackingListener::AFFILIATE_CODE_KEY, $centerId);
}
}
}