custom/plugins/KplngiProductOrder/src/Subscriber/OnPositionChange.php line 39

Open in your IDE?
  1. <?php
  2. namespace Kplngi\ProductOrder\Subscriber;
  3. use Kplngi\ProductOrder\Position\ProductCategoryDefinition;
  4. use Shopware\Core\Content\Category\SalesChannel\CachedCategoryRoute;
  5. use Shopware\Core\Content\Category\SalesChannel\CachedNavigationRoute;
  6. use Shopware\Core\Content\Product\SalesChannel\Listing\CachedProductListingRoute;
  7. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\Struct\ArrayEntity;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class OnPositionChange implements EventSubscriberInterface
  14. {
  15.     private const ENTITY_NAME 'kplngi_productcategoryposition';
  16.     private EntityRepositoryInterface $positionRepository;
  17.     private CacheInvalidator $cacheInvalidator;
  18.     public function __construct(
  19.         EntityRepositoryInterface $positionRepository,
  20.         CacheInvalidator $cacheInvalidator
  21.     )
  22.     {
  23.         $this->positionRepository $positionRepository;
  24.         $this->cacheInvalidator $cacheInvalidator;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             EntityWrittenContainerEvent::class => 'onPositionWritten'
  30.         ];
  31.     }
  32.     public function onPositionWritten(EntityWrittenContainerEvent $event): void
  33.     {
  34.         if (!$this->isPositionEntity($event)) {
  35.             return;
  36.         }
  37.         $categoryIds $this->getEffectedCategoryIds($event);
  38.         $navigationIds array_map([CachedNavigationRoute::class, 'buildName'], $categoryIds);
  39.         $navigationIds[] = CachedNavigationRoute::BASE_NAVIGATION_TAG;
  40.         $this->cacheInvalidator->invalidate($navigationIds);
  41.         $this->cacheInvalidator->invalidate(
  42.             array_map([CachedCategoryRoute::class, 'buildName'], $categoryIds)
  43.         );
  44.         $this->cacheInvalidator->invalidate(
  45.             array_map([CachedProductListingRoute::class, 'buildName'], $categoryIds)
  46.         );
  47.     }
  48.     private function isPositionEntity(EntityWrittenContainerEvent $event): bool
  49.     {
  50.         $entityEvent $event->getEventByEntityName(self::ENTITY_NAME);
  51.         if ($entityEvent === null) {
  52.             return false;
  53.         }
  54.         return true;
  55.     }
  56.     private function getEffectedCategoryIds(EntityWrittenContainerEvent $event): array
  57.     {
  58.         $positionIds $event->getPrimaryKeys(ProductCategoryDefinition::ENTITY_NAME);
  59.         if (empty($positionIds)) {
  60.             return [];
  61.         }
  62.         $criteria = new Criteria($positionIds);
  63.         $positions $this->positionRepository->search($criteria$event->getContext());
  64.         $categoryIds = [];
  65.         /** @var ArrayEntity */
  66.         foreach ($positions->getElements() as $position) {
  67.             $categoryIds[] = $position->get('categoryId');
  68.         }
  69.         return $categoryIds;
  70.     }
  71. }