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

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