custom/plugins/MaxiaListingVariants6/src/Subscriber/CacheInvalidationSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Maxia\MaxiaListingVariants6\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Maxia\MaxiaListingVariants6\Service\CachedProductConfiguratorLoader;
  5. use Maxia\MaxiaListingVariants6\Service\CachedVariantMappingLoader;
  6. use Shopware\Core\Content\Product\Events\ProductChangedEventInterface;
  7. use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
  8. use Shopware\Core\Content\Product\ProductDefinition;
  9. use Shopware\Core\Defaults;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  11. use Shopware\Core\Framework\Uuid\Uuid;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  14. class CacheInvalidationSubscriber implements EventSubscriberInterface
  15. {
  16.     private Connection $connection;
  17.     private CacheInvalidator $cacheInvalidator;
  18.     public function __construct(
  19.         CacheInvalidator $cacheInvalidator,
  20.         Connection $connection
  21.     ) {
  22.         $this->cacheInvalidator $cacheInvalidator;
  23.         $this->connection $connection;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             ProductIndexerEvent::class => [
  29.                 ['invalidateOnProductIndex'2000],
  30.             ],
  31.             EntityWrittenContainerEvent::class => [
  32.                 ['invalidateOnEntityWritten'2000]
  33.             ],
  34.         ];
  35.     }
  36.     public function invalidateOnEntityWritten(EntityWrittenContainerEvent $event): void
  37.     {
  38.         // invalidate table after product change
  39.         $productIds $event->getPrimaryKeys(ProductDefinition::ENTITY_NAME);
  40.         if (empty($productIds)) {
  41.             return;
  42.         }
  43.         $parentIds $this->connection->fetchFirstColumn(
  44.             'SELECT DISTINCT LOWER(HEX(parent_id))
  45.             FROM product
  46.             WHERE id IN (:productIds) AND parent_id IS NOT NULL AND version_id = :version',
  47.             ['productIds' => Uuid::fromHexToBytesList($productIds), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  48.             ['productIds' => Connection::PARAM_STR_ARRAY]
  49.         );
  50.         $parentIds array_unique($parentIds);
  51.         if ($parentIds) {
  52.             $this->cacheInvalidator->invalidate(
  53.                 array_merge(
  54.                     array_map([CachedVariantMappingLoader::class, 'buildName'], $productIds),
  55.                     array_map([CachedProductConfiguratorLoader::class, 'buildName'], $parentIds)
  56.                 )
  57.             );
  58.         }
  59.     }
  60.     public function invalidateOnProductIndex(ProductChangedEventInterface $event): void
  61.     {
  62.         $productIds $event->getIds();
  63.         $parentIds $this->connection->fetchFirstColumn(
  64.             'SELECT DISTINCT LOWER(HEX(COALESCE(parent_id, id)))
  65.             FROM product
  66.             WHERE id in (:productIds) AND version_id = :version',
  67.             ['productIds' => $productIds'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  68.             ['productIds' => Connection::PARAM_STR_ARRAY]
  69.         );
  70.         $parentIds array_unique($parentIds);
  71.         if ($parentIds) {
  72.             $this->cacheInvalidator->invalidate(
  73.                 array_merge(
  74.                     array_map([CachedVariantMappingLoader::class, 'buildName'], $productIds),
  75.                     array_map([CachedProductConfiguratorLoader::class, 'buildName'], $parentIds)
  76.                 )
  77.             );
  78.         }
  79.     }
  80. }