vendor/shopware/core/Framework/Adapter/Cache/CacheInvalidationSubscriber.php line 363

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Cache;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Cart\CachedRuleLoader;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupDefinition;
  6. use Shopware\Core\Checkout\Payment\PaymentMethodDefinition;
  7. use Shopware\Core\Checkout\Payment\SalesChannel\CachedPaymentMethodRoute;
  8. use Shopware\Core\Checkout\Shipping\SalesChannel\CachedShippingMethodRoute;
  9. use Shopware\Core\Checkout\Shipping\ShippingMethodDefinition;
  10. use Shopware\Core\Content\Category\CategoryDefinition;
  11. use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
  12. use Shopware\Core\Content\Category\SalesChannel\CachedCategoryRoute;
  13. use Shopware\Core\Content\Category\SalesChannel\CachedNavigationRoute;
  14. use Shopware\Core\Content\Cms\CmsPageDefinition;
  15. use Shopware\Core\Content\LandingPage\Event\LandingPageIndexerEvent;
  16. use Shopware\Core\Content\LandingPage\SalesChannel\CachedLandingPageRoute;
  17. use Shopware\Core\Content\Product\Aggregate\ProductCategory\ProductCategoryDefinition;
  18. use Shopware\Core\Content\Product\Aggregate\ProductCrossSelling\ProductCrossSellingDefinition;
  19. use Shopware\Core\Content\Product\Aggregate\ProductManufacturer\ProductManufacturerDefinition;
  20. use Shopware\Core\Content\Product\Aggregate\ProductProperty\ProductPropertyDefinition;
  21. use Shopware\Core\Content\Product\Events\ProductChangedEventInterface;
  22. use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
  23. use Shopware\Core\Content\Product\Events\ProductNoLongerAvailableEvent;
  24. use Shopware\Core\Content\Product\ProductDefinition;
  25. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\CachedProductCrossSellingRoute;
  26. use Shopware\Core\Content\Product\SalesChannel\Detail\CachedProductDetailRoute;
  27. use Shopware\Core\Content\Product\SalesChannel\Listing\CachedProductListingRoute;
  28. use Shopware\Core\Content\Product\SalesChannel\Review\CachedProductReviewRoute;
  29. use Shopware\Core\Content\ProductStream\ProductStreamDefinition;
  30. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionDefinition;
  31. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOptionTranslation\PropertyGroupOptionTranslationDefinition;
  32. use Shopware\Core\Content\Property\Aggregate\PropertyGroupTranslation\PropertyGroupTranslationDefinition;
  33. use Shopware\Core\Content\Property\PropertyGroupDefinition;
  34. use Shopware\Core\Content\Rule\Event\RuleIndexerEvent;
  35. use Shopware\Core\Content\Seo\CachedSeoResolver;
  36. use Shopware\Core\Content\Seo\Event\SeoUrlUpdateEvent;
  37. use Shopware\Core\Content\Sitemap\Event\SitemapGeneratedEvent;
  38. use Shopware\Core\Content\Sitemap\SalesChannel\CachedSitemapRoute;
  39. use Shopware\Core\Defaults;
  40. use Shopware\Core\Framework\Adapter\Translation\Translator;
  41. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  42. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  43. use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
  44. use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
  45. use Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent;
  46. use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
  47. use Shopware\Core\Framework\Plugin\Event\PluginPostUpdateEvent;
  48. use Shopware\Core\Framework\Uuid\Uuid;
  49. use Shopware\Core\System\Country\CountryDefinition;
  50. use Shopware\Core\System\Country\SalesChannel\CachedCountryRoute;
  51. use Shopware\Core\System\Currency\CurrencyDefinition;
  52. use Shopware\Core\System\Currency\SalesChannel\CachedCurrencyRoute;
  53. use Shopware\Core\System\Language\LanguageDefinition;
  54. use Shopware\Core\System\Language\SalesChannel\CachedLanguageRoute;
  55. use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelCountry\SalesChannelCountryDefinition;
  56. use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelCurrency\SalesChannelCurrencyDefinition;
  57. use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelLanguage\SalesChannelLanguageDefinition;
  58. use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelPaymentMethod\SalesChannelPaymentMethodDefinition;
  59. use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelShippingMethod\SalesChannelShippingMethodDefinition;
  60. use Shopware\Core\System\SalesChannel\Context\CachedSalesChannelContextFactory;
  61. use Shopware\Core\System\SalesChannel\SalesChannelDefinition;
  62. use Shopware\Core\System\Salutation\SalesChannel\CachedSalutationRoute;
  63. use Shopware\Core\System\Salutation\SalutationDefinition;
  64. use Shopware\Core\System\Snippet\SnippetDefinition;
  65. use Shopware\Core\System\SystemConfig\CachedSystemConfigLoader;
  66. use Shopware\Core\System\SystemConfig\Event\SystemConfigChangedEvent;
  67. use Shopware\Core\System\SystemConfig\SystemConfigService;
  68. use Shopware\Core\System\Tax\TaxDefinition;
  69. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  70. /**
  71.  * @internal - The functions inside this class are no public-api and can be changed without previous deprecation
  72.  */
  73. class CacheInvalidationSubscriber implements EventSubscriberInterface
  74. {
  75.     private Connection $connection;
  76.     private CacheInvalidator $cacheInvalidator;
  77.     public function __construct(
  78.         CacheInvalidator $cacheInvalidator,
  79.         Connection $connection
  80.     ) {
  81.         $this->cacheInvalidator $cacheInvalidator;
  82.         $this->connection $connection;
  83.     }
  84.     /**
  85.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  86.      */
  87.     public static function getSubscribedEvents()
  88.     {
  89.         return [
  90.             CategoryIndexerEvent::class => [
  91.                 ['invalidateCategoryRouteByCategoryIds'2000],
  92.                 ['invalidateListingRouteByCategoryIds'2001],
  93.             ],
  94.             LandingPageIndexerEvent::class => [
  95.                 ['invalidateIndexedLandingPages'2000],
  96.             ],
  97.             ProductIndexerEvent::class => [
  98.                 ['invalidateSearch'2000],
  99.                 ['invalidateListings'2001],
  100.                 ['invalidateProductIds'2002],
  101.                 ['invalidateDetailRoute'2004],
  102.                 ['invalidateStreamsAfterIndexing'2005],
  103.                 ['invalidateReviewRoute'2006],
  104.             ],
  105.             ProductNoLongerAvailableEvent::class => [
  106.                 ['invalidateSearch'2000],
  107.                 ['invalidateListings'2001],
  108.                 ['invalidateProductIds'2002],
  109.                 ['invalidateDetailRoute'2004],
  110.                 ['invalidateStreamsAfterIndexing'2005],
  111.                 ['invalidateReviewRoute'2006],
  112.             ],
  113.             EntityWrittenContainerEvent::class => [
  114.                 ['invalidateCmsPageIds'2001],
  115.                 ['invalidateCurrencyRoute'2002],
  116.                 ['invalidateLanguageRoute'2003],
  117.                 ['invalidateNavigationRoute'2004],
  118.                 ['invalidatePaymentMethodRoute'2005],
  119.                 ['invalidateProductAssignment'2006],
  120.                 ['invalidateManufacturerFilters'2007],
  121.                 ['invalidatePropertyFilters'2008],
  122.                 ['invalidateCrossSellingRoute'2009],
  123.                 ['invalidateContext'2010],
  124.                 ['invalidateShippingMethodRoute'2011],
  125.                 ['invalidateSnippets'2012],
  126.                 ['invalidateStreamsBeforeIndexing'2013],
  127.                 ['invalidateStreamIds'2014],
  128.                 ['invalidateCountryRoute'2015],
  129.                 ['invalidateSalutationRoute'2016],
  130.             ],
  131.             SeoUrlUpdateEvent::class => [
  132.                 ['invalidateSeoUrls'2000],
  133.             ],
  134.             RuleIndexerEvent::class => [
  135.                 ['invalidateRules'2000],
  136.             ],
  137.             PluginPostInstallEvent::class => [
  138.                 ['invalidateRules'2000],
  139.                 ['invalidateConfig'2001],
  140.             ],
  141.             PluginPostActivateEvent::class => [
  142.                 ['invalidateRules'2000],
  143.                 ['invalidateConfig'2001],
  144.             ],
  145.             PluginPostUpdateEvent::class => [
  146.                 ['invalidateRules'2000],
  147.                 ['invalidateConfig'2001],
  148.             ],
  149.             PluginPostDeactivateEvent::class => [
  150.                 ['invalidateRules'2000],
  151.                 ['invalidateConfig'2001],
  152.             ],
  153.             PluginPostUninstallEvent::class => [
  154.                 ['invalidateRules'2000],
  155.                 ['invalidateConfig'2001],
  156.             ],
  157.             SystemConfigChangedEvent::class => [
  158.                 ['invalidateConfigKey'2000],
  159.             ],
  160.             SitemapGeneratedEvent::class => [
  161.                 ['invalidateSitemap'2000],
  162.             ],
  163.         ];
  164.     }
  165.     public function invalidateSitemap(SitemapGeneratedEvent $event): void
  166.     {
  167.         $this->cacheInvalidator->invalidate([
  168.             CachedSitemapRoute::buildName($event->getSalesChannelContext()->getSalesChannelId()),
  169.         ]);
  170.     }
  171.     public function invalidateConfig(): void
  172.     {
  173.         // invalidates the complete cached config
  174.         $this->cacheInvalidator->invalidate([
  175.             CachedSystemConfigLoader::CACHE_TAG,
  176.         ]);
  177.     }
  178.     public function invalidateConfigKey(SystemConfigChangedEvent $event): void
  179.     {
  180.         // invalidates the complete cached config and routes which access a specific key
  181.         $this->cacheInvalidator->invalidate([
  182.             SystemConfigService::buildName($event->getKey()),
  183.             CachedSystemConfigLoader::CACHE_TAG,
  184.         ]);
  185.     }
  186.     public function invalidateSnippets(EntityWrittenContainerEvent $event): void
  187.     {
  188.         // invalidates all http cache items where the snippets used
  189.         $snippets $event->getEventByEntityName(SnippetDefinition::ENTITY_NAME);
  190.         if (!$snippets) {
  191.             return;
  192.         }
  193.         $tags = [];
  194.         foreach ($snippets->getPayloads() as $payload) {
  195.             if (isset($payload['translationKey'])) {
  196.                 $tags[] = Translator::buildName($payload['translationKey']);
  197.             }
  198.         }
  199.         $this->cacheInvalidator->invalidate($tags);
  200.     }
  201.     public function invalidateShippingMethodRoute(EntityWrittenContainerEvent $event): void
  202.     {
  203.         // checks if a shipping method changed or the assignment between shipping method and sales channel
  204.         $logs array_merge(
  205.             $this->getChangedShippingMethods($event),
  206.             $this->getChangedShippingAssignments($event)
  207.         );
  208.         $this->cacheInvalidator->invalidate($logs);
  209.     }
  210.     public function invalidateSeoUrls(SeoUrlUpdateEvent $event): void
  211.     {
  212.         // invalidates the cache for the seo url resolver based on the path infos which used for the new seo urls
  213.         $urls $event->getSeoUrls();
  214.         $pathInfo array_column($urls'pathInfo');
  215.         $this->cacheInvalidator->invalidate(array_map([CachedSeoResolver::class, 'buildName'], $pathInfo));
  216.     }
  217.     public function invalidateRules(): void
  218.     {
  219.         // invalidates the rule loader each time a rule changed or a plugin install state changed
  220.         $this->cacheInvalidator->invalidate([CachedRuleLoader::CACHE_KEY]);
  221.     }
  222.     public function invalidateCmsPageIds(EntityWrittenContainerEvent $event): void
  223.     {
  224.         // invalidates all routes and http cache pages where a cms page was loaded, the id is assigned as tag
  225.         $this->cacheInvalidator->invalidate(
  226.             array_map([EntityCacheKeyGenerator::class, 'buildCmsTag'], $event->getPrimaryKeys(CmsPageDefinition::ENTITY_NAME))
  227.         );
  228.     }
  229.     public function invalidateProductIds(ProductChangedEventInterface $event): void
  230.     {
  231.         // invalidates all routes which loads products in nested unknown objects, like cms listing elements or cross selling elements
  232.         $this->cacheInvalidator->invalidate(
  233.             array_map([EntityCacheKeyGenerator::class, 'buildProductTag'], $event->getIds())
  234.         );
  235.     }
  236.     public function invalidateStreamIds(EntityWrittenContainerEvent $event): void
  237.     {
  238.         // invalidates all routes which are loaded based on a stream (e.G. category listing and cross selling)
  239.         $this->cacheInvalidator->invalidate(
  240.             array_map([EntityCacheKeyGenerator::class, 'buildStreamTag'], $event->getPrimaryKeys(ProductStreamDefinition::ENTITY_NAME))
  241.         );
  242.     }
  243.     public function invalidateCategoryRouteByCategoryIds(CategoryIndexerEvent $event): void
  244.     {
  245.         // invalidates the category route cache when a category changed
  246.         $this->cacheInvalidator->invalidate(
  247.             array_map([CachedCategoryRoute::class, 'buildName'], $event->getIds())
  248.         );
  249.     }
  250.     public function invalidateListingRouteByCategoryIds(CategoryIndexerEvent $event): void
  251.     {
  252.         // invalidates the product listing route each time a category changed
  253.         $this->cacheInvalidator->invalidate(
  254.             array_map([CachedProductListingRoute::class, 'buildName'], $event->getIds())
  255.         );
  256.     }
  257.     public function invalidateIndexedLandingPages(LandingPageIndexerEvent $event): void
  258.     {
  259.         // invalidates the landing page route, if the corresponding landing page changed
  260.         $this->cacheInvalidator->invalidate(
  261.             array_map([CachedLandingPageRoute::class, 'buildName'], $event->getIds())
  262.         );
  263.     }
  264.     public function invalidateCurrencyRoute(EntityWrittenContainerEvent $event): void
  265.     {
  266.         // invalidates the currency route when a currency changed or an assignment between the sales channel and currency changed
  267.         $this->cacheInvalidator->invalidate(array_merge(
  268.             $this->getChangedCurrencyAssignments($event),
  269.             $this->getChangedCurrencies($event)
  270.         ));
  271.     }
  272.     public function invalidateLanguageRoute(EntityWrittenContainerEvent $event): void
  273.     {
  274.         // invalidates the language route when a language changed or an assignment between the sales channel and language changed
  275.         $this->cacheInvalidator->invalidate(array_merge(
  276.             $this->getChangedLanguageAssignments($event),
  277.             $this->getChangedLanguages($event)
  278.         ));
  279.     }
  280.     public function invalidateCountryRoute(EntityWrittenContainerEvent $event): void
  281.     {
  282.         // invalidates the country route when a country changed or an assignment between the sales channel and country changed
  283.         $this->cacheInvalidator->invalidate(array_merge(
  284.             $this->getChangedCountryAssignments($event),
  285.             $this->getChangedCountries($event),
  286.         ));
  287.     }
  288.     public function invalidateSalutationRoute(EntityWrittenContainerEvent $event): void
  289.     {
  290.         // invalidates the salutation route when a salutation changed
  291.         $this->cacheInvalidator->invalidate(array_merge(
  292.             $this->getChangedSalutations($event),
  293.         ));
  294.     }
  295.     public function invalidateNavigationRoute(EntityWrittenContainerEvent $event): void
  296.     {
  297.         // invalidates the navigation route when a category changed or the entry point configuration of an sales channel changed
  298.         $logs array_merge(
  299.             $this->getChangedCategories($event),
  300.             $this->getChangedEntryPoints($event)
  301.         );
  302.         $this->cacheInvalidator->invalidate($logs);
  303.     }
  304.     public function invalidatePaymentMethodRoute(EntityWrittenContainerEvent $event): void
  305.     {
  306.         // invalidates the payment method route when a payment method changed or an assignment between the sales channel and payment method changed
  307.         $logs array_merge(
  308.             $this->getChangedPaymentMethods($event),
  309.             $this->getChangedPaymentAssignments($event)
  310.         );
  311.         $this->cacheInvalidator->invalidate($logs);
  312.     }
  313.     public function invalidateSearch(): void
  314.     {
  315.         // invalidates the search and suggest route each time a product changed
  316.         $this->cacheInvalidator->invalidate([
  317.             'product-suggest-route',
  318.             'product-search-route',
  319.         ]);
  320.     }
  321.     public function invalidateDetailRoute(ProductChangedEventInterface $event): void
  322.     {
  323.         //invalidates the product detail route each time a product changed or if the product is no longer available (because out of stock)
  324.         $this->cacheInvalidator->invalidate(
  325.             array_map([CachedProductDetailRoute::class, 'buildName'], $event->getIds())
  326.         );
  327.     }
  328.     public function invalidateProductAssignment(EntityWrittenContainerEvent $event): void
  329.     {
  330.         //invalidates the product listing route, each time a product - category assignment changed
  331.         $ids $event->getPrimaryKeys(ProductCategoryDefinition::ENTITY_NAME);
  332.         $ids array_column($ids'categoryId');
  333.         $this->cacheInvalidator->invalidate(
  334.             array_map([CachedProductListingRoute::class, 'buildName'], $ids)
  335.         );
  336.     }
  337.     public function invalidateContext(EntityWrittenContainerEvent $event): void
  338.     {
  339.         //invalidates the context cache - each time one of the entities which are considered inside the context factory changed
  340.         $ids $event->getPrimaryKeys(SalesChannelDefinition::ENTITY_NAME);
  341.         $keys array_map([CachedSalesChannelContextFactory::class, 'buildName'], $ids);
  342.         if ($event->getEventByEntityName(CurrencyDefinition::ENTITY_NAME)) {
  343.             $keys[] = CachedSalesChannelContextFactory::ALL_TAG;
  344.         }
  345.         if ($event->getEventByEntityName(PaymentMethodDefinition::ENTITY_NAME)) {
  346.             $keys[] = CachedSalesChannelContextFactory::ALL_TAG;
  347.         }
  348.         if ($event->getEventByEntityName(ShippingMethodDefinition::ENTITY_NAME)) {
  349.             $keys[] = CachedSalesChannelContextFactory::ALL_TAG;
  350.         }
  351.         if ($event->getEventByEntityName(TaxDefinition::ENTITY_NAME)) {
  352.             $keys[] = CachedSalesChannelContextFactory::ALL_TAG;
  353.         }
  354.         if ($event->getEventByEntityName(CountryDefinition::ENTITY_NAME)) {
  355.             $keys[] = CachedSalesChannelContextFactory::ALL_TAG;
  356.         }
  357.         if ($event->getEventByEntityName(CustomerGroupDefinition::ENTITY_NAME)) {
  358.             $keys[] = CachedSalesChannelContextFactory::ALL_TAG;
  359.         }
  360.         if ($event->getEventByEntityName(LanguageDefinition::ENTITY_NAME)) {
  361.             $keys[] = CachedSalesChannelContextFactory::ALL_TAG;
  362.         }
  363.         $keys array_filter(array_unique($keys));
  364.         if (empty($keys)) {
  365.             return;
  366.         }
  367.         $this->cacheInvalidator->invalidate($keys);
  368.     }
  369.     public function invalidateManufacturerFilters(EntityWrittenContainerEvent $event): void
  370.     {
  371.         // invalidates the product listing route, each time a manufacturer changed
  372.         $ids $event->getPrimaryKeys(ProductManufacturerDefinition::ENTITY_NAME);
  373.         if (empty($ids)) {
  374.             return;
  375.         }
  376.         $ids $this->connection->fetchFirstColumn(
  377.             'SELECT DISTINCT LOWER(HEX(category_id)) as category_id
  378.              FROM product_category_tree
  379.                 INNER JOIN product ON product.id = product_category_tree.product_id AND product_category_tree.product_version_id = product.version_id
  380.              WHERE product.product_manufacturer_id IN (:ids)
  381.              AND product.version_id = :version',
  382.             ['ids' => Uuid::fromHexToBytesList($ids), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  383.             ['ids' => Connection::PARAM_STR_ARRAY]
  384.         );
  385.         $this->cacheInvalidator->invalidate(
  386.             array_map([CachedProductListingRoute::class, 'buildName'], $ids)
  387.         );
  388.     }
  389.     public function invalidatePropertyFilters(EntityWrittenContainerEvent $event): void
  390.     {
  391.         $this->cacheInvalidator->invalidate(array_merge(
  392.             $this->getChangedPropertyFilterTags($event),
  393.             $this->getDeletedPropertyFilterTags($event)
  394.         ));
  395.     }
  396.     public function invalidateReviewRoute(ProductChangedEventInterface $event): void
  397.     {
  398.         $this->cacheInvalidator->invalidate(
  399.             array_map([CachedProductReviewRoute::class, 'buildName'], $event->getIds())
  400.         );
  401.     }
  402.     public function invalidateListings(ProductChangedEventInterface $event): void
  403.     {
  404.         // invalidates product listings which are based on the product category assignment
  405.         $this->cacheInvalidator->invalidate(
  406.             array_map([CachedProductListingRoute::class, 'buildName'], $this->getProductCategoryIds($event->getIds()))
  407.         );
  408.     }
  409.     public function invalidateStreamsBeforeIndexing(EntityWrittenContainerEvent $event): void
  410.     {
  411.         // invalidates all stream based pages and routes before the product indexer changes product_stream_mapping
  412.         $ids $event->getPrimaryKeys(ProductDefinition::ENTITY_NAME);
  413.         if (empty($ids)) {
  414.             return;
  415.         }
  416.         // invalidates product listings which are based on a product stream
  417.         $ids $this->connection->fetchFirstColumn(
  418.             'SELECT DISTINCT LOWER(HEX(product_stream_id))
  419.              FROM product_stream_mapping
  420.              WHERE product_stream_mapping.product_id IN (:ids)
  421.              AND product_stream_mapping.product_version_id = :version',
  422.             ['ids' => Uuid::fromHexToBytesList($ids), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  423.             ['ids' => Connection::PARAM_STR_ARRAY]
  424.         );
  425.         $this->cacheInvalidator->invalidate(
  426.             array_map([EntityCacheKeyGenerator::class, 'buildStreamTag'], $ids)
  427.         );
  428.     }
  429.     public function invalidateStreamsAfterIndexing(ProductChangedEventInterface $event): void
  430.     {
  431.         // invalidates all stream based pages and routes after the product indexer changes product_stream_mapping
  432.         $ids $this->connection->fetchFirstColumn(
  433.             'SELECT DISTINCT LOWER(HEX(product_stream_id))
  434.              FROM product_stream_mapping
  435.              WHERE product_stream_mapping.product_id IN (:ids)
  436.              AND product_stream_mapping.product_version_id = :version',
  437.             ['ids' => Uuid::fromHexToBytesList($event->getIds()), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  438.             ['ids' => Connection::PARAM_STR_ARRAY]
  439.         );
  440.         $this->cacheInvalidator->invalidate(
  441.             array_map([EntityCacheKeyGenerator::class, 'buildStreamTag'], $ids)
  442.         );
  443.     }
  444.     public function invalidateCrossSellingRoute(EntityWrittenContainerEvent $event): void
  445.     {
  446.         // invalidates the product detail route for the changed cross selling definitions
  447.         $ids $event->getPrimaryKeys(ProductCrossSellingDefinition::ENTITY_NAME);
  448.         if (empty($ids)) {
  449.             return;
  450.         }
  451.         $ids $this->connection->fetchFirstColumn(
  452.             'SELECT DISTINCT LOWER(HEX(product_id)) FROM product_cross_selling WHERE id IN (:ids)',
  453.             ['ids' => Uuid::fromHexToBytesList($ids)],
  454.             ['ids' => Connection::PARAM_STR_ARRAY]
  455.         );
  456.         $this->cacheInvalidator->invalidate(
  457.             array_map([CachedProductCrossSellingRoute::class, 'buildName'], $ids)
  458.         );
  459.     }
  460.     private function getDeletedPropertyFilterTags(EntityWrittenContainerEvent $event): array
  461.     {
  462.         // invalidates the product listing route, each time a property changed
  463.         $ids $event->getDeletedPrimaryKeys(ProductPropertyDefinition::ENTITY_NAME);
  464.         if (empty($ids)) {
  465.             return [];
  466.         }
  467.         $productIds array_column($ids'productId');
  468.         return array_merge(
  469.             array_map([CachedProductDetailRoute::class, 'buildName'], array_unique($productIds)),
  470.             array_map([CachedProductListingRoute::class, 'buildName'], $this->getProductCategoryIds($productIds))
  471.         );
  472.     }
  473.     private function getChangedPropertyFilterTags(EntityWrittenContainerEvent $event): array
  474.     {
  475.         // invalidates the product listing route and detail rule, each time a property group changed
  476.         $propertyGroupIds array_unique(array_merge(
  477.             $event->getPrimaryKeysWithPayloadIgnoringFields(PropertyGroupDefinition::ENTITY_NAME, ['id''updatedAt']),
  478.             array_column($event->getPrimaryKeysWithPayloadIgnoringFields(PropertyGroupTranslationDefinition::ENTITY_NAME, ['propertyGroupId''languageId''updatedAt']), 'propertyGroupId')
  479.         ));
  480.         // invalidates the product listing route and detail rule, each time a property option changed
  481.         $propertyOptionIds array_unique(array_merge(
  482.             $event->getPrimaryKeysWithPayloadIgnoringFields(PropertyGroupOptionDefinition::ENTITY_NAME, ['id''updatedAt']),
  483.             array_column($event->getPrimaryKeysWithPayloadIgnoringFields(PropertyGroupOptionTranslationDefinition::ENTITY_NAME, ['propertyGroupOptionId''languageId''updatedAt']), 'propertyGroupOptionId')
  484.         ));
  485.         if (empty($propertyGroupIds) && empty($propertyOptionIds)) {
  486.             return [];
  487.         }
  488.         $productIds $this->connection->fetchFirstColumn(
  489.             'SELECT product_property.product_id
  490.              FROM product_property
  491.                 LEFT JOIN property_group_option productProperties ON productProperties.id = product_property.property_group_option_id
  492.              WHERE productProperties.property_group_id IN (:ids) OR productProperties.id IN (:optionIds)
  493.              AND product_property.product_version_id = :version',
  494.             ['ids' => Uuid::fromHexToBytesList($propertyGroupIds), 'optionIds' => Uuid::fromHexToBytesList($propertyOptionIds), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  495.             ['ids' => Connection::PARAM_STR_ARRAY'optionIds' => Connection::PARAM_STR_ARRAY]
  496.         );
  497.         $productIds array_unique(array_merge(
  498.             $productIds,
  499.             $this->connection->fetchFirstColumn(
  500.                 'SELECT product_option.product_id
  501.                  FROM product_option
  502.                     LEFT JOIN property_group_option productOptions ON productOptions.id = product_option.property_group_option_id
  503.                  WHERE productOptions.property_group_id IN (:ids) OR productOptions.id IN (:optionIds)
  504.                  AND product_option.product_version_id = :version',
  505.                 ['ids' => Uuid::fromHexToBytesList($propertyGroupIds), 'optionIds' => Uuid::fromHexToBytesList($propertyOptionIds), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  506.                 ['ids' => Connection::PARAM_STR_ARRAY'optionIds' => Connection::PARAM_STR_ARRAY]
  507.             )
  508.         ));
  509.         if (empty($productIds)) {
  510.             return [];
  511.         }
  512.         $parentIds $this->connection->fetchFirstColumn(
  513.             'SELECT DISTINCT LOWER(HEX(COALESCE(parent_id, id)))
  514.             FROM product
  515.             WHERE id in (:productIds) AND version_id = :version',
  516.             ['productIds' => $productIds'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  517.             ['productIds' => Connection::PARAM_STR_ARRAY]
  518.         );
  519.         $categoryIds $this->connection->fetchFirstColumn(
  520.             'SELECT DISTINCT LOWER(HEX(category_id))
  521.             FROM product_category_tree
  522.             WHERE product_id in (:productIds) AND product_version_id = :version',
  523.             ['productIds' => $productIds'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  524.             ['productIds' => Connection::PARAM_STR_ARRAY]
  525.         );
  526.         return array_merge(
  527.             array_map([CachedProductDetailRoute::class, 'buildName'], array_filter($parentIds)),
  528.             array_map([CachedProductListingRoute::class, 'buildName'], array_filter($categoryIds)),
  529.         );
  530.     }
  531.     private function getProductCategoryIds(array $ids): array
  532.     {
  533.         return $this->connection->fetchFirstColumn(
  534.             'SELECT DISTINCT LOWER(HEX(category_id)) as category_id
  535.              FROM product_category_tree
  536.              WHERE product_id IN (:ids)
  537.              AND product_version_id = :version
  538.              AND category_version_id = :version',
  539.             ['ids' => Uuid::fromHexToBytesList($ids), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  540.             ['ids' => Connection::PARAM_STR_ARRAY]
  541.         );
  542.     }
  543.     private function getChangedShippingMethods(EntityWrittenContainerEvent $event): array
  544.     {
  545.         $ids $event->getPrimaryKeys(ShippingMethodDefinition::ENTITY_NAME);
  546.         if (empty($ids)) {
  547.             return [];
  548.         }
  549.         $ids $this->connection->fetchFirstColumn(
  550.             'SELECT DISTINCT LOWER(HEX(sales_channel_id)) as id FROM sales_channel_shipping_method WHERE shipping_method_id IN (:ids)',
  551.             ['ids' => Uuid::fromHexToBytesList($ids)],
  552.             ['ids' => Connection::PARAM_STR_ARRAY]
  553.         );
  554.         $tags = [];
  555.         if ($event->getDeletedPrimaryKeys(ShippingMethodDefinition::ENTITY_NAME)) {
  556.             $tags[] = CachedShippingMethodRoute::ALL_TAG;
  557.         }
  558.         return array_merge($tagsarray_map([CachedShippingMethodRoute::class, 'buildName'], $ids));
  559.     }
  560.     private function getChangedShippingAssignments(EntityWrittenContainerEvent $event): array
  561.     {
  562.         //Used to detect changes to the shipping assignment of a sales channel
  563.         $ids $event->getPrimaryKeys(SalesChannelShippingMethodDefinition::ENTITY_NAME);
  564.         $ids array_column($ids'salesChannelId');
  565.         return array_map([CachedShippingMethodRoute::class, 'buildName'], $ids);
  566.     }
  567.     private function getChangedPaymentMethods(EntityWrittenContainerEvent $event): array
  568.     {
  569.         $ids $event->getPrimaryKeys(PaymentMethodDefinition::ENTITY_NAME);
  570.         if (empty($ids)) {
  571.             return [];
  572.         }
  573.         $ids $this->connection->fetchFirstColumn(
  574.             'SELECT DISTINCT LOWER(HEX(sales_channel_id)) as id FROM sales_channel_payment_method WHERE payment_method_id IN (:ids)',
  575.             ['ids' => Uuid::fromHexToBytesList($ids)],
  576.             ['ids' => Connection::PARAM_STR_ARRAY]
  577.         );
  578.         $tags = [];
  579.         if ($event->getDeletedPrimaryKeys(PaymentMethodDefinition::ENTITY_NAME)) {
  580.             $tags[] = CachedPaymentMethodRoute::ALL_TAG;
  581.         }
  582.         return array_merge($tagsarray_map([CachedPaymentMethodRoute::class, 'buildName'], $ids));
  583.     }
  584.     private function getChangedPaymentAssignments(EntityWrittenContainerEvent $event): array
  585.     {
  586.         //Used to detect changes to the language assignment of a sales channel
  587.         $ids $event->getPrimaryKeys(SalesChannelPaymentMethodDefinition::ENTITY_NAME);
  588.         $ids array_column($ids'salesChannelId');
  589.         return array_map([CachedPaymentMethodRoute::class, 'buildName'], $ids);
  590.     }
  591.     private function getChangedCategories(EntityWrittenContainerEvent $event): array
  592.     {
  593.         $ids $event->getPrimaryKeysWithPayload(CategoryDefinition::ENTITY_NAME);
  594.         if (empty($ids)) {
  595.             return [];
  596.         }
  597.         $ids array_map([CachedNavigationRoute::class, 'buildName'], $ids);
  598.         $ids[] = CachedNavigationRoute::BASE_NAVIGATION_TAG;
  599.         return $ids;
  600.     }
  601.     private function getChangedEntryPoints(EntityWrittenContainerEvent $event): array
  602.     {
  603.         $ids $event->getPrimaryKeysWithPropertyChange(
  604.             SalesChannelDefinition::ENTITY_NAME,
  605.             ['navigationCategoryId''navigationCategoryDepth''serviceCategoryId''footerCategoryId']
  606.         );
  607.         if (empty($ids)) {
  608.             return [];
  609.         }
  610.         return [CachedNavigationRoute::ALL_TAG];
  611.     }
  612.     private function getChangedCountries(EntityWrittenContainerEvent $event): array
  613.     {
  614.         $ids $event->getPrimaryKeys(CountryDefinition::ENTITY_NAME);
  615.         if (empty($ids)) {
  616.             return [];
  617.         }
  618.         //Used to detect changes to the country itself and invalidate the route for all sales channels in which the country is assigned.
  619.         $ids $this->connection->fetchFirstColumn(
  620.             'SELECT DISTINCT LOWER(HEX(sales_channel_id)) as id FROM sales_channel_country WHERE country_id IN (:ids)',
  621.             ['ids' => Uuid::fromHexToBytesList($ids)],
  622.             ['ids' => Connection::PARAM_STR_ARRAY]
  623.         );
  624.         $tags = [];
  625.         if ($event->getDeletedPrimaryKeys(CountryDefinition::ENTITY_NAME)) {
  626.             $tags[] = CachedCountryRoute::ALL_TAG;
  627.         }
  628.         return array_merge($tagsarray_map([CachedCountryRoute::class, 'buildName'], $ids));
  629.     }
  630.     private function getChangedCountryAssignments(EntityWrittenContainerEvent $event): array
  631.     {
  632.         //Used to detect changes to the country assignment of a sales channel
  633.         $ids $event->getPrimaryKeys(SalesChannelCountryDefinition::ENTITY_NAME);
  634.         $ids array_column($ids'salesChannelId');
  635.         return array_map([CachedCountryRoute::class, 'buildName'], $ids);
  636.     }
  637.     private function getChangedSalutations(EntityWrittenContainerEvent $event): array
  638.     {
  639.         $ids $event->getPrimaryKeys(SalutationDefinition::ENTITY_NAME);
  640.         if (empty($ids)) {
  641.             return [];
  642.         }
  643.         return [CachedSalutationRoute::ALL_TAG];
  644.     }
  645.     private function getChangedLanguages(EntityWrittenContainerEvent $event): array
  646.     {
  647.         $ids $event->getPrimaryKeys(LanguageDefinition::ENTITY_NAME);
  648.         if (empty($ids)) {
  649.             return [];
  650.         }
  651.         //Used to detect changes to the language itself and invalidate the route for all sales channels in which the language is assigned.
  652.         $ids $this->connection->fetchFirstColumn(
  653.             'SELECT DISTINCT LOWER(HEX(sales_channel_id)) as id FROM sales_channel_language WHERE language_id IN (:ids)',
  654.             ['ids' => Uuid::fromHexToBytesList($ids)],
  655.             ['ids' => Connection::PARAM_STR_ARRAY]
  656.         );
  657.         $tags = [];
  658.         if ($event->getDeletedPrimaryKeys(LanguageDefinition::ENTITY_NAME)) {
  659.             $tags[] = CachedLanguageRoute::ALL_TAG;
  660.         }
  661.         return array_merge($tagsarray_map([CachedLanguageRoute::class, 'buildName'], $ids));
  662.     }
  663.     private function getChangedLanguageAssignments(EntityWrittenContainerEvent $event): array
  664.     {
  665.         //Used to detect changes to the language assignment of a sales channel
  666.         $ids $event->getPrimaryKeys(SalesChannelLanguageDefinition::ENTITY_NAME);
  667.         $ids array_column($ids'salesChannelId');
  668.         return array_map([CachedLanguageRoute::class, 'buildName'], $ids);
  669.     }
  670.     private function getChangedCurrencies(EntityWrittenContainerEvent $event): array
  671.     {
  672.         $ids $event->getPrimaryKeys(CurrencyDefinition::ENTITY_NAME);
  673.         if (empty($ids)) {
  674.             return [];
  675.         }
  676.         //Used to detect changes to the currency itself and invalidate the route for all sales channels in which the currency is assigned.
  677.         $ids $this->connection->fetchFirstColumn(
  678.             'SELECT DISTINCT LOWER(HEX(sales_channel_id)) as id FROM sales_channel_currency WHERE currency_id IN (:ids)',
  679.             ['ids' => Uuid::fromHexToBytesList($ids)],
  680.             ['ids' => Connection::PARAM_STR_ARRAY]
  681.         );
  682.         $tags = [];
  683.         if ($event->getDeletedPrimaryKeys(CurrencyDefinition::ENTITY_NAME)) {
  684.             $tags[] = CachedCurrencyRoute::ALL_TAG;
  685.         }
  686.         return array_merge($tagsarray_map([CachedCurrencyRoute::class, 'buildName'], $ids));
  687.     }
  688.     private function getChangedCurrencyAssignments(EntityWrittenContainerEvent $event): array
  689.     {
  690.         //Used to detect changes to the currency assignment of a sales channel
  691.         $ids $event->getPrimaryKeys(SalesChannelCurrencyDefinition::ENTITY_NAME);
  692.         $ids array_column($ids'salesChannelId');
  693.         return array_map([CachedCurrencyRoute::class, 'buildName'], $ids);
  694.     }
  695. }