Completed
Push — develop ( 8d438a...1f78f5 )
by Alejandro
16s queued 12s
created

ShortUrlMeta::getValidUntil()   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 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 Cake\Chronos\Chronos;
8
use Shlinkio\Shlink\Core\Exception\ValidationException;
9
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
10
11
use function Shlinkio\Shlink\Core\parseDateField;
12
13
use const Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH;
0 ignored issues
show
Bug introduced by
The constant Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
15
final class ShortUrlMeta
16
{
17
    private ?Chronos $validSince = null;
18
    private ?Chronos $validUntil = null;
19
    private ?string $customSlug = null;
20
    private ?int $maxVisits = null;
21
    private ?bool $findIfExists = null;
22
    private ?string $domain = null;
23
    private int $shortCodeLength = 5;
24
25
    // Enforce named constructors
26
    private function __construct()
27
    {
28
    }
29
30 82
    public static function createEmpty(): self
31
    {
32 82
        return new self();
33
    }
34
35
    /**
36
     * @throws ValidationException
37
     */
38 122
    public static function fromRawData(array $data): self
39
    {
40 122
        $instance = new self();
41 122
        $instance->validateAndInit($data);
42 111
        return $instance;
43
    }
44
45
    /**
46
     * @throws ValidationException
47
     */
48 122
    private function validateAndInit(array $data): void
49
    {
50 122
        $inputFilter = new ShortUrlMetaInputFilter($data);
51 122
        if (! $inputFilter->isValid()) {
52 11
            throw ValidationException::fromInputFilter($inputFilter);
53
        }
54
55 111
        $this->validSince = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE));
56 111
        $this->validUntil = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL));
57 111
        $this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG);
58 111
        $this->maxVisits = $this->getOptionalIntFromInputFilter($inputFilter, ShortUrlMetaInputFilter::MAX_VISITS);
59 111
        $this->findIfExists = $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS);
60 111
        $this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN);
61 111
        $this->shortCodeLength = $this->getOptionalIntFromInputFilter(
62 111
            $inputFilter,
63 111
            ShortUrlMetaInputFilter::SHORT_CODE_LENGTH,
64 100
        ) ?? DEFAULT_SHORT_CODES_LENGTH;
65
    }
66
67 111
    private function getOptionalIntFromInputFilter(ShortUrlMetaInputFilter $inputFilter, string $fieldName): ?int
68
    {
69 111
        $value = $inputFilter->getValue($fieldName);
70 111
        return $value !== null ? (int) $value : null;
71
    }
72
73 187
    public function getValidSince(): ?Chronos
74
    {
75 187
        return $this->validSince;
76
    }
77
78 5
    public function hasValidSince(): bool
79
    {
80 5
        return $this->validSince !== null;
81
    }
82
83 187
    public function getValidUntil(): ?Chronos
84
    {
85 187
        return $this->validUntil;
86
    }
87
88 5
    public function hasValidUntil(): bool
89
    {
90 5
        return $this->validUntil !== null;
91
    }
92
93 187
    public function getCustomSlug(): ?string
94
    {
95 187
        return $this->customSlug;
96
    }
97
98 187
    public function hasCustomSlug(): bool
99
    {
100 187
        return $this->customSlug !== null;
101
    }
102
103 187
    public function getMaxVisits(): ?int
104
    {
105 187
        return $this->maxVisits;
106
    }
107
108 5
    public function hasMaxVisits(): bool
109
    {
110 5
        return $this->maxVisits !== null;
111
    }
112
113 12
    public function findIfExists(): bool
114
    {
115 12
        return (bool) $this->findIfExists;
116
    }
117
118
    public function hasDomain(): bool
119
    {
120
        return $this->domain !== null;
121
    }
122
123 182
    public function getDomain(): ?string
124
    {
125 182
        return $this->domain;
126
    }
127
128 182
    public function getShortCodeLength(): int
129
    {
130 182
        return $this->shortCodeLength;
131
    }
132
}
133