vendor/store.shopware.com/dreiscset/src/Subscriber/DataAbstractionLayer/ProductWrittenSubscriber.php line 75

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSet\Subscriber\DataAbstractionLayer;
  3. use DreiscSet\Core\Cache\AbstractCacheInvalidator;
  4. use DreiscSet\Core\Content\DreiscSetGroup\DreiscSetGroupRepository;
  5. use DreiscSet\Core\Content\DreiscSetGroupItem\DreiscSetGroupItemRepository;
  6. use DreiscSet\Core\Content\Product\ProductRepository;
  7. use DreiscSet\Core\Content\ProductStream\ProductStreamRepository;
  8. use Shopware\Core\Content\ProductStream\Service\ProductStreamBuilder;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Shopware\Core\Framework\Context;
  15. class ProductWrittenSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var DreiscSetGroupItemRepository
  19.      */
  20.     protected $dreiscSetGroupItemRepository;
  21.     /**
  22.      * @var AbstractCacheInvalidator
  23.      */
  24.     protected $cacheInvalidator;
  25.     /**
  26.      * @var ProductStreamRepository
  27.      */
  28.     protected $productStreamRepository;
  29.     /**
  30.      * @var ProductStreamBuilder
  31.      */
  32.     protected $productStreamBuilder;
  33.     /**
  34.      * @var ProductRepository
  35.      */
  36.     protected $productRepository;
  37.     /**
  38.      * @var DreiscSetGroupRepository
  39.      */
  40.     protected $dreiscSetGroupRepository;
  41.     /**
  42.      * @param DreiscSetGroupItemRepository $dreiscSetGroupItemRepository
  43.      * @param AbstractCacheInvalidator $cacheInvalidator
  44.      * @param ProductStreamRepository $productStreamRepository
  45.      * @param ProductStreamBuilder $productStreamBuilder
  46.      * @param ProductRepository $productRepository
  47.      * @param DreiscSetGroupRepository $dreiscSetGroupRepository
  48.      */
  49.     public function __construct(DreiscSetGroupItemRepository $dreiscSetGroupItemRepositoryAbstractCacheInvalidator $cacheInvalidatorProductStreamRepository $productStreamRepositoryProductStreamBuilder $productStreamBuilderProductRepository $productRepositoryDreiscSetGroupRepository $dreiscSetGroupRepository)
  50.     {
  51.         $this->dreiscSetGroupItemRepository $dreiscSetGroupItemRepository;
  52.         $this->cacheInvalidator $cacheInvalidator;
  53.         $this->productStreamRepository $productStreamRepository;
  54.         $this->productStreamBuilder $productStreamBuilder;
  55.         $this->productRepository $productRepository;
  56.         $this->dreiscSetGroupRepository $dreiscSetGroupRepository;
  57.     }
  58.     public static function getSubscribedEvents()
  59.     {
  60.         return [
  61.             'product.written' => 'onProductWritten'
  62.         ];
  63.     }
  64.     public function onProductWritten(EntityWrittenEvent $entityWrittenEvent)
  65.     {
  66.         $invalideDreiscSetIds = [];
  67.         /** Determine invalid sets based on direct product assignments to the set group */
  68.         $invalideDreiscSetIds array_merge($this->determineDirectAssignments($entityWrittenEvent->getIds()), $invalideDreiscSetIds);
  69.         /** Determine invalid sets based on product stream assignments to the set group */
  70.         $invalideDreiscSetIds array_merge($this->determineProductStreamAssignments($entityWrittenEvent->getIds()), $invalideDreiscSetIds);
  71.         $this->cacheInvalidator->invalidateSetParentProducts($invalideDreiscSetIds);
  72.     }
  73.     /**
  74.      * @param array $productIds
  75.      * @return array
  76.      */
  77.     private function determineDirectAssignments(array $productIds): array
  78.     {
  79.         $invalideDreiscSetIds = [];
  80.         /** Fetch all sets with the products are assigned to */
  81.         $criteria = (new Criteria())
  82.             ->addFilter(
  83.                 new EqualsAnyFilter('productId'$productIds)
  84.             )
  85.             ->addAssociation('dreiscSetGroup');
  86.         $entitySearchResult  $this->dreiscSetGroupItemRepository->search($criteria);
  87.         /** Abort, if no match was found */
  88.         if (=== $entitySearchResult->count()) {
  89.             return $invalideDreiscSetIds;
  90.         }
  91.         /** Fetch the dreisc set ids which we need to invalidate */
  92.         foreach($entitySearchResult->getEntities() as $dreiscSetGroupItemEntity ) {
  93.             $invalideDreiscSetIds[] = $dreiscSetGroupItemEntity->getDreiscSetGroup()->getDreiscSetId();
  94.         }
  95.         return $invalideDreiscSetIds;
  96.     }
  97.     /**
  98.      * @param array $productIds
  99.      * @return array
  100.      */
  101.     private function determineProductStreamAssignments(array $productIds): array
  102.     {
  103.         $invalideProductStreamIds $this->fetchProductStreamIdsWithProducts($productIds);
  104.         $invalideDreiscSetIds = [];
  105.         /** Fetch all sets with the products are assigned to */
  106.         $criteria = (new Criteria())
  107.             ->addFilter(
  108.                 new EqualsAnyFilter('productStreamId'$invalideProductStreamIds)
  109.             );
  110.         $entitySearchResult  $this->dreiscSetGroupRepository->search($criteria);
  111.         /** Abort, if no match was found */
  112.         if (=== $entitySearchResult->count()) {
  113.             return $invalideDreiscSetIds;
  114.         }
  115.         /** Fetch the dreisc set ids which we need to invalidate */
  116.         foreach($entitySearchResult->getEntities() as $dreiscSetGroupEntity ) {
  117.             $invalideDreiscSetIds[] = $dreiscSetGroupEntity->getDreiscSetId();
  118.         }
  119.         return $invalideDreiscSetIds;
  120.     }
  121.     /**
  122.      * @param array $productIds
  123.      * @return array
  124.      */
  125.     private function fetchProductStreamIdsWithProducts(array $productIds): array
  126.     {
  127.         $invalideProductStreamIds = [];
  128.         /** At first, we iterate all product streams to check, if one of the given products is part of it */
  129.         $idSearchResult $this->productStreamRepository->searchIds(new Criteria());
  130.         foreach ($idSearchResult->getIds() as $productStreamId) {
  131.             /** Build the product stream filters */
  132.             $productStreamFilters $this->productStreamBuilder->buildFilters($productStreamIdContext::createDefaultContext());
  133.             /** Create a criteria with the product stream filters and the products ids */
  134.             $criteria = (new Criteria())->addFilter(
  135.                 new MultiFilter(MultiFilter::CONNECTION_AND, [
  136.                     ...$productStreamFilters,
  137.                     new EqualsAnyFilter('product.id'$productIds)
  138.                 ])
  139.             );
  140.             if (!empty($this->productRepository->searchIds($criteria)->getIds())) {
  141.                 $invalideProductStreamIds[] = $productStreamId;
  142.             }
  143.         }
  144.         return $invalideProductStreamIds;
  145.     }
  146. }