custom/plugins/AcrisShopSwitchCS/src/Subscriber/ResponseCookieSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ShopSwitch\Subscriber;
  3. use Acris\ShopSwitch\Components\Struct\DetectedCountryIdStruct;
  4. use Acris\ShopSwitch\Components\Struct\ShopSwitchCheckedStruct;
  5. use Acris\ShopSwitch\Components\ShopSwitchCookieService;
  6. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  7. use Shopware\Core\PlatformRequest;
  8. use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class ResponseCookieSubscriber implements EventSubscriberInterface
  16. {
  17.     public const CHECKED_COOKIE_EXTENSION 'acris_shop_switch_checked';
  18.     private ShopSwitchCookieService $shopSwitchCookieService;
  19.     private AbstractContextSwitchRoute $contextSwitchRoute;
  20.     public function __construct(ShopSwitchCookieService $shopSwitchCookieServiceAbstractContextSwitchRoute $contextSwitchRoute)
  21.     {
  22.         $this->shopSwitchCookieService $shopSwitchCookieService;
  23.         $this->contextSwitchRoute $contextSwitchRoute;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::RESPONSE => [
  29.                 ['setResponseCache', -1900]
  30.             ]
  31.         ];
  32.     }
  33.     public function setResponseCache(ResponseEvent $event)
  34.     {
  35.         $request $event->getRequest();
  36.         $response $event->getResponse();
  37.         $salesChannelContext $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  38.         if (!$salesChannelContext instanceof SalesChannelContext) {
  39.             return;
  40.         }
  41.         $this->setRememberInCookie($salesChannelContext$request$response);
  42.         $this->saveDetectedCountryIdInCookie($salesChannelContext$request$response);
  43.     }
  44.     private function setRememberInCookie(SalesChannelContext $salesChannelContextRequest $requestResponse $response): void
  45.     {
  46.         if($salesChannelContext->hasExtension(self::CHECKED_COOKIE_EXTENSION) !== true) {
  47.             return;
  48.         }
  49.         $checkedCookieExtension $salesChannelContext->getExtension(self::CHECKED_COOKIE_EXTENSION);
  50.         if($checkedCookieExtension instanceof ShopSwitchCheckedStruct && empty($checkedCookieExtension->getDomainId()) === false) {
  51.             $this->shopSwitchCookieService->rememberInCookie($request$response$checkedCookieExtension->getDomainId(), $salesChannelContext);
  52.         }
  53.     }
  54.     private function saveDetectedCountryIdInCookie($salesChannelContextRequest $requestResponse $response)
  55.     {
  56.         if($salesChannelContext->hasExtension(DetectedCountryIdStruct::EXTENSION_KEY) !== true) {
  57.             return;
  58.         }
  59.         $detectedCountryIdStruct $salesChannelContext->getExtension(DetectedCountryIdStruct::EXTENSION_KEY);
  60.         if($detectedCountryIdStruct instanceof DetectedCountryIdStruct && empty($detectedCountryIdStruct->getCountryId()) === false) {
  61.             $this->shopSwitchCookieService->saveCountryIdInCookie($detectedCountryIdStruct->getCountryId(), $response$salesChannelContext);
  62.             $this->contextSwitchRoute->switchContext(new RequestDataBag(['countryId' => $detectedCountryIdStruct->getCountryId()]), $salesChannelContext);
  63.         }
  64.     }
  65. }