Completed
Push — develop ( 8162da...f53fa5 )
by Alejandro
31s queued 13s
created

ShortUrlMeta   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 122
ccs 50
cts 50
cp 1
rs 10
c 1
b 0
f 0
wmc 19

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