vendor/lnb/shopware6-lnb-faq/src/Subscriber/CacheInvalidationSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace Lnb\Shopware6\LnbFaq\Subscriber;
  3. use Lnb\Shopware6\LnbFaq\Content\Faq\FaqDefinition;
  4. use Lnb\Shopware6\LnbFaq\Content\FaqGroup\FaqGroupDefinition;
  5. use Lnb\Shopware6\LnbFaq\Content\FaqGroupPosition\FaqGroupPositionDefinition;
  6. use Shopware\Core\Content\Category\Event\CategoryRouteCacheTagsEvent;
  7. use Shopware\Core\Content\Category\SalesChannel\CategoryRouteResponse;
  8. use Shopware\Core\Content\Cms\CmsPageEntity;
  9. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CacheInvalidationSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(
  15.         private CacheInvalidator $cacheInvalidator
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             EntityWrittenContainerEvent::class => 'invalidateCache',
  22.             CategoryRouteCacheTagsEvent::class => 'invalidateCategoryCache',
  23.         ];
  24.     }
  25.     public function invalidateCache(EntityWrittenContainerEvent $event): void
  26.     {
  27.         if (
  28.             !empty($event->getPrimaryKeys(FaqDefinition::ENTITY_NAME)) ||
  29.             !empty($event->getPrimaryKeys(FaqGroupDefinition::ENTITY_NAME)) ||
  30.             !empty($event->getPrimaryKeys(FaqGroupPositionDefinition::ENTITY_NAME))
  31.         ) {
  32.             $this->cacheInvalidator->invalidate(
  33.                 ['any-faq'],
  34.                 true
  35.             );
  36.         }
  37.     }
  38.     public function invalidateCategoryCache(CategoryRouteCacheTagsEvent $event): void
  39.     {
  40.         $response $event->getResponse();
  41.         if (!$response instanceof CategoryRouteResponse) {
  42.             return;
  43.         }
  44.         $page $response->getCategory()->getCmsPage();
  45.         if (!$page instanceof CmsPageEntity) {
  46.             return;
  47.         }
  48.         $slots $page->getElementsOfType('lnb-faq-groups');
  49.         if (!empty($slots)) {
  50.             $event->setTags(
  51.                 array_merge($event->getTags(), ['any-faq'])
  52.             );
  53.         }
  54.     }
  55. }