Completed
Push — master ( aca90e...8ef0e7 )
by Alejandro
10s
created

ShortUrlMeta::validate()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 9
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
ccs 12
cts 12
cp 1
crap 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Model;
5
6
use Shlinkio\Shlink\Core\Exception\ValidationException;
7
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
8
9
final class ShortUrlMeta
10
{
11
    /**
12
     * @var \DateTime|null
13
     */
14
    private $validSince;
15
    /**
16
     * @var \DateTime|null
17
     */
18
    private $validUntil;
19
    /**
20
     * @var string|null
21
     */
22
    private $customSlug;
23
    /**
24
     * @var int|null
25
     */
26
    private $maxVisits;
27
28
    // Force named constructors
29 7
    private function __construct()
30
    {
31 7
    }
32
33
    /**
34
     * @param array $data
35
     * @return ShortUrlMeta
36
     * @throws ValidationException
37
     */
38 5
    public static function createFromRawData(array $data): self
39
    {
40 5
        $instance = new self();
41 5
        $instance->validate($data);
42 2
        return $instance;
43
    }
44
45
    /**
46
     * @param string|\DateTimeInterface|null $validSince
47
     * @param string|\DateTimeInterface|null $validUntil
48
     * @param string|null $customSlug
49
     * @param int|null $maxVisits
50
     * @return ShortUrlMeta
51
     * @throws ValidationException
52
     */
53 2
    public static function createFromParams(
54
        $validSince = null,
55
        $validUntil = null,
56
        $customSlug = null,
57
        $maxVisits = null
58
    ): self {
59
        // We do not type hint the arguments because that will be done by the validation process
60 2
        $instance = new self();
61 2
        $instance->validate([
62 2
            ShortUrlMetaInputFilter::VALID_SINCE => $validSince,
63 2
            ShortUrlMetaInputFilter::VALID_UNTIL => $validUntil,
64 2
            ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug,
65 2
            ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits,
66
        ]);
67 2
        return $instance;
68
    }
69
70
    /**
71
     * @param array $data
72
     * @throws ValidationException
73
     */
74 7
    private function validate(array $data)
75
    {
76 7
        $inputFilter = new ShortUrlMetaInputFilter($data);
77 7
        if (! $inputFilter->isValid()) {
78 3
            throw ValidationException::fromInputFilter($inputFilter);
79
        }
80
81 4
        $this->validSince = $inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE);
82 4
        $this->validSince = $this->validSince !== null ? new \DateTime($this->validSince) : null;
83 4
        $this->validUntil = $inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL);
84 4
        $this->validUntil = $this->validUntil !== null ? new \DateTime($this->validUntil) : null;
85 4
        $this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG);
86 4
        $this->maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS);
87 4
        $this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null;
88 4
    }
89
90
    /**
91
     * @return \DateTime|null
92
     */
93 2
    public function getValidSince()
94
    {
95 2
        return $this->validSince;
96
    }
97
98 2
    public function hasValidSince(): bool
99
    {
100 2
        return $this->validSince !== null;
101
    }
102
103
    /**
104
     * @return \DateTime|null
105
     */
106 2
    public function getValidUntil()
107
    {
108 2
        return $this->validUntil;
109
    }
110
111 2
    public function hasValidUntil(): bool
112
    {
113 2
        return $this->validUntil !== null;
114
    }
115
116
    /**
117
     * @return null|string
118
     */
119 1
    public function getCustomSlug()
120
    {
121 1
        return $this->customSlug;
122
    }
123
124 1
    public function hasCustomSlug(): bool
125
    {
126 1
        return $this->customSlug !== null;
127
    }
128
129
    /**
130
     * @return int|null
131
     */
132 2
    public function getMaxVisits()
133
    {
134 2
        return $this->maxVisits;
135
    }
136
137 2
    public function hasMaxVisits(): bool
138
    {
139 2
        return $this->maxVisits !== null;
140
    }
141
}
142