Simplified implementation iterating unlocated visits

This commit is contained in:
Alejandro Celaya 2019-02-23 07:29:07 +01:00
parent 955ae00036
commit 091ea974eb
4 changed files with 13 additions and 147 deletions

View File

@ -1,34 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Paginator\Adapter;
use Doctrine\ORM\Query;
use Zend\Paginator\Adapter\AdapterInterface;
class PaginableQueryAdapter implements AdapterInterface
{
/** @var Query */
private $query;
/** @var int */
private $totalItems;
public function __construct(Query $query, int $totalItems)
{
$this->query = $query;
$this->totalItems = $totalItems;
}
public function getItems($offset, $itemCountPerPage): iterable
{
return $this->query
->setMaxResults($itemCountPerPage)
->setFirstResult($offset)
->iterate();
}
public function count(): int
{
return $this->totalItems;
}
}

View File

@ -1,38 +0,0 @@
<?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);
}
}

View File

@ -1,65 +0,0 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Paginator;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Paginator\ImplicitLoopPaginator;
use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator;
use function Functional\map;
use function range;
class ImplicitLoopPaginatorTest extends TestCase
{
private const TOTAL_ITEMS = 10;
/** @var Paginator */
private $paginator;
protected function setUp(): void
{
$this->paginator = new Paginator(new ArrayAdapter(range(1, self::TOTAL_ITEMS)));
}
/**
* @test
* @dataProvider provideItemsPerPage
*/
public function allElementsAreIteratedRegardlessThePageSize(int $itemsPerPage): void
{
$this->paginator->setItemCountPerPage($itemsPerPage);
$implicitLoopPaginator = new ImplicitLoopPaginator($this->paginator);
$iteratedItems = 0;
foreach ($implicitLoopPaginator as $item) {
$iteratedItems++;
}
$this->assertEquals(self::TOTAL_ITEMS, $iteratedItems);
}
public function provideItemsPerPage(): iterable
{
return map(range(1, 20), function (int $i) {
return [$i];
});
}
/** @test */
public function valuesWrappedInPaginatorAreProperlyParsed(): void
{
$valueParser = function (int $item) {
return $item * 3;
};
$this->paginator->setItemCountPerPage(5);
$implicitLoopPaginator = new ImplicitLoopPaginator($this->paginator, $valueParser);
$items = [];
foreach ($implicitLoopPaginator as $item) {
$items[] = $item;
}
$this->assertEquals([3, 6, 9, 12, 15, 18, 21, 24, 27, 30], $items);
}
}

View File

@ -5,12 +5,8 @@ namespace Shlinkio\Shlink\Core\Repository;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableQueryAdapter;
use Shlinkio\Shlink\Common\Paginator\ImplicitLoopPaginator;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Entity\Visit;
use Zend\Paginator\Paginator;
use function array_shift;
class VisitRepository extends EntityRepository implements VisitRepositoryInterface
{
@ -19,18 +15,25 @@ class VisitRepository extends EntityRepository implements VisitRepositoryInterfa
*/
public function findUnlocatedVisits(int $blockSize = self::DEFAULT_BLOCK_SIZE): iterable
{
$count = $this->count(['visitLocation' => null]);
$dql = <<<DQL
SELECT v FROM Shlinkio\Shlink\Core\Entity\Visit AS v WHERE v.visitLocation IS NULL
DQL;
$query = $this->getEntityManager()->createQuery($dql);
$remainingVisitsToProcess = $this->count(['visitLocation' => null]);
$offset = 0;
$paginator = new Paginator(new PaginableQueryAdapter($query, $count));
$paginator->setItemCountPerPage($blockSize);
while ($remainingVisitsToProcess > 0) {
$iterator = $query->setMaxResults($blockSize)
->setFirstResult($offset)
->iterate();
return new ImplicitLoopPaginator($paginator, function (array $value) {
return array_shift($value);
});
foreach ($iterator as $key => [$value]) {
yield $key => $value;
}
$remainingVisitsToProcess -= $blockSize;
$offset += $blockSize;
}
}
/**