diff --git a/module/Core/src/Model/VisitsParams.php b/module/Core/src/Model/VisitsParams.php index e583fb73..dd8d1787 100644 --- a/module/Core/src/Model/VisitsParams.php +++ b/module/Core/src/Model/VisitsParams.php @@ -8,18 +8,30 @@ use Shlinkio\Shlink\Common\Util\DateRange; final class VisitsParams { + private const FIRST_PAGE = 1; + private const ALL_ITEMS = -1; + /** @var null|DateRange */ private $dateRange; /** @var int */ - private $page = 1; - /** @var null|int */ + private $page; + /** @var int */ 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->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 @@ -49,13 +61,8 @@ final class VisitsParams return $this->page; } - public function getItemsPerPage(): ?int + public function getItemsPerPage(): int { return $this->itemsPerPage; } - - public function hasItemsPerPage(): bool - { - return $this->itemsPerPage !== null; - } } diff --git a/module/Core/src/Service/VisitsTracker.php b/module/Core/src/Service/VisitsTracker.php index 27ccf0ea..86d671e3 100644 --- a/module/Core/src/Service/VisitsTracker.php +++ b/module/Core/src/Service/VisitsTracker.php @@ -59,7 +59,7 @@ class VisitsTracker implements VisitsTrackerInterface /** @var VisitRepository $repo */ $repo = $this->em->getRepository(Visit::class); $paginator = new Paginator(new VisitsPaginatorAdapter($repo, $shortCode, $params)); - $paginator->setItemCountPerPage($params->hasItemsPerPage() ? $params->getItemsPerPage() : -1) + $paginator->setItemCountPerPage($params->getItemsPerPage()) ->setCurrentPageNumber($params->getPage()); return $paginator; diff --git a/module/Rest/test/Action/Visit/GetVisitsActionTest.php b/module/Rest/test/Action/Visit/GetVisitsActionTest.php index c3d9f81a..9b2756f4 100644 --- a/module/Rest/test/Action/Visit/GetVisitsActionTest.php +++ b/module/Rest/test/Action/Visit/GetVisitsActionTest.php @@ -60,18 +60,24 @@ class GetVisitsActionTest extends TestCase /** * @test */ - public function datesAreReadFromQuery() + public function paramsAreReadFromQuery() { $shortCode = 'abc123'; $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([]))) ->shouldBeCalledOnce(); $response = $this->action->handle( 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()); }