custom/plugins/shopware6-sapoci/src/Subscriber/ResponseHeaderSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Orcamultimedia\OciPunchout\Subscriber;
  4. use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
  5. use Shopware\Core\PlatformRequest;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Cookie;
  8. use Symfony\Component\HttpFoundation\Response;
  9. class ResponseHeaderSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var array
  13.      */
  14.     private array $frameancestors;
  15.     public function __construct(
  16.         array $frameancestors
  17.     )
  18.     {
  19.         $this->frameancestors $frameancestors;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             BeforeSendResponseEvent::class => [['beforeSendOciResponse']]
  25.         ];
  26.     }
  27.     public function beforeSendOciResponse(BeforeSendResponseEvent $event): void
  28.     {
  29.         $response $event->getResponse();
  30.         $request $event->getRequest();
  31.         $requestCookie $request->cookies->get('sapoci');
  32.         $responseCookies $response->headers->getCookies();
  33.         $responseCookie array_filter($responseCookies, fn(Cookie $cookie) => $cookie->getName() === 'sapoci');
  34.         $isPunchout false;
  35.         if (
  36.             $response->getStatusCode() !== Response::HTTP_OK
  37.             && $response->getStatusCode() !== Response::HTTP_NOT_FOUND
  38.             && $response->getStatusCode() !== Response::HTTP_FOUND
  39.         ) {
  40.             return;
  41.         }
  42.         if ($request->hasSession()) {
  43.             $session $request->getSession();
  44.         } elseif ($requestCookie == 'is-punchout') {
  45.             $isPunchout true;
  46.         } elseif ($responseCookie && $responseCookie[0]->getValue() == 'is-punchout') {
  47.             $isPunchout true;
  48.         } else {
  49.             return;
  50.         }
  51.         if (!$isPunchout && (isset($session) && !$session->get('oci'))) {
  52.             return;
  53.         }
  54.         if ($response->headers->has(PlatformRequest::HEADER_FRAME_OPTIONS)) {
  55.             $response->headers->remove(PlatformRequest::HEADER_FRAME_OPTIONS);
  56.         }
  57.         $response->headers->set(
  58.             'Content-Security-Policy',
  59.             sprintf("frame-ancestors %s"implode(' '$this->frameancestors)),
  60.             false
  61.         );
  62.         foreach ($response->headers->getCookies() as $cookie) {
  63.             $response->headers->setCookie($cookie->withSameSite(Cookie::SAMESITE_NONE)->withSecure());
  64.         }
  65.         $event->setResponse($response);
  66.     }
  67. }