vendor/shopware/core/System/SalesChannel/Entity/SalesChannelRepository.php line 193

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