<?php
namespace Crehler\MojeBambinoEngine\Subscriber;
use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber;
use Shopware\Storefront\Framework\Routing\Router;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LogoutSubscriber implements EventSubscriberInterface
{
protected Router $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => ['onResponse', -2500]
];
}
public function onResponse(ResponseEvent $event)
{
if ('frontend.account.logout.page' === $event->getRequest()->attributes->get('_route') && $event->getResponse() instanceof RedirectResponse) {
$loginPage = $this->router->generate('frontend.account.login.page');
if ($loginPage !== $event->getResponse()->getTargetUrl()) return;
$event->setResponse(new RedirectResponse($this->router->generate('frontend.home.page')));
$event->getResponse()->headers->removeCookie(CacheResponseSubscriber::SYSTEM_STATE_COOKIE);
$event->getResponse()->headers->clearCookie(CacheResponseSubscriber::SYSTEM_STATE_COOKIE);
$event->getResponse()->headers->removeCookie(CacheResponseSubscriber::CONTEXT_CACHE_COOKIE);
$event->getResponse()->headers->clearCookie(CacheResponseSubscriber::CONTEXT_CACHE_COOKIE);
}
}
}