custom/plugins/WbfkCmsHideParts/src/Cache/CacheInvalidationSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. namespace Wbfk\CmsHideParts\Cache;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Exception;
  5. use Shopware\Core\Content\Category\SalesChannel\CachedCategoryRoute;
  6. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Wbfk\CmsHideParts\DataAbstractionLayer\HiddenCmsPartsDefinition;
  12. use Wbfk\CmsHideParts\DataAbstractionLayer\HiddenEntityPartsDefinition;
  13. class CacheInvalidationSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var CacheInvalidator
  17.      */
  18.     private CacheInvalidator $cacheInvalidator;
  19.     /**
  20.      * @var Connection
  21.      */
  22.     private Connection $connection;
  23.     public function __construct(CacheInvalidator $cacheInvalidatorConnection $connection)
  24.     {
  25.         $this->cacheInvalidator $cacheInvalidator;
  26.         $this->connection $connection;
  27.     }
  28.     /**
  29.      * @return \array[][]
  30.      */
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             EntityWrittenContainerEvent::class => [
  35.                 ['invalidatePage'3001],
  36.             ]
  37.         ];
  38.     }
  39.     /**
  40.      * @throws Exception
  41.      */
  42.     public function invalidatePage(EntityWrittenContainerEvent $event): void
  43.     {
  44.         $pageHiddenPartsIds $event->getPrimaryKeys(HiddenCmsPartsDefinition::ENTITY_NAME);
  45.         if ($pageHiddenPartsIds) {
  46.             $pageIdsToInvalidate $this->connection->fetchFirstColumn(
  47.                 'SELECT LOWER(HEX(`cms_page_id`)) FROM `wbfk_hidden_cms_parts` WHERE `id` IN (:ids)',
  48.                 ['ids' => Uuid::fromHexToBytesList($pageHiddenPartsIds)],
  49.                 ['ids' => Connection::PARAM_STR_ARRAY]
  50.             );
  51.             if ($pageIdsToInvalidate) {
  52.                 $this->cacheInvalidator->invalidate(
  53.                     array_map([EntityCacheKeyGenerator::class, 'buildCmsTag'], $pageIdsToInvalidate)
  54.                 );
  55.             }
  56.         }
  57.         $entityHiddenPartsIds $event->getPrimaryKeys(HiddenEntityPartsDefinition::ENTITY_NAME);
  58.         if ($entityHiddenPartsIds) {
  59.             $entitiesToInvalidate $this->connection->fetchAllAssociative(
  60.                 'SELECT LOWER(HEX(`id`)) as `id`, entity, LOWER(HEX(`entity_id`)) as `entity_id` FROM `wbfk_hidden_entity_parts` WHERE `id` IN (:ids)',
  61.                 ['ids' => Uuid::fromHexToBytesList($entityHiddenPartsIds)],
  62.                 ['ids' => Connection::PARAM_STR_ARRAY]
  63.             );
  64.             $idsByType = [];
  65.             foreach ($entitiesToInvalidate as $entity) {
  66.                 $idsByType[$entity['entity']][] = $entity['id'];
  67.             }
  68.             if (array_key_exists('category'$idsByType)) {
  69.                 $this->cacheInvalidator->invalidate(
  70.                     array_map([CachedCategoryRoute::class, 'buildName'], $idsByType['category'])
  71.                 );
  72.             }
  73.             if (array_key_exists('product'$idsByType)) {
  74.                 $this->cacheInvalidator->invalidate(
  75.                     array_map([EntityCacheKeyGenerator::class, 'buildProductTag'], $idsByType['product'])
  76.                 );
  77.             }
  78.         }
  79.     }
  80. }