vendor/dvdw/events/src/Framework/Adapter/Cache/CacheInvalidationSubscriber.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dvdw\Events\Framework\Adapter\Cache;
  3. use Doctrine\DBAL\Connection;
  4. use Dvdw\Events\Content\DvdwEvent\Aggregate\DvdwEventUsp\DvdwEventUspDefinition;
  5. use Dvdw\Events\Content\DvdwEvent\DvdwEventDefinition;
  6. use Dvdw\Events\Content\DvdwEvent\SalesChannel\CachedDvdwEventRoute;
  7. use Dvdw\Events\Content\DvdwProduct\DvdwProductDefinition;
  8. use Dvdw\Events\Content\DvdwProduct\SalesChannel\CachedDvdwProductRoute;
  9. use Dvdw\Events\Content\DvdwPromotion\Aggregate\DvdwPromotionLimitation\DvdwPromotionLimitationDefinition;
  10. use Dvdw\Events\Content\DvdwPromotion\DvdwPromotionDefinition;
  11. use Dvdw\Events\Content\DvdwPromotion\SalesChannel\CachedDvdwPromotionRoute;
  12. use Dvdw\Events\Content\DvdwTicket\DvdwTicketDefinition;
  13. use Dvdw\Events\Content\DvdwTicket\SalesChannel\CachedDvdwTicketRoute;
  14. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class CacheInvalidationSubscriber implements EventSubscriberInterface
  19. {
  20.     private CacheInvalidator $cacheInvalidator;
  21.     private Connection $connection;
  22.     public function __construct(
  23.         CacheInvalidator $cacheInvalidator,
  24.         Connection $connection
  25.     )
  26.     {
  27.         $this->cacheInvalidator $cacheInvalidator;
  28.         $this->connection $connection;
  29.     }
  30.     public static function getSubscribedEvents(): iterable
  31.     {
  32.         return [
  33.             EntityWrittenContainerEvent::class => [
  34.                 ['invalidateDvdwEventIds'2001],
  35.                 ['invalidateDvdwProductIds'2002],
  36.                 ['invalidateDvdwPromotionIds'2003],
  37.                 ['invalidateDvdwTicketIds'2004]
  38.             ]
  39.         ];
  40.     }
  41.     public function invalidateDvdwEventIds(EntityWrittenContainerEvent $event): void
  42.     {
  43.         $this->cacheInvalidator->invalidate($this->getChangedDvdwEvents($event));
  44.     }
  45.     public function invalidateDvdwProductIds(EntityWrittenContainerEvent $event): void
  46.     {
  47.         $this->cacheInvalidator->invalidate($this->getChangedDvdwProducts($event));
  48.     }
  49.     public function invalidateDvdwPromotionIds(EntityWrittenContainerEvent $event): void
  50.     {
  51.         $this->cacheInvalidator->invalidate($this->getChangedDvdwPromotions($event));
  52.     }
  53.     public function invalidateDvdwTicketIds(EntityWrittenContainerEvent $event): void
  54.     {
  55.         $this->cacheInvalidator->invalidate($this->getChangedDvdwTickets($event));
  56.     }
  57.     private function getChangedDvdwEvents(EntityWrittenContainerEvent $event): array
  58.     {
  59.         $ids array_merge(
  60.             $this->getRelevantIds($eventDvdwEventDefinition::ENTITY_NAME),
  61.             $this->getIdsFromAssociation($eventDvdwEventUspDefinition::ENTITY_NAME'dvdw_event_id')
  62.         );
  63.         if (empty($ids)) {
  64.             return [];
  65.         }
  66.         return $this->buildRouteNames(CachedDvdwEventRoute::class, $ids);
  67.     }
  68.     private function getChangedDvdwProducts(EntityWrittenContainerEvent $event): array
  69.     {
  70.         return $this->getChanged(
  71.             $event,
  72.             DvdwProductDefinition::ENTITY_NAME,
  73.             CachedDvdwProductRoute::class
  74.         );
  75.     }
  76.     private function getChangedDvdwPromotions(EntityWrittenContainerEvent $event): array
  77.     {
  78.         $ids array_merge(
  79.             $this->getRelevantIds($eventDvdwPromotionDefinition::ENTITY_NAME),
  80.             $this->getIdsFromAssociation($eventDvdwPromotionLimitationDefinition::ENTITY_NAME'dvdw_promotion_id')
  81.         );
  82.         if (empty($ids)) {
  83.             return [];
  84.         }
  85.         return $this->buildRouteNames(CachedDvdwPromotionRoute::class, $ids);
  86.     }
  87.     private function getChangedDvdwTickets(EntityWrittenContainerEvent $event): array
  88.     {
  89.         return $this->getChanged(
  90.             $event,
  91.             DvdwTicketDefinition::ENTITY_NAME,
  92.             CachedDvdwTicketRoute::class
  93.         );
  94.     }
  95.     private function getChanged(EntityWrittenContainerEvent $eventstring $entitystring $cachedRouteClass): array
  96.     {
  97.         $ids $this->getRelevantIds($event$entity);
  98.         if (empty($ids)) {
  99.             return [];
  100.         }
  101.         return $this->buildRouteNames($cachedRouteClass$ids);
  102.     }
  103.     private function getRelevantIds(EntityWrittenContainerEvent $eventstring $entity): array
  104.     {
  105.         return array_merge($event->getPrimaryKeys($entity), $event->getDeletedPrimaryKeys($entity));
  106.     }
  107.     private function buildRouteNames(string $cachedRouteClass, array $ids): array
  108.     {
  109.         return array_map([$cachedRouteClass'buildName'], $ids);
  110.     }
  111.     private function getIdsFromAssociation(EntityWrittenContainerEvent $eventstring $associationstring $column): array
  112.     {
  113.         $ids $this->getRelevantIds($event$association);
  114.         if (empty($ids)) {
  115.             return [];
  116.         }
  117.         return $this->connection->fetchFirstColumn(
  118.             sprintf('SELECT DISTINCT LOWER(HEX(%s)) FROM %s WHERE id IN (:ids)'$column$association),
  119.             ['ids' => Uuid::fromHexToBytesList($ids)],
  120.             ['ids' => Connection::PARAM_STR_ARRAY]
  121.         );
  122.     }
  123. }