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

  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 NetzpBlog6\Resolver\BlogElementResolver;
  7. use Shopware\Core\Content\Category\Event\CategoryRouteCacheKeyEvent;
  8. use Shopware\Core\Content\Cms\Aggregate\CmsSlotTranslation\CmsSlotTranslationDefinition;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  15. class CacheInvalidationSubscriber implements EventSubscriberInterface
  16. {
  17.     public function __construct(private readonly ContainerInterface $container,
  18.                                 private readonly CacheInvalidator $cacheInvalidator,
  19.                                 private readonly EntityRepository $cmsSlotsRepository)
  20.     {
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             EntityWrittenContainerEvent::class => [ ['invalidate'2001] ],
  26.             CategoryRouteCacheKeyEvent::class  => 'onCategoryRouteCacheKey'
  27.         ];
  28.     }
  29.     public function onCategoryRouteCacheKey(CategoryRouteCacheKeyEvent $event): void
  30.     {
  31.         $repo $this->container->get('category.repository');
  32.         $criteria = (new Criteria([$event->getNavigationId()]));
  33.         $criteria->addAssociation('cmsPage.sections.blocks.slots.config');
  34.         $category $repo->search($criteria$event->getContext()->getContext())->getEntities()->first();
  35.         if( ! $category) {
  36.             return;
  37.         }
  38.         $page $category->getCmsPage();
  39.         if ( ! $page) {
  40.             return;
  41.         }
  42.         $blogElements $page->getElementsOfType(BlogElementResolver::ELEMENT_TYPE);
  43.         if(! empty($blogElements))
  44.         {
  45.             $customerId $event->getContext()->getCustomer()?->getId();
  46.             $customerGroupId $event->getContext()->getCustomer()?->getGroupId();
  47.             if ($customerId) {
  48.                 $event->addPart('customer_' $customerId);
  49.             }
  50.             if ($customerGroupId) {
  51.                 $event->addPart('customergroup_' $customerGroupId);
  52.             }
  53.         }
  54.     }
  55.     public function invalidate(EntityWrittenContainerEvent $event): void
  56.     {
  57.         $changesPost $event->getPrimaryKeys(BlogDefinition::ENTITY_NAME);
  58.         $changesCmsSlots $event->getPrimaryKeys(CmsSlotTranslationDefinition::ENTITY_NAME);
  59.         $changesNavigation $event->getPrimaryKeys('category');
  60.         $mustInvalidateBlogListing false;
  61.         if ( ! empty($changesPost)) {
  62.             $blogPostRoutes = [];
  63.             foreach ($changesPost as $postId) {
  64.                 $blogPostRoutes[] = CachedBlogPostRoute::buildName($postId);
  65.             }
  66.             $this->cacheInvalidator->invalidate($blogPostRoutes);
  67.             $mustInvalidateBlogListing true;
  68.         }
  69.         if ( ! empty($changesNavigation)) {
  70.             $blogListingRoutes = [];
  71.             foreach ($changesNavigation as $navigationId) {
  72.                 $blogListingRoutes[] = CachedBlogListingRoute::buildName($navigationId);
  73.             }
  74.             $this->cacheInvalidator->invalidate($blogListingRoutes);
  75.         }
  76.         if( ! empty($changesCmsSlots)) {
  77.             $cmsSlotIds array_map(function($item) {
  78.                 if(is_array($item) && array_key_exists('cmsSlotId'$item)) {
  79.                     return $item['cmsSlotId'];
  80.                 }
  81.                 return $item;
  82.             }, $changesCmsSlots);
  83.             if( ! empty($cmsSlotIds)) {
  84.                 $criteriaSlots = new Criteria($cmsSlotIds);
  85.                 $slots $this->cmsSlotsRepository->search($criteriaSlots$event->getContext())->getEntities();
  86.                 foreach($slots as $slot) {
  87.                     if($slot->getType() == BlogElementResolver::ELEMENT_TYPE) {
  88.                         $mustInvalidateBlogListing true;
  89.                         break;
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.         if($mustInvalidateBlogListing) {
  95.             $this->cacheInvalidator->invalidate([
  96.                 CachedBlogListingRoute::buildName('')
  97.             ]);
  98.         }
  99.     }
  100. }