<?php declare(strict_types=1);
namespace S360ListingViewSwitch\Storefront\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Framework\Cache\Event\HttpCacheGenerateKeyEvent;
use S360ListingViewSwitch\S360ListingViewSwitch;
class HttpCacheGenerateKeyEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
HttpCacheGenerateKeyEvent::class => 'addCacheKey'
];
}
/**
* if cookie is set and value is valid, cache key gets decorated
* one cannot access session in here, not started yet and would cause conflict if started here
*/
public function addCacheKey(HttpCacheGenerateKeyEvent $event): void
{
// default, TODO: replace with default value
$listingMode = "gallery";
if(isset($_COOKIE[S360ListingViewSwitch::COOKIE_KEY])){
if(in_array($_COOKIE[S360ListingViewSwitch::COOKIE_KEY], S360ListingViewSwitch::AVAILABLE_VIEWS)){
$listingMode = $_COOKIE[S360ListingViewSwitch::COOKIE_KEY];
}
}
$hash = 'md' . hash('sha256', $event->getHash() . "-" . $listingMode);
$event->setHash($hash);
}
}