<?php
declare(strict_types=1);
namespace Madco\Mykey\Subscriber;
use Madco\Mykey\Constants;
use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class LogoutSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents(): array
{
return [
CustomerLogoutEvent::class => 'onCustomerLogout',
];
}
public function onCustomerLogout(CustomerLogoutEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
if ($request) {
$session = $request->getSession();
$session->remove(Constants::MYKEY_AFFILIATE_SESSION_ATTRIBUTE);
$session->remove(Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE);
}
$event->getContext()->removeExtension(Constants::MYKEY_AFFILIATE_CONTEXT_EXTENSION_KEY);
$event->getContext()->removeExtension(Constants::MYKEY_PARTNER_CONTEXT_EXTENSION_KEY);
$event->getSalesChannelContext()->removeExtension(Constants::MYKEY_AFFILIATE_CONTEXT_EXTENSION_KEY);
$event->getSalesChannelContext()->removeExtension(Constants::MYKEY_PARTNER_CONTEXT_EXTENSION_KEY);
}
}