custom/plugins/AcrisCookieConsentCS/src/Subscriber/BeforeResponseSendSubscriber.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CookieConsent\Subscriber;
  3. use Acris\CookieConsent\Components\CookieService;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
  6. use Shopware\Core\PlatformRequest;
  7. use Shopware\Storefront\Framework\Routing\StorefrontResponse;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10. class BeforeResponseSendSubscriber implements EventSubscriberInterface
  11. {
  12.     private CookieService $cookieService;
  13.     public function __construct(CookieService $cookieService)
  14.     {
  15.         $this->cookieService $cookieService;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             BeforeSendResponseEvent::class => 'setFunctionalCookiesWithValue',
  21.         ];
  22.     }
  23.     public function setFunctionalCookiesWithValue(BeforeSendResponseEvent $responseEvent): void
  24.     {
  25.         $request $responseEvent->getRequest();
  26.         $response $responseEvent->getResponse();
  27.         if ($response instanceof StorefrontResponse) {
  28.             if ($request->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID)) {
  29.                 $context Context::createDefaultContext();
  30.                 $defaultCookies $this->cookieService->getDefaultCookies($request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID), $context);
  31.                 $requestCookies $request->cookies->all();
  32.                 if (!empty($defaultCookies)) {
  33.                     foreach ($defaultCookies as $cookieEntity) {
  34.                         if (empty($requestCookies) || !empty($requestCookies) && !array_key_exists($cookieEntity->getCookieId(), $requestCookies)) {
  35.                             $response->headers->setCookie(new Cookie($cookieEntity->getCookieId(), $cookieEntity->getDefaultValue(), time() + (86400 30), '/'nullfalsefalse));
  36.                         }
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.     }
  42. }