custom/plugins/NetiNextEasyCouponDesigns/src/Subscriber/CacheInvalidationSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCouponDesigns\Subscriber;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Content\Product\SalesChannel\Detail\CachedProductDetailRoute;
  6. use Shopware\Core\Content\Product\SalesChannel\Listing\CachedProductListingRoute;
  7. use Shopware\Core\Content\Product\SalesChannel\Review\CachedProductReviewRoute;
  8. use Shopware\Core\Defaults;
  9. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class CacheInvalidationSubscriber implements EventSubscriberInterface
  15. {
  16.     private Connection       $connection;
  17.     private CacheInvalidator $cacheInvalidator;
  18.     public function __construct(
  19.         Connection       $connection,
  20.         CacheInvalidator $cacheInvalidator
  21.     ) {
  22.         $this->connection       $connection;
  23.         $this->cacheInvalidator $cacheInvalidator;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             'neti_ec_design.written' => 'onInvalidateProductCache',
  29.         ];
  30.     }
  31.     public function onInvalidateProductCache(EntityWrittenEvent $event): void
  32.     {
  33.         $sql '
  34.             SELECT LOWER(HEX(ecp.product_id))
  35.             FROM neti_ec_design ecd
  36.             LEFT JOIN neti_ec_design_product   ecdp ON ecdp.design_id = ecd.id
  37.             LEFT JOIN neti_easy_coupon_product ecp  ON ecp.id         = ecdp.product_id
  38.             WHERE ecd.id IN (?)
  39.               AND ecp.product_id IS NOT NULL
  40.         ';
  41.         /** @var string[] $productIds */
  42.         $productIds $this->connection->fetchFirstColumn(
  43.             $sql,
  44.             [
  45.                 Uuid::fromHexToBytesList($event->getIds()),
  46.             ],
  47.             [
  48.                 Connection::PARAM_STR_ARRAY,
  49.             ]
  50.         );
  51.         if ($productIds !== []) {
  52.             $this->cacheInvalidator->invalidate([
  53.                 'product-suggest-route',
  54.                 'product-search-route',
  55.             ], true);
  56.             /**
  57.              * @psalm-suppress MixedArgumentTypeCoercion The first parameter is of type string[]
  58.              */
  59.             $this->cacheInvalidator->invalidate(
  60.                 array_map(
  61.                     [ CachedProductListingRoute::class, 'buildName' ],
  62.                     $this->getProductCategoryIds($productIds)
  63.                 ),
  64.                 true
  65.             );
  66.             /**
  67.              * @psalm-suppress MixedArgumentTypeCoercion The first parameter is of type string[]
  68.              */
  69.             $this->cacheInvalidator->invalidate(
  70.                 array_map([ EntityCacheKeyGenerator::class, 'buildProductTag' ], $productIds),
  71.                 true
  72.             );
  73.             /**
  74.              * @psalm-suppress MixedArgumentTypeCoercion The first parameter is of type string[]
  75.              */
  76.             $this->cacheInvalidator->invalidate(
  77.                 array_map([ CachedProductDetailRoute::class, 'buildName' ], $productIds),
  78.                 true
  79.             );
  80.             /**
  81.              * @psalm-suppress MixedArgumentTypeCoercion The first parameter is of type string[]
  82.              */
  83.             $this->cacheInvalidator->invalidate(
  84.                 array_map([ CachedProductReviewRoute::class, 'buildName' ], $productIds),
  85.                 true
  86.             );
  87.         }
  88.     }
  89.     private function getProductCategoryIds(array $ids): array
  90.     {
  91.         return $this->connection->fetchFirstColumn(
  92.             'SELECT DISTINCT LOWER(HEX(category_id)) as category_id
  93.              FROM product_category_tree
  94.              WHERE product_id IN (:ids)
  95.              AND product_version_id = :version
  96.              AND category_version_id = :version',
  97.             [ 'ids' => Uuid::fromHexToBytesList($ids), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION) ],
  98.             [ 'ids' => Connection::PARAM_STR_ARRAY ]
  99.         );
  100.     }
  101. }