<?php declare(strict_types=1);
namespace Acris\ShopSwitch\Subscriber;
use Acris\ShopSwitch\Components\Struct\RedirectResponseStruct;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Framework\Routing\StorefrontResponse;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ResponseRedirectSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [
['setResponseCache', 9000]
]
];
}
public function setResponseCache(ResponseEvent $event)
{
$response = $event->getResponse();
if($response instanceof StorefrontResponse && $response->getContext() instanceof SalesChannelContext) {
$salesChannelContext = $response->getContext();
if($salesChannelContext->hasExtension(RedirectResponseStruct::EXTENSION_KEY) === true) {
$event->setResponse($salesChannelContext->getExtension(RedirectResponseStruct::EXTENSION_KEY)->getRedirectResponse());
}
}
}
}