<?php declare(strict_types=1);
namespace Acris\ShopSwitch\Subscriber;
use Acris\ShopSwitch\Components\Struct\DetectedCountryIdStruct;
use Acris\ShopSwitch\Components\Struct\ShopSwitchCheckedStruct;
use Acris\ShopSwitch\Components\ShopSwitchCookieService;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
class ResponseCookieSubscriber implements EventSubscriberInterface
{
public const CHECKED_COOKIE_EXTENSION = 'acris_shop_switch_checked';
private ShopSwitchCookieService $shopSwitchCookieService;
private AbstractContextSwitchRoute $contextSwitchRoute;
public function __construct(ShopSwitchCookieService $shopSwitchCookieService, AbstractContextSwitchRoute $contextSwitchRoute)
{
$this->shopSwitchCookieService = $shopSwitchCookieService;
$this->contextSwitchRoute = $contextSwitchRoute;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [
['setResponseCache', -1900]
]
];
}
public function setResponseCache(ResponseEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
$salesChannelContext = $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
if (!$salesChannelContext instanceof SalesChannelContext) {
return;
}
$this->setRememberInCookie($salesChannelContext, $request, $response);
$this->saveDetectedCountryIdInCookie($salesChannelContext, $request, $response);
}
private function setRememberInCookie(SalesChannelContext $salesChannelContext, Request $request, Response $response): void
{
if($salesChannelContext->hasExtension(self::CHECKED_COOKIE_EXTENSION) !== true) {
return;
}
$checkedCookieExtension = $salesChannelContext->getExtension(self::CHECKED_COOKIE_EXTENSION);
if($checkedCookieExtension instanceof ShopSwitchCheckedStruct && empty($checkedCookieExtension->getDomainId()) === false) {
$this->shopSwitchCookieService->rememberInCookie($request, $response, $checkedCookieExtension->getDomainId(), $salesChannelContext);
}
}
private function saveDetectedCountryIdInCookie($salesChannelContext, Request $request, Response $response)
{
if($salesChannelContext->hasExtension(DetectedCountryIdStruct::EXTENSION_KEY) !== true) {
return;
}
$detectedCountryIdStruct = $salesChannelContext->getExtension(DetectedCountryIdStruct::EXTENSION_KEY);
if($detectedCountryIdStruct instanceof DetectedCountryIdStruct && empty($detectedCountryIdStruct->getCountryId()) === false) {
$this->shopSwitchCookieService->saveCountryIdInCookie($detectedCountryIdStruct->getCountryId(), $response, $salesChannelContext);
$this->contextSwitchRoute->switchContext(new RequestDataBag(['countryId' => $detectedCountryIdStruct->getCountryId()]), $salesChannelContext);
}
}
}