<?php declare(strict_types=1);
namespace SwagB2bPlatform\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use function array_keys;
use function mb_strpos;
use function sprintf;
class CookieSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => 'overrideCsrfCookies',
];
}
public function overrideCsrfCookies(ResponseEvent $event): void
{
$cookies = $event->getRequest()->cookies;
$csrf = $cookies->get('csrf');
if (!$csrf) {
return;
}
$names = array_keys($csrf);
foreach ($names as $name) {
if (mb_strpos($name, 'frontend.b2b') === false) {
continue;
}
$cookie = new Cookie(sprintf('csrf[%s]', $name));
$event->getResponse()->headers->setCookie($cookie);
}
}
}