src/Core/Framework/DataAbstractionLayer/Dbal/Common/RepositoryIterator.php line 91

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  9. use Shopware\Core\Framework\Log\Package;
  10. #[Package('core')]
  11. class RepositoryIterator
  12. {
  13.     private readonly Criteria $criteria;
  14.     private readonly EntityRepository $repository;
  15.     private readonly Context $context;
  16.     private bool $autoIncrement false;
  17.     public function __construct(EntityRepository $repositoryContext $context, ?Criteria $criteria null)
  18.     {
  19.         if ($criteria === null) {
  20.             $criteria = new Criteria();
  21.             $criteria->setOffset(0);
  22.         }
  23.         if ($criteria->getLimit() === null || $criteria->getLimit() < 1) {
  24.             $criteria->setLimit(50);
  25.         }
  26.         if ($repository->getDefinition()->hasAutoIncrement()) {
  27.             $criteria->addSorting(new FieldSorting('autoIncrement'FieldSorting::ASCENDING));
  28.             $criteria->setFilter('increment', new RangeFilter('autoIncrement', [RangeFilter::GTE => 0]));
  29.             $this->autoIncrement true;
  30.         }
  31.         $this->criteria $criteria;
  32.         $this->repository $repository;
  33.         $this->context = clone $context;
  34.     }
  35.     public function getTotal(): int
  36.     {
  37.         $criteria = clone $this->criteria;
  38.         $criteria->setOffset(0);
  39.         $criteria->setLimit(1);
  40.         $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
  41.         $result $this->repository->searchIds($criteria$this->context);
  42.         return $result->getTotal();
  43.     }
  44.     public function fetchIds(): ?array
  45.     {
  46.         $this->criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  47.         $ids $this->repository->searchIds($this->criteria$this->context);
  48.         $values $ids->getIds();
  49.         if (empty($values)) {
  50.             return null;
  51.         }
  52.         if (!$this->autoIncrement) {
  53.             $this->criteria->setOffset($this->criteria->getOffset() + $this->criteria->getLimit());
  54.             return $values;
  55.         }
  56.         $last end($values);
  57.         if (!\is_string($last)) {
  58.             throw new \RuntimeException('Expected string as last element of ids array');
  59.         }
  60.         $increment $ids->getDataFieldOfId($last'autoIncrement');
  61.         $this->criteria->setFilter('increment', new RangeFilter('autoIncrement', [RangeFilter::GT => $increment]));
  62.         return $values;
  63.     }
  64.     public function fetch(): ?EntitySearchResult
  65.     {
  66.         $this->criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  67.         $result $this->repository->search(clone $this->criteria$this->context);
  68.         // increase offset for next iteration
  69.         $this->criteria->setOffset($this->criteria->getOffset() + $this->criteria->getLimit());
  70.         if (empty($result->getIds())) {
  71.             return null;
  72.         }
  73.         return $result;
  74.     }
  75. }