custom/plugins/SasBlogModule/src/Subscriber/BlogCacheInvalidSubscriber.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sas\BlogModule\Subscriber;
  3. use Sas\BlogModule\Content\Blog\BlogSeoUrlRoute;
  4. use Sas\BlogModule\Controller\CachedBlogController;
  5. use Sas\BlogModule\Controller\CachedBlogRssController;
  6. use Sas\BlogModule\Controller\CachedBlogSearchController;
  7. use Shopware\Core\Content\Category\SalesChannel\CachedCategoryRoute;
  8. use Shopware\Core\Content\Seo\Event\SeoEvents;
  9. use Shopware\Core\Content\Seo\SeoUrlUpdater;
  10. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. use Shopware\Core\System\SystemConfig\SystemConfigService;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. /**
  21.  * After you change the SEO Template within the SEO settings, we need to re-generate all existing URLs.
  22.  * All old URL's should match the new saved SEO Template pattern.
  23.  */
  24. class BlogCacheInvalidSubscriber implements EventSubscriberInterface
  25. {
  26.     private SeoUrlUpdater $seoUrlUpdater;
  27.     private EntityRepository $categoryRepository;
  28.     private CacheInvalidator $cacheInvalidator;
  29.     private SystemConfigService $systemConfigService;
  30.     public function __construct(
  31.         SeoUrlUpdater $seoUrlUpdater,
  32.         EntityRepository $categoryRepository,
  33.         CacheInvalidator $cacheInvalidator,
  34.         SystemConfigService $systemConfigService
  35.     ) {
  36.         $this->seoUrlUpdater $seoUrlUpdater;
  37.         $this->categoryRepository $categoryRepository;
  38.         $this->cacheInvalidator $cacheInvalidator;
  39.         $this->systemConfigService $systemConfigService;
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             'sas_blog_entries.written' => [
  45.                 ['onUpdateSeoUrl'10],
  46.                 ['onUpdateInvalidateCache'11],
  47.             ],
  48.             'sas_blog_entries.deleted' => [
  49.                 ['onDeleteSeoUrl'10],
  50.                 ['onDeleteInvalidateCache'11],
  51.             ],
  52.             SeoEvents::SEO_URL_TEMPLATE_WRITTEN_EVENT => [
  53.                 ['updateSeoUrlForAllArticles'10],
  54.                 ['onUpdateCacheCategory'11],
  55.             ],
  56.         ];
  57.     }
  58.     /**
  59.      * When a blog article created or updated we will generate the SeoUrl for it
  60.      */
  61.     public function onUpdateSeoUrl(EntityWrittenEvent $event): void
  62.     {
  63.         $this->seoUrlUpdater->update(BlogSeoUrlRoute::ROUTE_NAME$event->getIds());
  64.     }
  65.     /**
  66.      * When a blog article deleted we will mark as deleted the SeoUrl
  67.      */
  68.     public function onDeleteSeoUrl(EntityDeletedEvent $event): void
  69.     {
  70.         $this->seoUrlUpdater->update(BlogSeoUrlRoute::ROUTE_NAME$event->getIds());
  71.     }
  72.     /**
  73.      * Invalidate blog cms cache when create or update
  74.      */
  75.     public function onUpdateInvalidateCache(EntityWrittenEvent $event): void
  76.     {
  77.         $this->invalidateCache($event->getIds());
  78.         $this->invalidateCacheCategory($event->getContext());
  79.     }
  80.     /**
  81.      * Invalidate blog cms cache when delete article
  82.      */
  83.     public function onDeleteInvalidateCache(EntityDeletedEvent $event): void
  84.     {
  85.         $this->invalidateCache($event->getIds());
  86.         $this->invalidateCacheCategory($event->getContext());
  87.     }
  88.     /**
  89.      * When update SEO template in the settings, we will update all SEO URLs for the blog articles
  90.      */
  91.     public function updateSeoUrlForAllArticles(): void
  92.     {
  93.         $this->seoUrlUpdater->update(BlogSeoUrlRoute::ROUTE_NAME, []);
  94.     }
  95.     /**
  96.      * Invalidate blog category cache
  97.      */
  98.     private function invalidateCacheCategory(Context $context): void
  99.     {
  100.         $catIds $this->getBlogCategoryIds($context);
  101.         // invalidates the category route cache when a category changed
  102.         $this->cacheInvalidator->invalidate(
  103.             array_map([CachedCategoryRoute::class, 'buildName'], $catIds)
  104.         );
  105.     }
  106.     private function getBlogCategoryIds(Context $context): array
  107.     {
  108.         $criteria = new Criteria();
  109.         $criteria->addFilter(new EqualsFilter('active'true));
  110.         $criteria->addFilter(new EqualsFilter('cmsPage.sections.blocks.type''blog-listing'));
  111.         $criteria->addAssociation('cmsPage.sections.blocks');
  112.         return $this->categoryRepository->search($criteria$context)->getIds();
  113.     }
  114.     /**
  115.      * Invalidate cache
  116.      */
  117.     private function invalidateCache(array $articleIds): void
  118.     {
  119.         $this->cacheInvalidator->invalidate(
  120.             array_map([CachedBlogController::class, 'buildName'], $articleIds)
  121.         );
  122.         $this->cacheInvalidator->invalidate([
  123.             'product-suggest-route',
  124.             'product-search-route',
  125.             CachedBlogSearchController::SEARCH_TAG,
  126.             CachedBlogRssController::RSS_TAG,
  127.         ]);
  128.         $cmsBlogDetailPageId $this->systemConfigService->get('SasBlogModule.config.cmsBlogDetailPage');
  129.         if (!\is_string($cmsBlogDetailPageId)) {
  130.             return;
  131.         }
  132.         $this->cacheInvalidator->invalidate(
  133.             array_map([EntityCacheKeyGenerator::class, 'buildCmsTag'], [$cmsBlogDetailPageId])
  134.         );
  135.     }
  136. }