vendor/store.shopware.com/swagplatformsecurity/src/Fixes/NEXT37527/SalesChannelRepository.php line 126

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SalesChannel\Entity;
  3. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  4. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  5. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityAggregationResultLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEventFactory;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Field\AssociationField;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Read\EntityReaderInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\RepositorySearchDetector;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntityAggregatorInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearcherInterface;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  23. use Shopware\Core\Framework\Log\Package;
  24. use Shopware\Core\Framework\Struct\ArrayEntity;
  25. use Shopware\Core\System\SalesChannel\Event\SalesChannelProcessCriteriaEvent;
  26. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  27. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  28. /**
  29.  * @final tag:v6.5.0
  30.  */
  31. #[Package('sales-channel')]
  32. class SalesChannelRepository implements SalesChannelRepositoryInterface
  33. {
  34.     /**
  35.      * @var EntityDefinition
  36.      */
  37.     protected $definition;
  38.     /**
  39.      * @var EntityReaderInterface
  40.      */
  41.     protected $reader;
  42.     /**
  43.      * @var EntitySearcherInterface
  44.      */
  45.     protected $searcher;
  46.     /**
  47.      * @var EntityAggregatorInterface
  48.      */
  49.     protected $aggregator;
  50.     /**
  51.      * @var EventDispatcherInterface
  52.      */
  53.     protected $eventDispatcher;
  54.     private ?EntityLoadedEventFactory $eventFactory;
  55.     /**
  56.      * @internal
  57.      *
  58.      * @deprecated tag:v6.5.0 - $eventFactory parameter will be required
  59.      */
  60.     public function __construct(
  61.         EntityDefinition $definition,
  62.         EntityReaderInterface $reader,
  63.         EntitySearcherInterface $searcher,
  64.         EntityAggregatorInterface $aggregator,
  65.         EventDispatcherInterface $eventDispatcher,
  66.         ?EntityLoadedEventFactory $eventFactory
  67.     ) {
  68.         $this->definition $definition;
  69.         $this->reader $reader;
  70.         $this->searcher $searcher;
  71.         $this->aggregator $aggregator;
  72.         $this->eventDispatcher $eventDispatcher;
  73.         if ($eventFactory !== null) {
  74.             $this->eventFactory $eventFactory;
  75.         }
  76.     }
  77.     /**
  78.      * @deprecated tag:v6.5.0 - Will be removed
  79.      */
  80.     public function setEntityLoadedEventFactory(EntityLoadedEventFactory $eventFactory): void
  81.     {
  82.         $this->eventFactory $eventFactory;
  83.     }
  84.     /**
  85.      * @throws InconsistentCriteriaIdsException
  86.      */
  87.     public function search(Criteria $criteriaSalesChannelContext $salesChannelContext): EntitySearchResult
  88.     {
  89.         $criteria = clone $criteria;
  90.         $this->processCriteria($criteria$salesChannelContext);
  91.         $aggregations null;
  92.         if ($criteria->getAggregations()) {
  93.             $aggregations $this->aggregate($criteria$salesChannelContext);
  94.         }
  95.         if (!RepositorySearchDetector::isSearchRequired($this->definition$criteria)) {
  96.             $entities $this->read($criteria$salesChannelContext);
  97.             return new EntitySearchResult($this->definition->getEntityName(), $entities->count(), $entities$aggregations$criteria$salesChannelContext->getContext());
  98.         }
  99.         $ids $this->doSearch($criteria$salesChannelContext);
  100.         if (empty($ids->getIds())) {
  101.             /** @var EntityCollection<Entity> $collection */
  102.             $collection $this->definition->getCollectionClass();
  103.             return new EntitySearchResult($this->definition->getEntityName(), $ids->getTotal(), new $collection(), $aggregations$criteria$salesChannelContext->getContext());
  104.         }
  105.         $readCriteria $criteria->cloneForRead($ids->getIds());
  106.         $entities $this->read($readCriteria$salesChannelContext);
  107.         $search $ids->getData();
  108.         /** @var Entity $element */
  109.         foreach ($entities as $element) {
  110.             if (!\array_key_exists($element->getUniqueIdentifier(), $search)) {
  111.                 continue;
  112.             }
  113.             $data $search[$element->getUniqueIdentifier()];
  114.             unset($data['id']);
  115.             if (empty($data)) {
  116.                 continue;
  117.             }
  118.             $element->addExtension('search', new ArrayEntity($data));
  119.         }
  120.         $result = new EntitySearchResult($this->definition->getEntityName(), $ids->getTotal(), $entities$aggregations$criteria$salesChannelContext->getContext());
  121.         $result->addState(...$ids->getStates());
  122.         $event = new EntitySearchResultLoadedEvent($this->definition$result);
  123.         $this->eventDispatcher->dispatch($event$event->getName());
  124.         $event = new SalesChannelEntitySearchResultLoadedEvent($this->definition$result$salesChannelContext);
  125.         $this->eventDispatcher->dispatch($event$event->getName());
  126.         return $result;
  127.     }
  128.     public function aggregate(Criteria $criteriaSalesChannelContext $salesChannelContext): AggregationResultCollection
  129.     {
  130.         $criteria = clone $criteria;
  131.         $this->processCriteria($criteria$salesChannelContext);
  132.         $result $this->aggregator->aggregate($this->definition$criteria$salesChannelContext->getContext());
  133.         $event = new EntityAggregationResultLoadedEvent($this->definition$result$salesChannelContext->getContext());
  134.         $this->eventDispatcher->dispatch($event$event->getName());
  135.         return $result;
  136.     }
  137.     public function searchIds(Criteria $criteriaSalesChannelContext $salesChannelContext): IdSearchResult
  138.     {
  139.         $criteria = clone $criteria;
  140.         $this->processCriteria($criteria$salesChannelContext);
  141.         return $this->doSearch($criteria$salesChannelContext);
  142.     }
  143.     /**
  144.      * @return EntityCollection<Entity>
  145.      */
  146.     private function read(Criteria $criteriaSalesChannelContext $salesChannelContext): EntityCollection
  147.     {
  148.         $criteria = clone $criteria;
  149.         $entities $this->reader->read($this->definition$criteria$salesChannelContext->getContext());
  150.         if ($this->eventFactory === null) {
  151.             throw new \RuntimeException('Event loaded factory was not injected');
  152.         }
  153.         if (method_exists($criteria'getFields')) {
  154.             if ($criteria->getFields() === []) {
  155.                 $events $this->eventFactory->createForSalesChannel($entities->getElements(), $salesChannelContext);
  156.             } else {
  157.                 $events $this->eventFactory->createPartialForSalesChannel($entities->getElements(), $salesChannelContext);
  158.             }
  159.         } else {
  160.             $events $this->eventFactory->createForSalesChannel($entities->getElements(), $salesChannelContext);
  161.         }
  162.         foreach ($events as $event) {
  163.             $this->eventDispatcher->dispatch($event);
  164.         }
  165.         return $entities;
  166.     }
  167.     private function doSearch(Criteria $criteriaSalesChannelContext $salesChannelContext): IdSearchResult
  168.     {
  169.         $result $this->searcher->search($this->definition$criteria$salesChannelContext->getContext());
  170.         $event = new SalesChannelEntityIdSearchResultLoadedEvent($this->definition$result$salesChannelContext);
  171.         $this->eventDispatcher->dispatch($event$event->getName());
  172.         return $result;
  173.     }
  174.     private function processCriteria(Criteria $topCriteriaSalesChannelContext $salesChannelContext): void
  175.     {
  176.         if (!$this->definition instanceof SalesChannelDefinitionInterface) {
  177.             return;
  178.         }
  179.         $queue = [
  180.             ['definition' => $this->definition'criteria' => $topCriteria'path' => ''],
  181.         ];
  182.         $maxCount 100;
  183.         $processed = [];
  184.         // process all associations breadth-first
  185.         while (!empty($queue) && --$maxCount 0) {
  186.             $cur array_shift($queue);
  187.             $definition $cur['definition'];
  188.             $criteria $cur['criteria'];
  189.             $path $cur['path'];
  190.             $processedKey $path get_class($definition);
  191.             if (isset($processed[$processedKey])) {
  192.                 continue;
  193.             }
  194.             if ($definition instanceof SalesChannelDefinitionInterface) {
  195.                 if ($definition instanceof SalesChannelProductDefinition) {
  196.                     if ($path === '') {
  197.                         $definition->processCriteria($criteria$salesChannelContext);
  198.                     } else if (!$this->hasAvailableFilter($criteria)) {
  199.                         $criteria->addFilter(
  200.                             new ProductAvailableFilter($salesChannelContext->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_LINK)
  201.                         );
  202.                     }
  203.                 } else {
  204.                     $definition->processCriteria($criteria$salesChannelContext);
  205.                 }
  206.                 $eventName \sprintf('sales_channel.%s.process.criteria'$definition->getEntityName());
  207.                 $event = new SalesChannelProcessCriteriaEvent($criteria$salesChannelContext);
  208.                 $this->eventDispatcher->dispatch($event$eventName);
  209.             }
  210.             $processed[$processedKey] = true;
  211.             foreach ($criteria->getAssociations() as $associationName => $associationCriteria) {
  212.                 // find definition
  213.                 $field $definition->getField($associationName);
  214.                 if (!$field instanceof AssociationField) {
  215.                     continue;
  216.                 }
  217.                 $referenceDefinition $field->getReferenceDefinition();
  218.                 $queue[] = ['definition' => $referenceDefinition'criteria' => $associationCriteria'path' => $path '.' $associationName];
  219.                 if (!$field instanceof ManyToManyAssociationField) {
  220.                     continue;
  221.                 }
  222.                 $referenceDefinition $field->getToManyReferenceDefinition();
  223.                 $queue[] = ['definition' => $referenceDefinition'criteria' => $associationCriteria'path' => $path '.' $associationName];
  224.             }
  225.         }
  226.     }
  227.     private function hasAvailableFilter(Criteria $criteria): bool
  228.     {
  229.         foreach ($criteria->getFilters() as $filter) {
  230.             if ($filter instanceof ProductAvailableFilter) {
  231.                 return true;
  232.             }
  233.         }
  234.         return false;
  235.     }
  236. }