mirror of
https://github.com/shlinkio/shlink.git
synced 2025-01-11 00:22:04 -06:00
Encapsulated in VisitsParams how the itemsPerPage param is handled
This commit is contained in:
parent
05e56cc845
commit
b876870bd8
@ -8,18 +8,30 @@ use Shlinkio\Shlink\Common\Util\DateRange;
|
|||||||
|
|
||||||
final class VisitsParams
|
final class VisitsParams
|
||||||
{
|
{
|
||||||
|
private const FIRST_PAGE = 1;
|
||||||
|
private const ALL_ITEMS = -1;
|
||||||
|
|
||||||
/** @var null|DateRange */
|
/** @var null|DateRange */
|
||||||
private $dateRange;
|
private $dateRange;
|
||||||
/** @var int */
|
/** @var int */
|
||||||
private $page = 1;
|
private $page;
|
||||||
/** @var null|int */
|
/** @var int */
|
||||||
private $itemsPerPage;
|
private $itemsPerPage;
|
||||||
|
|
||||||
public function __construct(?DateRange $dateRange = null, int $page = 1, ?int $itemsPerPage = null)
|
public function __construct(?DateRange $dateRange = null, int $page = self::FIRST_PAGE, ?int $itemsPerPage = null)
|
||||||
{
|
{
|
||||||
$this->dateRange = $dateRange ?? new DateRange();
|
$this->dateRange = $dateRange ?? new DateRange();
|
||||||
$this->page = $page;
|
$this->page = $page;
|
||||||
$this->itemsPerPage = $itemsPerPage;
|
$this->itemsPerPage = $this->determineItemsPerPage($itemsPerPage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function determineItemsPerPage(?int $itemsPerPage): int
|
||||||
|
{
|
||||||
|
if ($itemsPerPage !== null && $itemsPerPage < 0) {
|
||||||
|
return self::ALL_ITEMS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $itemsPerPage ?? self::ALL_ITEMS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromRawData(array $query): self
|
public static function fromRawData(array $query): self
|
||||||
@ -49,13 +61,8 @@ final class VisitsParams
|
|||||||
return $this->page;
|
return $this->page;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getItemsPerPage(): ?int
|
public function getItemsPerPage(): int
|
||||||
{
|
{
|
||||||
return $this->itemsPerPage;
|
return $this->itemsPerPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasItemsPerPage(): bool
|
|
||||||
{
|
|
||||||
return $this->itemsPerPage !== null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ class VisitsTracker implements VisitsTrackerInterface
|
|||||||
/** @var VisitRepository $repo */
|
/** @var VisitRepository $repo */
|
||||||
$repo = $this->em->getRepository(Visit::class);
|
$repo = $this->em->getRepository(Visit::class);
|
||||||
$paginator = new Paginator(new VisitsPaginatorAdapter($repo, $shortCode, $params));
|
$paginator = new Paginator(new VisitsPaginatorAdapter($repo, $shortCode, $params));
|
||||||
$paginator->setItemCountPerPage($params->hasItemsPerPage() ? $params->getItemsPerPage() : -1)
|
$paginator->setItemCountPerPage($params->getItemsPerPage())
|
||||||
->setCurrentPageNumber($params->getPage());
|
->setCurrentPageNumber($params->getPage());
|
||||||
|
|
||||||
return $paginator;
|
return $paginator;
|
||||||
|
@ -60,18 +60,24 @@ class GetVisitsActionTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function datesAreReadFromQuery()
|
public function paramsAreReadFromQuery()
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->visitsTracker->info($shortCode, new VisitsParams(
|
$this->visitsTracker->info($shortCode, new VisitsParams(
|
||||||
new DateRange(null, Chronos::parse('2016-01-01 00:00:00'))
|
new DateRange(null, Chronos::parse('2016-01-01 00:00:00')),
|
||||||
|
3,
|
||||||
|
10
|
||||||
))
|
))
|
||||||
->willReturn(new Paginator(new ArrayAdapter([])))
|
->willReturn(new Paginator(new ArrayAdapter([])))
|
||||||
->shouldBeCalledOnce();
|
->shouldBeCalledOnce();
|
||||||
|
|
||||||
$response = $this->action->handle(
|
$response = $this->action->handle(
|
||||||
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
|
||||||
->withQueryParams(['endDate' => '2016-01-01 00:00:00'])
|
->withQueryParams([
|
||||||
|
'endDate' => '2016-01-01 00:00:00',
|
||||||
|
'page' => '3',
|
||||||
|
'itemsPerPage' => '10',
|
||||||
|
])
|
||||||
);
|
);
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user