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

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Translation;
  3. use Doctrine\DBAL\ArrayParameterType;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\System\Snippet\Aggregate\SnippetSet\SnippetSetDefinition;
  9. use Shopware\Core\System\Snippet\SnippetDefinition;
  10. use Shopware\Core\System\Snippet\SnippetEvents;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. /**
  13.  * @internal
  14.  */
  15. #[Package('core')]
  16. class TranslatorCacheInvalidate implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(
  22.         private readonly CacheInvalidator $cacheInvalidator,
  23.         private readonly Connection $connection
  24.     ) {
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             SnippetEvents::SNIPPET_WRITTEN_EVENT => 'invalidate',
  30.             SnippetEvents::SNIPPET_DELETED_EVENT => 'invalidate',
  31.             SnippetEvents::SNIPPET_SET_DELETED_EVENT => 'invalidate',
  32.         ];
  33.     }
  34.     public function invalidate(EntityWrittenEvent $event): void
  35.     {
  36.         if ($event->getEntityName() === SnippetSetDefinition::ENTITY_NAME) {
  37.             $this->clearCache($event->getIds());
  38.             return;
  39.         }
  40.         if ($event->getEntityName() === SnippetDefinition::ENTITY_NAME) {
  41.             $snippetIds $event->getIds();
  42.             $setIds $this->connection->fetchFirstColumn(
  43.                 'SELECT LOWER(HEX(snippet_set_id)) FROM snippet WHERE HEX(id) IN (:ids)',
  44.                 ['ids' => $snippetIds],
  45.                 ['ids' => ArrayParameterType::STRING]
  46.             );
  47.             $this->clearCache($setIds);
  48.         }
  49.     }
  50.     /**
  51.      * @param array<string> $snippetSetIds
  52.      */
  53.     private function clearCache(array $snippetSetIds): void
  54.     {
  55.         $snippetSetIds array_unique($snippetSetIds);
  56.         $snippetSetCacheKeys array_map(fn (string $setId) => 'translation.catalog.' $setId$snippetSetIds);
  57.         $this->cacheInvalidator->invalidate($snippetSetCacheKeystrue);
  58.     }
  59. }