vendor/dvdw/directory/src/Content/Product/Subscriber/CacheInvalidationSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DvdwDirectory\Content\Product\Subscriber;
  3. use DvdwDirectory\Content\Product\SalesChannel\CachedProductCountRoute;
  4. use Shopware\Core\Content\Product\ProductEvents;
  5. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CacheInvalidationSubscriber implements EventSubscriberInterface
  10. {
  11.     private CacheInvalidator $cacheInvalidator;
  12.     public function __construct(
  13.         CacheInvalidator $cacheInvalidator
  14.     )
  15.     {
  16.         $this->cacheInvalidator $cacheInvalidator;
  17.     }
  18.     public static function getSubscribedEvents(): iterable
  19.     {
  20.         return [
  21.             ProductEvents::PRODUCT_WRITTEN_EVENT => 'invalidate',
  22.             ProductEvents::PRODUCT_DELETED_EVENT => 'invalidate'
  23.         ];
  24.     }
  25.     public function invalidate(EntityWrittenEvent $event): void
  26.     {
  27.         foreach ($event->getWriteResults() as $writeResult) {
  28.             $operation $writeResult->getOperation();
  29.             if ($operation === EntityWriteResult::OPERATION_INSERT || $operation === EntityWriteResult::OPERATION_DELETE) {
  30.                 $this->cacheInvalidator->invalidate([CachedProductCountRoute::ALL_TAG]);
  31.                 break;
  32.             }
  33.         }
  34.     }
  35. }