src/Core/System/SalesChannel/Entity/SalesChannelRepository.php line 59

  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\Log\Package;
  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
  26.  */
  27. #[Package('sales-channel')]
  28. class SalesChannelRepository
  29. {
  30.     /**
  31.      * @internal
  32.      */
  33.     public function __construct(private readonly EntityDefinition $definition, private readonly EntityReaderInterface $reader, private readonly EntitySearcherInterface $searcher, private readonly EntityAggregatorInterface $aggregator, private readonly EventDispatcherInterface $eventDispatcher, private readonly EntityLoadedEventFactory $eventFactory)
  34.     {
  35.     }
  36.     /**
  37.      * @throws InconsistentCriteriaIdsException
  38.      */
  39.     public function search(Criteria $criteriaSalesChannelContext $salesChannelContext): EntitySearchResult
  40.     {
  41.         $criteria = clone $criteria;
  42.         $this->processCriteria($criteria$salesChannelContext);
  43.         $aggregations null;
  44.         if ($criteria->getAggregations()) {
  45.             $aggregations $this->aggregate($criteria$salesChannelContext);
  46.         }
  47.         if (!RepositorySearchDetector::isSearchRequired($this->definition$criteria)) {
  48.             $entities $this->read($criteria$salesChannelContext);
  49.             return new EntitySearchResult($this->definition->getEntityName(), $entities->count(), $entities$aggregations$criteria$salesChannelContext->getContext());
  50.         }
  51.         $ids $this->doSearch($criteria$salesChannelContext);
  52.         if (empty($ids->getIds())) {
  53.             /** @var EntityCollection<Entity> $collection */
  54.             $collection $this->definition->getCollectionClass();
  55.             return new EntitySearchResult($this->definition->getEntityName(), $ids->getTotal(), new $collection(), $aggregations$criteria$salesChannelContext->getContext());
  56.         }
  57.         $readCriteria $criteria->cloneForRead($ids->getIds());
  58.         $entities $this->read($readCriteria$salesChannelContext);
  59.         $search $ids->getData();
  60.         /** @var Entity $element */
  61.         foreach ($entities as $element) {
  62.             if (!\array_key_exists($element->getUniqueIdentifier(), $search)) {
  63.                 continue;
  64.             }
  65.             $data $search[$element->getUniqueIdentifier()];
  66.             unset($data['id']);
  67.             if (empty($data)) {
  68.                 continue;
  69.             }
  70.             $element->addExtension('search', new ArrayEntity($data));
  71.         }
  72.         $result = new EntitySearchResult($this->definition->getEntityName(), $ids->getTotal(), $entities$aggregations$criteria$salesChannelContext->getContext());
  73.         $result->addState(...$ids->getStates());
  74.         $event = new EntitySearchResultLoadedEvent($this->definition$result);
  75.         $this->eventDispatcher->dispatch($event$event->getName());
  76.         $event = new SalesChannelEntitySearchResultLoadedEvent($this->definition$result$salesChannelContext);
  77.         $this->eventDispatcher->dispatch($event$event->getName());
  78.         return $result;
  79.     }
  80.     public function aggregate(Criteria $criteriaSalesChannelContext $salesChannelContext): AggregationResultCollection
  81.     {
  82.         $criteria = clone $criteria;
  83.         $this->processCriteria($criteria$salesChannelContext);
  84.         $result $this->aggregator->aggregate($this->definition$criteria$salesChannelContext->getContext());
  85.         $event = new EntityAggregationResultLoadedEvent($this->definition$result$salesChannelContext->getContext());
  86.         $this->eventDispatcher->dispatch($event$event->getName());
  87.         return $result;
  88.     }
  89.     public function searchIds(Criteria $criteriaSalesChannelContext $salesChannelContext): IdSearchResult
  90.     {
  91.         $criteria = clone $criteria;
  92.         $this->processCriteria($criteria$salesChannelContext);
  93.         return $this->doSearch($criteria$salesChannelContext);
  94.     }
  95.     /**
  96.      * @return EntityCollection<Entity>
  97.      */
  98.     private function read(Criteria $criteriaSalesChannelContext $salesChannelContext): EntityCollection
  99.     {
  100.         $criteria = clone $criteria;
  101.         $entities $this->reader->read($this->definition$criteria$salesChannelContext->getContext());
  102.         if ($criteria->getFields() === []) {
  103.             $events $this->eventFactory->createForSalesChannel($entities->getElements(), $salesChannelContext);
  104.         } else {
  105.             $events $this->eventFactory->createPartialForSalesChannel($entities->getElements(), $salesChannelContext);
  106.         }
  107.         foreach ($events as $event) {
  108.             $this->eventDispatcher->dispatch($event);
  109.         }
  110.         return $entities;
  111.     }
  112.     private function doSearch(Criteria $criteriaSalesChannelContext $salesChannelContext): IdSearchResult
  113.     {
  114.         $result $this->searcher->search($this->definition$criteria$salesChannelContext->getContext());
  115.         $event = new SalesChannelEntityIdSearchResultLoadedEvent($this->definition$result$salesChannelContext);
  116.         $this->eventDispatcher->dispatch($event$event->getName());
  117.         return $result;
  118.     }
  119.     private function processCriteria(Criteria $topCriteriaSalesChannelContext $salesChannelContext): void
  120.     {
  121.         if (!$this->definition instanceof SalesChannelDefinitionInterface) {
  122.             return;
  123.         }
  124.         $queue = [
  125.             ['definition' => $this->definition'criteria' => $topCriteria],
  126.         ];
  127.         $maxCount 100;
  128.         $processed = [];
  129.         // process all associations breadth-first
  130.         while (!empty($queue) && --$maxCount 0) {
  131.             $cur array_shift($queue);
  132.             /** @var EntityDefinition $definition */
  133.             $definition $cur['definition'];
  134.             $criteria $cur['criteria'];
  135.             if (isset($processed[$definition::class])) {
  136.                 continue;
  137.             }
  138.             if ($definition instanceof SalesChannelDefinitionInterface) {
  139.                 $definition->processCriteria($criteria$salesChannelContext);
  140.                 $eventName \sprintf('sales_channel.%s.process.criteria'$definition->getEntityName());
  141.                 $event = new SalesChannelProcessCriteriaEvent($criteria$salesChannelContext);
  142.                 $this->eventDispatcher->dispatch($event$eventName);
  143.             }
  144.             $processed[$definition::class] = true;
  145.             foreach ($criteria->getAssociations() as $associationName => $associationCriteria) {
  146.                 // find definition
  147.                 $field $definition->getField($associationName);
  148.                 if (!$field instanceof AssociationField) {
  149.                     continue;
  150.                 }
  151.                 $referenceDefinition $field->getReferenceDefinition();
  152.                 $queue[] = ['definition' => $referenceDefinition'criteria' => $associationCriteria];
  153.             }
  154.         }
  155.     }
  156. }