custom/plugins/NetzpEvents6/src/Controller/StoreApi/CacheInvalidationSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpEvents6\Controller\StoreApi;
  3. use NetzpEvents6\Controller\StoreApi\EventListing\CachedEventListingRoute;
  4. use NetzpEvents6\Core\Content\Event\EventDefinition;
  5. use Shopware\Core\Content\Cms\Aggregate\CmsSlotTranslation\CmsSlotTranslationDefinition;
  6. use Shopware\Core\Content\Product\SalesChannel\Detail\CachedProductDetailRoute;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  12. class CacheInvalidationSubscriber implements EventSubscriberInterface
  13. {
  14.     private CacheInvalidator $cacheInvalidator;
  15.     private $eventsRepository;
  16.     private $cmsSlotsRepository;
  17.     public function __construct(CacheInvalidator $cacheInvalidator,
  18.                                 EntityRepositoryInterface $eventsRepository,
  19.                                 EntityRepositoryInterface $cmsSlotsRepository)
  20.     {
  21.         $this->cacheInvalidator $cacheInvalidator;
  22.         $this->eventsRepository $eventsRepository;
  23.         $this->cmsSlotsRepository $cmsSlotsRepository;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             EntityWrittenContainerEvent::class => [
  29.                 ['invalidate'2001]
  30.             ],
  31.         ];
  32.     }
  33.     public function invalidate(EntityWrittenContainerEvent $event): void
  34.     {
  35.         $changes $event->getPrimaryKeys(EventDefinition::ENTITY_NAME);
  36.         $changesCmsSlots $event->getPrimaryKeys(CmsSlotTranslationDefinition::ENTITY_NAME);
  37.         $changesNavigation $event->getPrimaryKeys('category');
  38.         $mustInvalidateEventListing false;
  39.         if (! empty($changes)) {
  40.             $criteria = new Criteria($changes);
  41.             $events $this->eventsRepository->search($criteria$event->getContext())->getEntities();
  42.             foreach ($events as $event) {
  43.                 $productId $event->getProductid();
  44.                 if ($productId) {
  45.                     $this->cacheInvalidator->invalidate([
  46.                         CachedProductDetailRoute::buildName($productId)
  47.                     ]);
  48.                 }
  49.             }
  50.             $mustInvalidateEventListing true;
  51.         }
  52.         if ( ! empty($changesNavigation)) {
  53.             $eventListingRoutes = [];
  54.             foreach ($changesNavigation as $navigationId) {
  55.                 $eventListingRoutes[] = CachedEventListingRoute::buildName($navigationId);
  56.             }
  57.             $this->cacheInvalidator->invalidate($eventListingRoutes);
  58.         }
  59.         if( ! empty($changesCmsSlots)) {
  60.             $cmsSlotIds array_map(function($item) {
  61.                 if(is_array($item) && array_key_exists('cmsSlotId'$item)) {
  62.                     return $item['cmsSlotId'];
  63.                 }
  64.                 return $item;
  65.             }, $changesCmsSlots);
  66.             if( ! empty($cmsSlotIds)) {
  67.                 $criteriaSlots = new Criteria($cmsSlotIds);
  68.                 $slots $this->cmsSlotsRepository->search($criteriaSlots$event->getContext())->getEntities();
  69.                 foreach($slots as $slot) {
  70.                     if($slot->getType() == 'netzp-events6') {
  71.                         $mustInvalidateEventListing true;
  72.                         break;
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.         if($mustInvalidateEventListing) {
  78.             $this->cacheInvalidator->invalidate([
  79.                 CachedEventListingRoute::buildName('')
  80.             ]);
  81.         }
  82.     }
  83. }