custom/plugins/VioCustomerPrice/src/Subscriber/CacheSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioCustomerPrice\Subscriber;
  4. use Shopware\Core\Content\Product\SalesChannel\Detail\CachedProductDetailRoute;
  5. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\TermsAggregation;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\TermsResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CacheSubscriber implements EventSubscriberInterface
  13. {
  14.     private EntityRepositoryInterface $customerPriceRepository;
  15.     private EntityRepositoryInterface $productRepository;
  16.     private CacheInvalidator $cacheInvalidator;
  17.     public function __construct(
  18.         EntityRepositoryInterface $customerPriceRepository,
  19.         EntityRepositoryInterface $productRepository,
  20.         CacheInvalidator $cacheInvalidator
  21.     )
  22.     {
  23.         $this->customerPriceRepository $customerPriceRepository;
  24.         $this->productRepository $productRepository;
  25.         $this->cacheInvalidator $cacheInvalidator;
  26.     }
  27.     /**
  28.      * @inheritDoc
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             'vio_customer_price.written' => 'onCustomerPriceWritten'
  34.         ];
  35.     }
  36.     public function onCustomerPriceWritten(EntityWrittenEvent $event): void
  37.     {
  38.         /** @var TermsResult $productIdsAggregation */
  39.         $productIdsAggregation $this->customerPriceRepository->aggregate(
  40.             (new Criteria($event->getIds()))
  41.                 ->addAggregation(
  42.                     new TermsAggregation(
  43.                         'productIds',
  44.                         'productId'
  45.                     )
  46.                 ),
  47.             $event->getContext()
  48.         )->get('productIds');
  49.         $productIds array_map(static function($productIdBucket) {
  50.             return $productIdBucket->getKey();
  51.         }, $productIdsAggregation->getBuckets());
  52.         $productIds array_filter($productIds);
  53.         $productIds array_unique($productIds);
  54.         /** @var TermsResult $parentIdsResult */
  55.         $parentIdsResult $this->productRepository
  56.             ->aggregate(
  57.                 (new Criteria($productIds))
  58.                     ->addAggregation(
  59.                         new TermsAggregation(
  60.                             'parentIds',
  61.                             'parentId'
  62.                         )
  63.                     )
  64.                 , $event->getContext()
  65.             )->get('parentIds');
  66.         $parentIds array_map(static function($productIdBucket) {
  67.             return $productIdBucket->getKey();
  68.         }, $parentIdsResult->getBuckets());
  69.         $parentIds array_filter($parentIds);
  70.         $parentIds array_unique($parentIds);
  71.         $productIds array_unique(array_merge($productIds$parentIds));
  72.         $tags array_map([CachedProductDetailRoute::class, 'buildName'], $productIds);
  73.         $this->cacheInvalidator->invalidate($tagstrue);
  74.     }
  75. }