VisitsParams::getPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Model;
6
7
use Shlinkio\Shlink\Common\Util\DateRange;
8
9
use function Shlinkio\Shlink\Core\parseDateFromQuery;
10
11
final class VisitsParams
12
{
13
    private const FIRST_PAGE = 1;
14
    private const ALL_ITEMS = -1;
15
16
    private ?DateRange $dateRange;
17
    private int $page;
18
    private int $itemsPerPage;
19
20 16
    public function __construct(?DateRange $dateRange = null, int $page = self::FIRST_PAGE, ?int $itemsPerPage = null)
21
    {
22 16
        $this->dateRange = $dateRange ?? new DateRange();
23 16
        $this->page = $page;
24 16
        $this->itemsPerPage = $this->determineItemsPerPage($itemsPerPage);
25 16
    }
26
27 16
    private function determineItemsPerPage(?int $itemsPerPage): int
28
    {
29 16
        if ($itemsPerPage !== null && $itemsPerPage < 0) {
30
            return self::ALL_ITEMS;
31
        }
32
33 16
        return $itemsPerPage ?? self::ALL_ITEMS;
34
    }
35
36 8
    public static function fromRawData(array $query): self
37
    {
38 8
        return new self(
39 8
            new DateRange(parseDateFromQuery($query, 'startDate'), parseDateFromQuery($query, 'endDate')),
40 8
            (int) ($query['page'] ?? 1),
41 8
            isset($query['itemsPerPage']) ? (int) $query['itemsPerPage'] : null,
42
        );
43
    }
44
45 7
    public function getDateRange(): DateRange
46
    {
47 7
        return $this->dateRange;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dateRange could return the type null which is incompatible with the type-hinted return Shlinkio\Shlink\Common\Util\DateRange. Consider adding an additional type-check to rule them out.
Loading history...
48
    }
49
50 3
    public function getPage(): int
51
    {
52 3
        return $this->page;
53
    }
54
55 3
    public function getItemsPerPage(): int
56
    {
57 3
        return $this->itemsPerPage;
58
    }
59
}
60