vendor/shopware/core/Framework/DataAbstractionLayer/Dbal/Common/RepositoryIterator.php line 94

  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(
  18.         EntityRepository $repository,
  19.         Context $context,
  20.         ?Criteria $criteria null
  21.     ) {
  22.         if ($criteria === null) {
  23.             $criteria = new Criteria();
  24.             $criteria->setOffset(0);
  25.         }
  26.         if ($criteria->getLimit() === null || $criteria->getLimit() < 1) {
  27.             $criteria->setLimit(50);
  28.         }
  29.         if ($repository->getDefinition()->hasAutoIncrement()) {
  30.             $criteria->addSorting(new FieldSorting('autoIncrement'FieldSorting::ASCENDING));
  31.             $criteria->setFilter('increment', new RangeFilter('autoIncrement', [RangeFilter::GTE => 0]));
  32.             $this->autoIncrement true;
  33.         }
  34.         $this->criteria $criteria;
  35.         $this->repository $repository;
  36.         $this->context = clone $context;
  37.     }
  38.     public function getTotal(): int
  39.     {
  40.         $criteria = clone $this->criteria;
  41.         $criteria->setOffset(0);
  42.         $criteria->setLimit(1);
  43.         $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
  44.         $result $this->repository->searchIds($criteria$this->context);
  45.         return $result->getTotal();
  46.     }
  47.     public function fetchIds(): ?array
  48.     {
  49.         $this->criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  50.         $ids $this->repository->searchIds($this->criteria$this->context);
  51.         $values $ids->getIds();
  52.         if (empty($values)) {
  53.             return null;
  54.         }
  55.         if (!$this->autoIncrement) {
  56.             $this->criteria->setOffset($this->criteria->getOffset() + $this->criteria->getLimit());
  57.             return $values;
  58.         }
  59.         $last end($values);
  60.         if (!\is_string($last)) {
  61.             throw new \RuntimeException('Expected string as last element of ids array');
  62.         }
  63.         $increment $ids->getDataFieldOfId($last'autoIncrement');
  64.         $this->criteria->setFilter('increment', new RangeFilter('autoIncrement', [RangeFilter::GT => $increment]));
  65.         return $values;
  66.     }
  67.     public function fetch(): ?EntitySearchResult
  68.     {
  69.         $this->criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  70.         $result $this->repository->search(clone $this->criteria$this->context);
  71.         // increase offset for next iteration
  72.         $this->criteria->setOffset($this->criteria->getOffset() + $this->criteria->getLimit());
  73.         if (empty($result->getIds())) {
  74.             return null;
  75.         }
  76.         return $result;
  77.     }
  78. }