custom/plugins/SwagPlatformSecurity/src/Fixes/NEXT37527/SalesChannelRepository.php line 99

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\EntityLoadedEvent;
  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 $eventFactory null;
  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.         $eventFactory null
  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($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.         if (method_exists($result'addState')) {
  122.             $result->addState(...$ids->getStates());
  123.         }
  124.         $event = new EntitySearchResultLoadedEvent($this->definition$result);
  125.         $this->eventDispatcher->dispatch($event$event->getName());
  126.         $event = new SalesChannelEntitySearchResultLoadedEvent($this->definition$result$salesChannelContext);
  127.         $this->eventDispatcher->dispatch($event$event->getName());
  128.         return $result;
  129.     }
  130.     public function aggregate(Criteria $criteriaSalesChannelContext $salesChannelContext): AggregationResultCollection
  131.     {
  132.         $criteria = clone $criteria;
  133.         $this->processCriteria($criteria$salesChannelContext);
  134.         $result $this->aggregator->aggregate($this->definition$criteria$salesChannelContext->getContext());
  135.         $event = new EntityAggregationResultLoadedEvent($this->definition$result$salesChannelContext->getContext());
  136.         $this->eventDispatcher->dispatch($event$event->getName());
  137.         return $result;
  138.     }
  139.     public function searchIds(Criteria $criteriaSalesChannelContext $salesChannelContext): IdSearchResult
  140.     {
  141.         $criteria = clone $criteria;
  142.         $this->processCriteria($criteria$salesChannelContext);
  143.         return $this->doSearch($criteria$salesChannelContext);
  144.     }
  145.     /**
  146.      * @return EntityCollection<Entity>
  147.      */
  148.     private function read(Criteria $criteriaSalesChannelContext $salesChannelContext): EntityCollection
  149.     {
  150.         $criteria = clone $criteria;
  151.         $entities $this->reader->read($this->definition$criteria$salesChannelContext->getContext());
  152.         if ($this->eventFactory === null) {
  153.             $event = new EntityLoadedEvent($this->definition$entities->getElements(), $salesChannelContext->getContext());
  154.             $this->eventDispatcher->dispatch($event$event->getName());
  155.     
  156.             $event = new SalesChannelEntityLoadedEvent($this->definition$entities->getElements(), $salesChannelContext);
  157.             $this->eventDispatcher->dispatch($event$event->getName());
  158.             return $entities;
  159.         }
  160.         if (method_exists($criteria'getFields')) {
  161.             if ($criteria->getFields() === []) {
  162.                 $events $this->eventFactory->createForSalesChannel($entities->getElements(), $salesChannelContext);
  163.             } else {
  164.                 $events $this->eventFactory->createPartialForSalesChannel($entities->getElements(), $salesChannelContext);
  165.             }
  166.         } else {
  167.             $events $this->eventFactory->createForSalesChannel($entities->getElements(), $salesChannelContext);
  168.         }
  169.         foreach ($events as $event) {
  170.             $this->eventDispatcher->dispatch($event);
  171.         }
  172.         return $entities;
  173.     }
  174.     private function doSearch(Criteria $criteriaSalesChannelContext $salesChannelContext): IdSearchResult
  175.     {
  176.         $result $this->searcher->search($this->definition$criteria$salesChannelContext->getContext());
  177.         $event = new SalesChannelEntityIdSearchResultLoadedEvent($this->definition$result$salesChannelContext);
  178.         $this->eventDispatcher->dispatch($event$event->getName());
  179.         return $result;
  180.     }
  181.     private function processCriteria(Criteria $topCriteriaSalesChannelContext $salesChannelContext): void
  182.     {
  183.         if (!$this->definition instanceof SalesChannelDefinitionInterface) {
  184.             return;
  185.         }
  186.         $queue = [
  187.             ['definition' => $this->definition'criteria' => $topCriteria'path' => ''],
  188.         ];
  189.         $maxCount 100;
  190.         $processed = [];
  191.         // process all associations breadth-first
  192.         while (!empty($queue) && --$maxCount 0) {
  193.             $cur array_shift($queue);
  194.             $definition $cur['definition'];
  195.             $criteria $cur['criteria'];
  196.             $path $cur['path'];
  197.             $processedKey $path get_class($definition);
  198.             if (isset($processed[$processedKey])) {
  199.                 continue;
  200.             }
  201.             if ($definition instanceof SalesChannelDefinitionInterface) {
  202.                 if ($definition instanceof SalesChannelProductDefinition) {
  203.                     if ($path === '') {
  204.                         $definition->processCriteria($criteria$salesChannelContext);
  205.                     } else if (!$this->hasAvailableFilter($criteria)) {
  206.                         $criteria->addFilter(
  207.                             new ProductAvailableFilter($salesChannelContext->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_LINK)
  208.                         );
  209.                     }
  210.                 } else {
  211.                     $definition->processCriteria($criteria$salesChannelContext);
  212.                 }
  213.                 $eventName = \sprintf('sales_channel.%s.process.criteria'$definition->getEntityName());
  214.                 if (class_exists(SalesChannelProcessCriteriaEvent::class)) {
  215.                     $event = new SalesChannelProcessCriteriaEvent($criteria$salesChannelContext);
  216.                     $this->eventDispatcher->dispatch($event$eventName);
  217.                 }
  218.             }
  219.             $processed[$processedKey] = true;
  220.             foreach ($criteria->getAssociations() as $associationName => $associationCriteria) {
  221.                 // find definition
  222.                 $field $definition->getField($associationName);
  223.                 if (!$field instanceof AssociationField) {
  224.                     continue;
  225.                 }
  226.                 $referenceDefinition $field->getReferenceDefinition();
  227.                 $queue[] = ['definition' => $referenceDefinition'criteria' => $associationCriteria'path' => $path '.' $associationName];
  228.                 if (!$field instanceof ManyToManyAssociationField) {
  229.                     continue;
  230.                 }
  231.                 $referenceDefinition $field->getToManyReferenceDefinition();
  232.                 $queue[] = ['definition' => $referenceDefinition'criteria' => $associationCriteria'path' => $path '.' $associationName];
  233.             }
  234.         }
  235.     }
  236.     private function hasAvailableFilter(Criteria $criteria): bool
  237.     {
  238.         foreach ($criteria->getFilters() as $filter) {
  239.             if ($filter instanceof ProductAvailableFilter) {
  240.                 return true;
  241.             }
  242.         }
  243.         return false;
  244.     }
  245. }