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

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.     public function __construct(CacheItemPoolInterface $cacheConnection $connection)
  21.     {
  22.         $this->cache $cache;
  23.         $this->connection $connection;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             SnippetEvents::SNIPPET_WRITTEN_EVENT => 'invalidate',
  29.             SnippetEvents::SNIPPET_DELETED_EVENT => 'invalidate',
  30.             SnippetEvents::SNIPPET_SET_DELETED_EVENT => 'invalidate',
  31.         ];
  32.     }
  33.     public function invalidate(EntityWrittenEvent $event): void
  34.     {
  35.         if ($event->getEntityName() === SnippetSetDefinition::ENTITY_NAME) {
  36.             $this->clearCache($event->getIds());
  37.             return;
  38.         }
  39.         if ($event->getEntityName() === SnippetDefinition::ENTITY_NAME) {
  40.             $snippetIds $event->getIds();
  41.             $rows $this->connection->fetchAll(
  42.                 'SELECT LOWER(HEX(snippet_set_id)) id FROM snippet WHERE HEX(id) IN (:ids)',
  43.                 ['ids' => $snippetIds],
  44.                 ['ids' => Connection::PARAM_STR_ARRAY]
  45.             );
  46.             $setIds = [];
  47.             foreach ($rows as ['id' => $id]) {
  48.                 $setIds[] = $id;
  49.             }
  50.             $this->clearCache($setIds);
  51.             return;
  52.         }
  53.     }
  54.     private function clearCache(array $snippetSetIds): void
  55.     {
  56.         $snippetSetIds array_unique($snippetSetIds);
  57.         foreach ($snippetSetIds as $id) {
  58.             $this->cache->deleteItem('translation.catalog.' $id);
  59.         }
  60.     }
  61. }