vendor/shopware/core/Framework/DataAbstractionLayer/EntityRepository.php line 255

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer;
  3. use Shopware\Core\Framework\Adapter\Database\ReplicaConnection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityAggregationResultLoadedEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityIdSearchResultLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEventFactory;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Read\EntityReaderInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntityAggregatorInterface;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearcherInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Write\CloneBehavior;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteContext;
  20. use Shopware\Core\Framework\Feature;
  21. use Shopware\Core\Framework\Struct\ArrayEntity;
  22. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  23. use Shopware\Core\Framework\Uuid\Uuid;
  24. use Shopware\Core\Profiling\Profiler;
  25. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  26. /**
  27.  * @final tag:v6.5.0
  28.  */
  29. class EntityRepository implements EntityRepositoryInterface
  30. {
  31.     private EntityReaderInterface $reader;
  32.     private EntitySearcherInterface $searcher;
  33.     private EntityAggregatorInterface $aggregator;
  34.     private EventDispatcherInterface $eventDispatcher;
  35.     private VersionManager $versionManager;
  36.     private EntityDefinition $definition;
  37.     private ?EntityLoadedEventFactory $eventFactory null;
  38.     /**
  39.      * @internal
  40.      *
  41.      * @deprecated tag:v6.5.0 - parameter $eventFactory will be required
  42.      */
  43.     public function __construct(
  44.         EntityDefinition $definition,
  45.         EntityReaderInterface $reader,
  46.         VersionManager $versionManager,
  47.         EntitySearcherInterface $searcher,
  48.         EntityAggregatorInterface $aggregator,
  49.         EventDispatcherInterface $eventDispatcher,
  50.         ?EntityLoadedEventFactory $eventFactory null
  51.     ) {
  52.         $this->reader $reader;
  53.         $this->searcher $searcher;
  54.         $this->aggregator $aggregator;
  55.         $this->eventDispatcher $eventDispatcher;
  56.         $this->versionManager $versionManager;
  57.         $this->definition $definition;
  58.         if ($eventFactory !== null) {
  59.             $this->eventFactory $eventFactory;
  60.         } else {
  61.             Feature::triggerDeprecationOrThrow(
  62.                 'v6.5.0.0',
  63.                 sprintf('EntityRepository constructor for definition %s requires the event factory as required 7th parameter in v6.5.0.0'$definition->getEntityName())
  64.             );
  65.         }
  66.     }
  67.     /**
  68.      * @deprecated tag:v6.5.0 - Will be removed, inject entity loaded event factory in __construct
  69.      */
  70.     public function setEntityLoadedEventFactory(EntityLoadedEventFactory $eventFactory): void
  71.     {
  72.         if (isset($this->eventFactory)) {
  73.             return;
  74.         }
  75.         Feature::triggerDeprecationOrThrow(
  76.             'v6.5.0.0',
  77.             sprintf('Repository for definition %s requires the event factory as __construct parameter'$this->definition->getEntityName())
  78.         );
  79.         $this->eventFactory $eventFactory;
  80.     }
  81.     public function getDefinition(): EntityDefinition
  82.     {
  83.         return $this->definition;
  84.     }
  85.     public function search(Criteria $criteriaContext $context): EntitySearchResult
  86.     {
  87.         if (!$criteria->getTitle()) {
  88.             return $this->_search($criteria$context);
  89.         }
  90.         return Profiler::trace($criteria->getTitle(), function () use ($criteria$context) {
  91.             return $this->_search($criteria$context);
  92.         }, 'repository');
  93.     }
  94.     public function aggregate(Criteria $criteriaContext $context): AggregationResultCollection
  95.     {
  96.         $criteria = clone $criteria;
  97.         $result $this->aggregator->aggregate($this->definition$criteria$context);
  98.         $event = new EntityAggregationResultLoadedEvent($this->definition$result$context);
  99.         $this->eventDispatcher->dispatch($event$event->getName());
  100.         return $result;
  101.     }
  102.     public function searchIds(Criteria $criteriaContext $context): IdSearchResult
  103.     {
  104.         $criteria = clone $criteria;
  105.         $this->eventDispatcher->dispatch(new EntitySearchedEvent($criteria$this->definition$context));
  106.         $result $this->searcher->search($this->definition$criteria$context);
  107.         $event = new EntityIdSearchResultLoadedEvent($this->definition$result);
  108.         $this->eventDispatcher->dispatch($event$event->getName());
  109.         return $result;
  110.     }
  111.     /**
  112.      * @param array<array<string, mixed|null>> $data
  113.      */
  114.     public function update(array $dataContext $context): EntityWrittenContainerEvent
  115.     {
  116.         ReplicaConnection::ensurePrimary();
  117.         $affected $this->versionManager->update($this->definition$dataWriteContext::createFromContext($context));
  118.         $event EntityWrittenContainerEvent::createWithWrittenEvents($affected$context, []);
  119.         $this->eventDispatcher->dispatch($event);
  120.         return $event;
  121.     }
  122.     /**
  123.      * @param array<array<string, mixed|null>> $data
  124.      */
  125.     public function upsert(array $dataContext $context): EntityWrittenContainerEvent
  126.     {
  127.         ReplicaConnection::ensurePrimary();
  128.         $affected $this->versionManager->upsert($this->definition$dataWriteContext::createFromContext($context));
  129.         $event EntityWrittenContainerEvent::createWithWrittenEvents($affected$context, []);
  130.         $this->eventDispatcher->dispatch($event);
  131.         return $event;
  132.     }
  133.     /**
  134.      * @param array<array<string, mixed|null>> $data
  135.      */
  136.     public function create(array $dataContext $context): EntityWrittenContainerEvent
  137.     {
  138.         ReplicaConnection::ensurePrimary();
  139.         $affected $this->versionManager->insert($this->definition$dataWriteContext::createFromContext($context));
  140.         $event EntityWrittenContainerEvent::createWithWrittenEvents($affected$context, []);
  141.         $this->eventDispatcher->dispatch($event);
  142.         return $event;
  143.     }
  144.     /**
  145.      * @param array<array<string, mixed|null>> $ids
  146.      */
  147.     public function delete(array $idsContext $context): EntityWrittenContainerEvent
  148.     {
  149.         ReplicaConnection::ensurePrimary();
  150.         $affected $this->versionManager->delete($this->definition$idsWriteContext::createFromContext($context));
  151.         $event EntityWrittenContainerEvent::createWithDeletedEvents($affected->getDeleted(), $context$affected->getNotFound());
  152.         if ($affected->getWritten()) {
  153.             $updates EntityWrittenContainerEvent::createWithWrittenEvents($affected->getWritten(), $context, []);
  154.             if ($updates->getEvents() !== null) {
  155.                 $event->addEvent(...$updates->getEvents());
  156.             }
  157.         }
  158.         $this->eventDispatcher->dispatch($event);
  159.         return $event;
  160.     }
  161.     public function createVersion(string $idContext $context, ?string $name null, ?string $versionId null): string
  162.     {
  163.         ReplicaConnection::ensurePrimary();
  164.         if (!$this->definition->isVersionAware()) {
  165.             throw new \RuntimeException(sprintf('Entity %s is not version aware'$this->definition->getEntityName()));
  166.         }
  167.         return $this->versionManager->createVersion($this->definition$idWriteContext::createFromContext($context), $name$versionId);
  168.     }
  169.     public function merge(string $versionIdContext $context): void
  170.     {
  171.         ReplicaConnection::ensurePrimary();
  172.         if (!$this->definition->isVersionAware()) {
  173.             throw new \RuntimeException(sprintf('Entity %s is not version aware'$this->definition->getEntityName()));
  174.         }
  175.         $this->versionManager->merge($versionIdWriteContext::createFromContext($context));
  176.     }
  177.     public function clone(string $idContext $context, ?string $newId null, ?CloneBehavior $behavior null): EntityWrittenContainerEvent
  178.     {
  179.         ReplicaConnection::ensurePrimary();
  180.         $newId $newId ?? Uuid::randomHex();
  181.         if (!Uuid::isValid($newId)) {
  182.             throw new InvalidUuidException($newId);
  183.         }
  184.         $affected $this->versionManager->clone(
  185.             $this->definition,
  186.             $id,
  187.             $newId,
  188.             $context->getVersionId(),
  189.             WriteContext::createFromContext($context),
  190.             $behavior ?? new CloneBehavior()
  191.         );
  192.         $event EntityWrittenContainerEvent::createWithWrittenEvents($affected$context, [], true);
  193.         $this->eventDispatcher->dispatch($event);
  194.         return $event;
  195.     }
  196.     /**
  197.      * @return EntityCollection<Entity>
  198.      */
  199.     private function read(Criteria $criteriaContext $context): EntityCollection
  200.     {
  201.         $criteria = clone $criteria;
  202.         $entities $this->reader->read($this->definition$criteria$context);
  203.         if ($this->eventFactory === null) {
  204.             throw new \RuntimeException('Event loaded factory was not injected');
  205.         }
  206.         if ($criteria->getFields() === []) {
  207.             $event $this->eventFactory->create($entities->getElements(), $context);
  208.         } else {
  209.             $event $this->eventFactory->createPartial($entities->getElements(), $context);
  210.         }
  211.         $this->eventDispatcher->dispatch($event);
  212.         return $entities;
  213.     }
  214.     private function _search(Criteria $criteriaContext $context): EntitySearchResult
  215.     {
  216.         $criteria = clone $criteria;
  217.         $aggregations null;
  218.         if ($criteria->getAggregations()) {
  219.             $aggregations $this->aggregate($criteria$context);
  220.         }
  221.         if (!RepositorySearchDetector::isSearchRequired($this->definition$criteria)) {
  222.             $this->eventDispatcher->dispatch(
  223.                 new EntitySearchedEvent($criteria$this->definition$context)
  224.             );
  225.             $entities $this->read($criteria$context);
  226.             return new EntitySearchResult($this->definition->getEntityName(), $entities->count(), $entities$aggregations$criteria$context);
  227.         }
  228.         $ids $this->searchIds($criteria$context);
  229.         if (empty($ids->getIds())) {
  230.             /** @var EntityCollection<Entity> $collection */
  231.             $collection $this->definition->getCollectionClass();
  232.             return new EntitySearchResult($this->definition->getEntityName(), $ids->getTotal(), new $collection(), $aggregations$criteria$context);
  233.         }
  234.         $readCriteria $criteria->cloneForRead($ids->getIds());
  235.         $entities $this->read($readCriteria$context);
  236.         $search $ids->getData();
  237.         /** @var Entity $element */
  238.         foreach ($entities as $element) {
  239.             if (!\array_key_exists($element->getUniqueIdentifier(), $search)) {
  240.                 continue;
  241.             }
  242.             $data $search[$element->getUniqueIdentifier()];
  243.             unset($data['id']);
  244.             if (empty($data)) {
  245.                 continue;
  246.             }
  247.             $element->addExtension('search', new ArrayEntity($data));
  248.         }
  249.         $result = new EntitySearchResult($this->definition->getEntityName(), $ids->getTotal(), $entities$aggregations$criteria$context);
  250.         $result->addState(...$ids->getStates());
  251.         $event = new EntitySearchResultLoadedEvent($this->definition$result);
  252.         $this->eventDispatcher->dispatch($event$event->getName());
  253.         return $result;
  254.     }
  255. }