mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-23 07:33:58 -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
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user