custom/plugins/NetzpBlog6/src/Controller/StoreApi/CacheInvalidationSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpBlog6\Controller\StoreApi;
  3. use NetzpBlog6\Controller\StoreApi\BlogListing\CachedBlogListingRoute;
  4. use NetzpBlog6\Controller\StoreApi\BlogPost\CachedBlogPostRoute;
  5. use NetzpBlog6\Core\Content\Blog\BlogDefinition;
  6. use Shopware\Core\Content\Cms\Aggregate\CmsSlotTranslation\CmsSlotTranslationDefinition;
  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 $cmsSlotsRepository;
  16.     public function __construct(CacheInvalidator $cacheInvalidatorEntityRepositoryInterface $cmsSlotsRepository)
  17.     {
  18.         $this->cacheInvalidator $cacheInvalidator;
  19.         $this->cmsSlotsRepository $cmsSlotsRepository;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             EntityWrittenContainerEvent::class => [
  25.                 ['invalidate'2001]
  26.             ]
  27.         ];
  28.     }
  29.     public function invalidate(EntityWrittenContainerEvent $event): void
  30.     {
  31.         $changesPost $event->getPrimaryKeys(BlogDefinition::ENTITY_NAME);
  32.         $changesCmsSlots $event->getPrimaryKeys(CmsSlotTranslationDefinition::ENTITY_NAME);
  33.         $changesNavigation $event->getPrimaryKeys('category');
  34.         $mustInvalidateBlogListing false;
  35.         if ( ! empty($changesPost)) {
  36.             $blogPostRoutes = [];
  37.             foreach ($changesPost as $postId) {
  38.                 $blogPostRoutes[] = CachedBlogPostRoute::buildName($postId);
  39.             }
  40.             $this->cacheInvalidator->invalidate($blogPostRoutes);
  41.             $mustInvalidateBlogListing true;
  42.         }
  43.         if ( ! empty($changesNavigation)) {
  44.             $blogListingRoutes = [];
  45.             foreach ($changesNavigation as $navigationId) {
  46.                 $blogListingRoutes[] = CachedBlogListingRoute::buildName($navigationId);
  47.             }
  48.             $this->cacheInvalidator->invalidate($blogListingRoutes);
  49.         }
  50.         if( ! empty($changesCmsSlots)) {
  51.             $cmsSlotIds array_map(function($item) {
  52.                 if(is_array($item) && array_key_exists('cmsSlotId'$item)) {
  53.                     return $item['cmsSlotId'];
  54.                 }
  55.                 return $item;
  56.             }, $changesCmsSlots);
  57.             if( ! empty($cmsSlotIds)) {
  58.                 $criteriaSlots = new Criteria($cmsSlotIds);
  59.                 $slots $this->cmsSlotsRepository->search($criteriaSlots$event->getContext())->getEntities();
  60.                 foreach($slots as $slot) {
  61.                     if($slot->getType() == 'netzp-blog6') {
  62.                         $mustInvalidateBlogListing true;
  63.                         break;
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         if($mustInvalidateBlogListing) {
  69.             $this->cacheInvalidator->invalidate([
  70.                 CachedBlogListingRoute::buildName('')
  71.             ]);
  72.         }
  73.     }
  74. }