Updated VisitRepository::findUnlocatedVisits methods so that it paginates the amount of elements loaded in memory

This commit is contained in:
Alejandro Celaya
2019-02-22 19:31:03 +01:00
parent 08bd4f131c
commit 292937b962
7 changed files with 115 additions and 11 deletions

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Paginator;
use IteratorAggregate;
use Zend\Paginator\Paginator;
class ImplicitLoopPaginator implements IteratorAggregate
{
/** @var Paginator */
private $paginator;
/** @var callable */
private $valueParser;
public function __construct(Paginator $paginator, callable $valueParser = null)
{
$this->paginator = $paginator;
$this->valueParser = $valueParser ?? function ($value) {
return $value;
};
}
public function getIterator(): iterable
{
$totalPages = $this->paginator->count();
$processedPages = 0;
do {
$processedPages++;
$this->paginator->setCurrentPageNumber($processedPages);
foreach ($this->paginator as $key => $value) {
yield $key => ($this->valueParser)($value);
}
} while ($processedPages < $totalPages);
}
}