vendor/shopware/core/Framework/Adapter/Translation/TranslatorCacheInvalidate.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Translation;
  3. use Doctrine\DBAL\Connection;
  4. use Psr\Cache\CacheItemPoolInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\System\Snippet\Aggregate\SnippetSet\SnippetSetDefinition;
  7. use Shopware\Core\System\Snippet\SnippetDefinition;
  8. use Shopware\Core\System\Snippet\SnippetEvents;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class TranslatorCacheInvalidate implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var CacheItemPoolInterface
  14.      */
  15.     private $cache;
  16.     /**
  17.      * @var Connection
  18.      */
  19.     private $connection;
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(CacheItemPoolInterface $cacheConnection $connection)
  24.     {
  25.         $this->cache $cache;
  26.         $this->connection $connection;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             SnippetEvents::SNIPPET_WRITTEN_EVENT => 'invalidate',
  32.             SnippetEvents::SNIPPET_DELETED_EVENT => 'invalidate',
  33.             SnippetEvents::SNIPPET_SET_DELETED_EVENT => 'invalidate',
  34.         ];
  35.     }
  36.     public function invalidate(EntityWrittenEvent $event): void
  37.     {
  38.         if ($event->getEntityName() === SnippetSetDefinition::ENTITY_NAME) {
  39.             $this->clearCache($event->getIds());
  40.             return;
  41.         }
  42.         if ($event->getEntityName() === SnippetDefinition::ENTITY_NAME) {
  43.             $snippetIds $event->getIds();
  44.             $rows $this->connection->fetchAll(
  45.                 'SELECT LOWER(HEX(snippet_set_id)) id FROM snippet WHERE HEX(id) IN (:ids)',
  46.                 ['ids' => $snippetIds],
  47.                 ['ids' => Connection::PARAM_STR_ARRAY]
  48.             );
  49.             $setIds = [];
  50.             foreach ($rows as ['id' => $id]) {
  51.                 $setIds[] = $id;
  52.             }
  53.             $this->clearCache($setIds);
  54.             return;
  55.         }
  56.     }
  57.     /**
  58.      * @param array<string> $snippetSetIds
  59.      */
  60.     private function clearCache(array $snippetSetIds): void
  61.     {
  62.         $snippetSetIds array_unique($snippetSetIds);
  63.         foreach ($snippetSetIds as $id) {
  64.             $this->cache->deleteItem('translation.catalog.' $id);
  65.         }
  66.     }
  67. }