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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCoupon\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_easy_coupon_product.written' => 'onInvalidateProductCache',
  29.         ];
  30.     }
  31.     public function onInvalidateProductCache(EntityWrittenEvent $event): void
  32.     {
  33.         $sql '
  34.             SELECT LOWER(HEX(product_id))
  35.             FROM neti_easy_coupon_product
  36.             WHERE id IN (?) AND product_id IS NOT NULL
  37.         ';
  38.         $productIds $this->connection->fetchFirstColumn(
  39.             $sql,
  40.             [
  41.                 Uuid::fromHexToBytesList($event->getIds()),
  42.             ],
  43.             [
  44.                 Connection::PARAM_STR_ARRAY,
  45.             ]
  46.         );
  47.         if ($productIds !== []) {
  48.             $this->cacheInvalidator->invalidate([
  49.                 'product-suggest-route',
  50.                 'product-search-route',
  51.             ], true);
  52.             /**
  53.              * @psalm-suppress MixedArgumentTypeCoercion
  54.              * The format is correct
  55.              */
  56.             $this->cacheInvalidator->invalidate(
  57.                 array_map(
  58.                     [ CachedProductListingRoute::class, 'buildName' ],
  59.                     $this->getProductCategoryIds($productIds)
  60.                 ),
  61.                 true
  62.             );
  63.             /**
  64.              * @psalm-suppress ArgumentTypeCoercion
  65.              * The format is correct
  66.              */
  67.             $this->cacheInvalidator->invalidate(
  68.                 array_map([ EntityCacheKeyGenerator::class, 'buildProductTag' ], $productIds),
  69.                 true
  70.             );
  71.             /**
  72.              * @psalm-suppress ArgumentTypeCoercion
  73.              * The format is correct
  74.              */
  75.             $this->cacheInvalidator->invalidate(
  76.                 array_map([ CachedProductDetailRoute::class, 'buildName' ], $productIds),
  77.                 true
  78.             );
  79.             /**
  80.              * @psalm-suppress ArgumentTypeCoercion
  81.              * The format is correct
  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. }