Completed
Pull Request — develop (#809)
by Alejandro
12:25
created

ShortUrlsParams::dateRange()   A

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
use Shlinkio\Shlink\Core\Exception\ValidationException;
9
use Shlinkio\Shlink\Core\Validation\ShortUrlsParamsInputFilter;
10
11
use function Shlinkio\Shlink\Core\parseDateField;
12
13
final class ShortUrlsParams
14
{
15
    public const DEFAULT_ITEMS_PER_PAGE = 10;
16
17
    private int $page;
18
    private ?string $searchTerm;
19
    private array $tags;
20
    private ShortUrlsOrdering $orderBy;
21
    private ?DateRange $dateRange;
22
    private ?int $itemsPerPage = null;
23
24
    private function __construct()
25
    {
26
    }
27
28 3
    public static function emptyInstance(): self
29
    {
30 3
        return self::fromRawData([]);
31
    }
32
33
    /**
34
     * @throws ValidationException
35
     */
36 50
    public static function fromRawData(array $query): self
37
    {
38 50
        $instance = new self();
39 50
        $instance->validateAndInit($query);
40
41 50
        return $instance;
42
    }
43
44
    /**
45
     * @throws ValidationException
46
     */
47 50
    private function validateAndInit(array $query): void
48
    {
49 50
        $inputFilter = new ShortUrlsParamsInputFilter($query);
50 50
        if (! $inputFilter->isValid()) {
51
            throw ValidationException::fromInputFilter($inputFilter);
52
        }
53
54 50
        $this->page = (int) ($inputFilter->getValue(ShortUrlsParamsInputFilter::PAGE) ?? 1);
55 50
        $this->searchTerm = $inputFilter->getValue(ShortUrlsParamsInputFilter::SEARCH_TERM);
56 50
        $this->tags = (array) $inputFilter->getValue(ShortUrlsParamsInputFilter::TAGS);
57 50
        $this->dateRange = new DateRange(
58 50
            parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::START_DATE)),
59 50
            parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::END_DATE)),
60
        );
61 50
        $this->orderBy = ShortUrlsOrdering::fromRawData($query);
62 50
        $this->itemsPerPage = (int) (
63 50
            $inputFilter->getValue(ShortUrlsParamsInputFilter::ITEMS_PER_PAGE) ?? self::DEFAULT_ITEMS_PER_PAGE
64
        );
65
    }
66
67 1
    public function page(): int
68
    {
69 1
        return $this->page;
70
    }
71
72 1
    public function itemsPerPage(): int
73
    {
74 1
        return $this->itemsPerPage;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->itemsPerPage could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
75
    }
76
77 23
    public function searchTerm(): ?string
78
    {
79 23
        return $this->searchTerm;
80
    }
81
82 23
    public function tags(): array
83
    {
84 23
        return $this->tags;
85
    }
86
87 12
    public function orderBy(): ShortUrlsOrdering
88
    {
89 12
        return $this->orderBy;
90
    }
91
92 23
    public function dateRange(): ?DateRange
93
    {
94 23
        return $this->dateRange;
95
    }
96
}
97