Completed
Push — master ( eca780...57807c )
by Alejandro
27s queued 11s
created

ShortUrlsParams::emptyInstance()   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
    private int $page;
16
    private ?string $searchTerm;
17
    private array $tags;
18
    private ShortUrlsOrdering $orderBy;
19
    private ?DateRange $dateRange;
20
21
    private function __construct()
22
    {
23
    }
24
25 3
    public static function emptyInstance(): self
26
    {
27 3
        return self::fromRawData([]);
28
    }
29
30
    /**
31
     * @throws ValidationException
32
     */
33 49
    public static function fromRawData(array $query): self
34
    {
35 49
        $instance = new self();
36 49
        $instance->validateAndInit($query);
37
38 49
        return $instance;
39
    }
40
41
    /**
42
     * @throws ValidationException
43
     */
44 49
    private function validateAndInit(array $query): void
45
    {
46 49
        $inputFilter = new ShortUrlsParamsInputFilter($query);
47 49
        if (! $inputFilter->isValid()) {
48
            throw ValidationException::fromInputFilter($inputFilter);
49
        }
50
51 49
        $this->page = (int) ($inputFilter->getValue(ShortUrlsParamsInputFilter::PAGE) ?? 1);
52 49
        $this->searchTerm = $inputFilter->getValue(ShortUrlsParamsInputFilter::SEARCH_TERM);
53 49
        $this->tags = (array) $inputFilter->getValue(ShortUrlsParamsInputFilter::TAGS);
54 49
        $this->dateRange = new DateRange(
55 49
            parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::START_DATE)),
56 49
            parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::END_DATE)),
57
        );
58 49
        $this->orderBy = ShortUrlsOrdering::fromRawData($query);
59
    }
60
61 1
    public function page(): int
62
    {
63 1
        return $this->page;
64
    }
65
66 23
    public function searchTerm(): ?string
67
    {
68 23
        return $this->searchTerm;
69
    }
70
71 23
    public function tags(): array
72
    {
73 23
        return $this->tags;
74
    }
75
76 12
    public function orderBy(): ShortUrlsOrdering
77
    {
78 12
        return $this->orderBy;
79
    }
80
81 23
    public function dateRange(): ?DateRange
82
    {
83 23
        return $this->dateRange;
84
    }
85
}
86