<?php
declare(strict_types=1);
namespace Madco\Mykey\Storefront\Framework\Cache;
use Bodymed\Webshop\Core\Content\BodymedCenter\BodymedCenterEntity;
use Madco\Mykey\Struct\CurrentMykeyPartner;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber as CoreCacheResponseSubscriber;
use Madco\Mykey\Constants;
/**
* Erweitert den HTTP-Cache um Mykey-Funktionalität,
* damit gecachte Seiten den Mykey berücksichtigen.
*/
class CacheResponseSubscriber implements EventSubscriberInterface
{
private bool $httpCacheEnabled;
public function __construct($httpCacheEnabled)
{
$this->httpCacheEnabled = (bool) $httpCacheEnabled;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => [
['setResponseCache', -2500],
],
];
}
public function setResponseCache(ResponseEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
$salesChannelContext = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
if (!$this->httpCacheEnabled) {
return;
}
$cookies = $response->headers->getCookies();
if ($salesChannelContext instanceof SalesChannelContext) {
foreach ($cookies as $cookie) {
if ($cookie->getName() === CoreCacheResponseSubscriber::CONTEXT_CACHE_COOKIE) {
$cookie = Cookie::create(CoreCacheResponseSubscriber::CONTEXT_CACHE_COOKIE, $this->buildCacheHash($salesChannelContext));
$cookie->setSecureDefault($request->isSecure());
$response->headers->setCookie($cookie);
}
}
$mykeyPartnerStruct = $salesChannelContext->getExtension(Constants::MYKEY_PARTNER_CONTEXT_EXTENSION_KEY);
if ($mykeyPartnerStruct instanceof CurrentMykeyPartner) {
if ($mykeyPartnerStruct->getBodymedCenter() instanceof BodymedCenterEntity) {
$mykeyCookie = Cookie::create(Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE, \md5($mykeyPartnerStruct->getCode()));
$response->headers->setCookie($mykeyCookie);
}
}
}
}
/**
* Erweiterung des Http-Cache-hash um den Mykey, sodass für jeden Kunden mit einem Mykey
* ein individueller Cache generiert wird
*/
private function buildCacheHash(SalesChannelContext $salesChannelContext): string
{
$hashData = [
$salesChannelContext->getRuleIds(),
$salesChannelContext->getContext()->getVersionId(),
$salesChannelContext->getCurrency()->getId(),
];
$mykeyPartnerStruct = $salesChannelContext->getExtension(Constants::MYKEY_PARTNER_CONTEXT_EXTENSION_KEY);
if ($mykeyPartnerStruct instanceof CurrentMykeyPartner) {
if ($mykeyPartnerStruct->getBodymedCenter() instanceof BodymedCenterEntity) {
if ($mykeyPartnerStruct->getBodymedCenter()->getTags() &&
$mykeyPartnerStruct->getBodymedCenter()->getTags()->count() > 0
) {
$hashData[] = $mykeyPartnerStruct->getBodymedCenter()->getTags()->getIds();
} else {
$hashData[] = $mykeyPartnerStruct->getBodymedCenter()->getCode();
}
}
}
return \md5(\json_encode($hashData));
}
}