Completed
Push — develop ( 1f78f5...10fbf8 )
by Alejandro
16s queued 11s
created

ShortUrlMeta   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 95.74%

Importance

Changes 0
Metric Value
wmc 18
eloc 39
c 0
b 0
f 0
dl 0
loc 117
rs 10
ccs 45
cts 47
cp 0.9574

17 Methods

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