Passed
Pull Request — master (#293)
by Alejandro
04:32
created

VisitsParams::fromRawData()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Model;
5
6
use Cake\Chronos\Chronos;
7
use Shlinkio\Shlink\Common\Util\DateRange;
8
9
final class VisitsParams
10
{
11
    /** @var null|DateRange */
12
    private $dateRange;
13
    /** @var int */
14
    private $page = 1;
15
    /** @var null|int */
16
    private $itemsPerPage;
17
18 7
    public function __construct(?DateRange $dateRange = null, int $page = 1, ?int $itemsPerPage = null)
19
    {
20 7
        $this->dateRange = $dateRange ?? new DateRange();
21 7
        $this->page = $page;
22 7
        $this->itemsPerPage = $itemsPerPage;
23
    }
24
25 3
    public static function fromRawData(array $query): self
26
    {
27 3
        $startDate = self::getDateQueryParam($query, 'startDate');
28 3
        $endDate = self::getDateQueryParam($query, 'endDate');
29
30 3
        return new self(
31 3
            new DateRange($startDate, $endDate),
32 3
            (int) ($query['page'] ?? 1),
33 3
            isset($query['itemsPerPage']) ? (int) $query['itemsPerPage'] : null
34
        );
35
    }
36
37 3
    private static function getDateQueryParam(array $query, string $key): ?Chronos
38
    {
39 3
        return ! isset($query[$key]) || empty($query[$key]) ? null : Chronos::parse($query[$key]);
40
    }
41
42 1
    public function getDateRange(): DateRange
43
    {
44 1
        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...
45
    }
46
47 1
    public function getPage(): int
48
    {
49 1
        return $this->page;
50
    }
51
52
    public function getItemsPerPage(): ?int
53
    {
54
        return $this->itemsPerPage;
55
    }
56
57 1
    public function hasItemsPerPage(): bool
58
    {
59 1
        return $this->itemsPerPage !== null;
60
    }
61
}
62