custom/plugins/ShopStudioBlog/src/Core/Framework/Adapter/Cache/CacheInvalidationSubscriber.php line 129

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ShopStudio\Blog\Core\Framework\Adapter\Cache;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Exception;
  5. use ShopStudio\Blog\Content\BlogPost\Aggregate\BlogPostComment\Events\BlogPostCommentChangedEventInterface;
  6. use ShopStudio\Blog\Content\BlogPost\Aggregate\BlogPostComment\Events\BlogPostCommentIndexerEvent;
  7. use ShopStudio\Blog\Content\BlogPost\Events\BlogPostChangedEventInterface;
  8. use ShopStudio\Blog\Content\BlogPost\Events\BlogPostIndexerEvent;
  9. use ShopStudio\Blog\Content\BlogPost\SalesChannel\Comment\CachedBlogPostCommentRoute;
  10. use ShopStudio\Blog\Content\BlogPost\SalesChannel\Detail\CachedBlogPostDetailRoute;
  11. use ShopStudio\Blog\Content\BlogPost\SalesChannel\Listing\CachedBlogPostListingRoute;
  12. use ShopStudio\Blog\Content\BlogPost\SalesChannel\Search\CachedBlogPostSearchRoute;
  13. use ShopStudio\Blog\Content\BlogPost\SalesChannel\Suggest\CachedBlogPostSuggestRoute;
  14. use ShopStudio\Blog\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  15. use Shopware\Core\Defaults;
  16. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  17. use Shopware\Core\Framework\Uuid\Uuid;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. /**
  20.  * @since 2.0.0
  21.  */
  22. class CacheInvalidationSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @since 2.0.0
  26.      */
  27.     private CacheInvalidator $cacheInvalidator;
  28.     /**
  29.      * @since 2.0.0
  30.      */
  31.     private Connection $connection;
  32.     /**
  33.      * @since 2.0.0
  34.      */
  35.     public function __construct(
  36.         CacheInvalidator $cacheInvalidator,
  37.         Connection $connection
  38.     ) {
  39.         $this->cacheInvalidator $cacheInvalidator;
  40.         $this->connection $connection;
  41.     }
  42.     /**
  43.      * @since 2.0.0
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             BlogPostIndexerEvent::class => [
  49.                 ['invalidateBlogPostSearch'2000],
  50.                 ['invalidateBlogPostListings'2001],
  51.                 ['invalidateBlogPostIds'2002],
  52.                 ['invalidateBlogPostDetails'2004],
  53.             ],
  54.             BlogPostCommentIndexerEvent::class => [
  55.                 ['invalidateBlogPostComments'2000],
  56.                 ['invalidateBlogPostCommentIds'2002],
  57.                 ['invalidateBlogPostDetailsByComment'2004],
  58.             ],
  59.         ];
  60.     }
  61.     /**
  62.      * Invalidates the search and suggest route each time a blog post changed.
  63.      *
  64.      * @since 2.0.0
  65.      */
  66.     public function invalidateBlogPostSearch(): void
  67.     {
  68.         $this->cacheInvalidator->invalidate([
  69.             CachedBlogPostSuggestRoute::NAME,
  70.             CachedBlogPostSearchRoute::NAME,
  71.         ]);
  72.     }
  73.     /**
  74.      * @since 2.0.0
  75.      */
  76.     public function invalidateBlogPostIds(BlogPostChangedEventInterface $event): void
  77.     {
  78.         $this->cacheInvalidator->invalidate(
  79.             array_map([EntityCacheKeyGenerator::class, 'buildBlogPostTag'], $event->getIds())
  80.         );
  81.     }
  82.     /**
  83.      * Invalidates blog post listings which are based on the blog post category assignment.
  84.      *
  85.      * @since 2.0.0
  86.      *
  87.      * @throws Exception
  88.      */
  89.     public function invalidateBlogPostListings(BlogPostChangedEventInterface $event): void
  90.     {
  91.         $ids $this->connection->fetchFirstColumn(
  92.             'SELECT DISTINCT LOWER(HEX(category_id)) as category_id
  93.              FROM shopstudio_blog_blog_post_category_tree
  94.              WHERE shopstudio_blog_blog_post_id IN (:ids)
  95.              AND category_version_id = :version',
  96.             ['ids' => Uuid::fromHexToBytesList($event->getIds()), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  97.             ['ids' => Connection::PARAM_STR_ARRAY]
  98.         );
  99.         $this->cacheInvalidator->invalidate(
  100.             array_map([CachedBlogPostListingRoute::class, 'buildName'], $ids)
  101.         );
  102.     }
  103.     /**
  104.      * Invalidates the blog post detail route each time a blog post changed.
  105.      *
  106.      * @since 2.0.0
  107.      */
  108.     public function invalidateBlogPostDetails(BlogPostChangedEventInterface $event): void
  109.     {
  110.         $this->cacheInvalidator->invalidate(
  111.             array_map([CachedBlogPostDetailRoute::class, 'buildName'], $event->getIds())
  112.         );
  113.     }
  114.     /**
  115.      * @since 2.0.0
  116.      */
  117.     public function invalidateBlogPostCommentIds(BlogPostCommentChangedEventInterface $event): void
  118.     {
  119.         $this->cacheInvalidator->invalidate(
  120.             array_map([EntityCacheKeyGenerator::class, 'buildBlogPostCommentTag'], $event->getIds())
  121.         );
  122.     }
  123.     /**
  124.      * Invalidates blog post listings which are based on the blog post category assignment.
  125.      *
  126.      * @since 2.0.0
  127.      *
  128.      * @throws Exception
  129.      */
  130.     public function invalidateBlogPostComments(BlogPostCommentChangedEventInterface $event): void
  131.     {
  132.         $ids $this->findBlogPostIdsByCommentIds($event->getIds());
  133.         $this->cacheInvalidator->invalidate(
  134.             array_map([CachedBlogPostCommentRoute::class, 'buildName'], $ids)
  135.         );
  136.     }
  137.     /**
  138.      * @since 2.0.0
  139.      *
  140.      * @throws Exception
  141.      */
  142.     public function invalidateBlogPostDetailsByComment(BlogPostCommentChangedEventInterface $event): void
  143.     {
  144.         $ids $this->findBlogPostIdsByCommentIds($event->getIds());
  145.         $this->cacheInvalidator->invalidate(
  146.             array_map([CachedBlogPostDetailRoute::class, 'buildName'], $ids)
  147.         );
  148.     }
  149.     /**
  150.      * @since 2.0.0
  151.      *
  152.      * @param string[] $blogPostCommentIds
  153.      *
  154.      * @throws Exception
  155.      *
  156.      * @return string[]
  157.      */
  158.     private function findBlogPostIdsByCommentIds(array $blogPostCommentIds): array
  159.     {
  160.         return $this->connection->fetchFirstColumn(
  161.             'SELECT DISTINCT LOWER(HEX(shopstudio_blog_blog_post_id)) as blog_post_id
  162.              FROM shopstudio_blog_blog_post_comment
  163.              WHERE id IN (:ids)',
  164.             ['ids' => Uuid::fromHexToBytesList($blogPostCommentIds)],
  165.             ['ids' => Connection::PARAM_STR_ARRAY]
  166.         );
  167.     }
  168. }