vendor/shopware/platform/src/Core/Framework/DataAbstractionLayer/Dbal/Common/RepositoryIterator.php line 68

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. class RepositoryIterator
  8. {
  9.     /**
  10.      * @var Criteria
  11.      */
  12.     private $criteria;
  13.     /**
  14.      * @var EntityRepositoryInterface
  15.      */
  16.     private $repository;
  17.     /**
  18.      * @var Context
  19.      */
  20.     private $context;
  21.     public function __construct(EntityRepositoryInterface $repositoryContext $context, ?Criteria $criteria null)
  22.     {
  23.         if ($criteria === null) {
  24.             $criteria = new Criteria();
  25.             $criteria->setOffset(0);
  26.         }
  27.         if ($criteria->getLimit() === null || $criteria->getLimit() < 1) {
  28.             $criteria->setLimit(50);
  29.         }
  30.         $this->criteria $criteria;
  31.         $this->repository $repository;
  32.         $this->context = clone $context;
  33.     }
  34.     public function getTotal(): int
  35.     {
  36.         $criteria = clone $this->criteria;
  37.         $criteria->setOffset(0);
  38.         $criteria->setLimit(1);
  39.         $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
  40.         $result $this->repository->searchIds($criteria$this->context);
  41.         return $result->getTotal();
  42.     }
  43.     public function fetchIds(): ?array
  44.     {
  45.         $this->criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  46.         $ids $this->repository->searchIds($this->criteria$this->context);
  47.         $this->criteria->setOffset($this->criteria->getOffset() + $this->criteria->getLimit());
  48.         if (!empty($ids->getIds())) {
  49.             return $ids->getIds();
  50.         }
  51.         return null;
  52.     }
  53.     public function fetch(): ?EntitySearchResult
  54.     {
  55.         $this->criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_NONE);
  56.         $result $this->repository->search(clone $this->criteria$this->context);
  57.         // increase offset for next iteration
  58.         $this->criteria->setOffset($this->criteria->getOffset() + $this->criteria->getLimit());
  59.         if (empty($result->getIds())) {
  60.             return null;
  61.         }
  62.         return $result;
  63.     }
  64. }