<?php declare(strict_types=1);
namespace ShopStudio\PixelYourShop\Framework\Adapter\Cache;
use ShopStudio\PixelYourShop\Pixel\PixelDefinition;
use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\System\SystemConfig\CachedSystemConfigLoader;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @since 2.0.0
*/
class CacheInvalidationSubscriber implements EventSubscriberInterface
{
/**
* @since 2.0.0
*/
private CacheInvalidator $cacheInvalidator;
/**
* @since 2.0.0
*/
public function __construct(CacheInvalidator $cacheInvalidator)
{
$this->cacheInvalidator = $cacheInvalidator;
}
/**
* @since 2.0.0
*/
public static function getSubscribedEvents(): array
{
return [
EntityWrittenContainerEvent::class => [
['invalidateConfigKeys', 2000],
],
];
}
/**
* Delete any config to clear the pixel-related cache.
*
* @since 2.0.0
*/
public function invalidateConfigKeys(EntityWrittenContainerEvent $event): void
{
$pixelWrittenEvent = $event->getEventByEntityName(PixelDefinition::ENTITY_NAME);
if ($pixelWrittenEvent === null) {
return;
}
$this->cacheInvalidator->invalidate([
SystemConfigService::buildName('ShopStudioPixelYourShop.config.cookieConsentProvider'),
SystemConfigService::buildName('ShopStudioPixelYourShop.config.debugEnabled'),
CachedSystemConfigLoader::CACHE_TAG,
]);
}
}