<?php declare(strict_types=1);
namespace Acris\CookieConsent\Subscriber;
use Shopware\Storefront\Framework\Routing\NotFound\NotFoundPageCacheKeyEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
class NotFoundCacheKeySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
// we don't want to get the event name by ::class because of event did not exist before 6.4.15.0.
return [
'Shopware\Storefront\Framework\Routing\NotFound\NotFoundPageCacheKeyEvent' => 'onNotFoundPageCacheKeyEvent'
];
}
public function onNotFoundPageCacheKeyEvent(NotFoundPageCacheKeyEvent $notFoundPageCacheKeyEvent): void
{
if($notFoundPageCacheKeyEvent->getRequest()->cookies instanceof ParameterBag && !empty($notFoundPageCacheKeyEvent->getRequest()->cookies->all()) && !empty($notFoundPageCacheKeyEvent->getKey()) && is_string($notFoundPageCacheKeyEvent->getKey()) && is_array($notFoundPageCacheKeyEvent->getRequest()->cookies->all())) {
$key = json_encode($notFoundPageCacheKeyEvent->getRequest()->cookies->all());
if (empty($key)) return;
$hashKey = md5($key);
if (empty($hashKey)) return;
$notFoundPageCacheKeyEvent->setKey($notFoundPageCacheKeyEvent->getKey() . $hashKey);
}
}
}