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

Open in your IDE?
  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\EntityRepositoryInterface;
  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\Feature;
  10. use Shopware\Core\Framework\Log\Package;
  11. #[Package('core')]
  12. class RepositoryIterator
  13. {
  14.     /**
  15.      * @var Criteria
  16.      */
  17.     private $criteria;
  18.     /**
  19.      * @var EntityRepositoryInterface
  20.      */
  21.     private $repository;
  22.     /**
  23.      * @var Context
  24.      */
  25.     private $context;
  26.     private bool $autoIncrement false;
  27.     public function __construct(EntityRepositoryInterface $repositoryContext $context, ?Criteria $criteria null)
  28.     {
  29.         if ($criteria === null) {
  30.             $criteria = new Criteria();
  31.             $criteria->setOffset(0);
  32.         }
  33.         if ($criteria->getLimit() === null || $criteria->getLimit() < 1) {
  34.             $criteria->setLimit(50);
  35.         }
  36.         if ($repository->getDefinition()->hasAutoIncrement()) {
  37.             $criteria->addSorting(new FieldSorting('autoIncrement'FieldSorting::ASCENDING));
  38.             $criteria->setFilter('increment', new RangeFilter('autoIncrement', [RangeFilter::GTE => 0]));
  39.             $this->autoIncrement true;
  40.         }
  41.         $this->criteria $criteria;
  42.         $this->repository $repository;
  43.         $this->context = clone $context;
  44.     }
  45.     public function getTotal(): int
  46.     {
  47.         $criteria = clone $this->criteria;
  48.         $criteria->setOffset(0);
  49.         $criteria->setLimit(1);
  50.         $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
  51.         $result $this->repository->searchIds($criteria$this->context);
  52.         return $result->getTotal();
  53.     }
  54.     public function fetchIds(): ?array
  55.     {
  56.         $this->criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  57.         $ids $this->repository->searchIds($this->criteria$this->context);
  58.         $values $ids->getIds();
  59.         if (empty($values)) {
  60.             return null;
  61.         }
  62.         if (!$this->autoIncrement) {
  63.             $this->criteria->setOffset($this->criteria->getOffset() + $this->criteria->getLimit());
  64.             return $values;
  65.         }
  66.         $last end($values);
  67.         if (!\is_string($last)) {
  68.             throw new \RuntimeException('Expected string as last element of ids array');
  69.         }
  70.         $increment $ids->getDataFieldOfId($lastFeature::isActive('v6.5.0.0') ? 'autoIncrement' 'auto_increment');
  71.         $this->criteria->setFilter('increment', new RangeFilter('autoIncrement', [RangeFilter::GT => $increment]));
  72.         return $values;
  73.     }
  74.     public function fetch(): ?EntitySearchResult
  75.     {
  76.         $this->criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  77.         $result $this->repository->search(clone $this->criteria$this->context);
  78.         // increase offset for next iteration
  79.         $this->criteria->setOffset($this->criteria->getOffset() + $this->criteria->getLimit());
  80.         if (empty($result->getIds())) {
  81.             return null;
  82.         }
  83.         return $result;
  84.     }
  85. }