Passed
Pull Request — master (#567)
by Alejandro
03:47
created

ShortUrlMeta   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 98.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 156
rs 10
ccs 54
cts 55
cp 0.9818
wmc 22

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A createFromParams() 0 20 1
A parseDateField() 0 11 4
A createEmpty() 0 3 1
A validate() 0 14 3
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 createFromRawData() 0 5 1
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 DateTimeInterface;
9
use Shlinkio\Shlink\Core\Exception\ValidationException;
10
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
11
12
final class ShortUrlMeta
13
{
14
    /** @var Chronos|null */
15
    private $validSince;
16
    /** @var Chronos|null */
17
    private $validUntil;
18
    /** @var string|null */
19
    private $customSlug;
20
    /** @var int|null */
21
    private $maxVisits;
22
    /** @var bool|null */
23
    private $findIfExists;
24
    /** @var string|null */
25
    private $domain;
26
27
    // Force named constructors
28
    private function __construct()
29
    {
30
    }
31
32 54
    public static function createEmpty(): self
33
    {
34 54
        return new self();
35
    }
36
37
    /**
38
     * @param array $data
39
     * @throws ValidationException
40
     */
41 10
    public static function createFromRawData(array $data): self
42
    {
43 10
        $instance = new self();
44 10
        $instance->validate($data);
45 5
        return $instance;
46
    }
47
48
    /**
49
     * @param string|Chronos|null $validSince
50
     * @param string|Chronos|null $validUntil
51
     * @param string|null $customSlug
52
     * @param int|null $maxVisits
53
     * @param bool|null $findIfExists
54
     * @param string|null $domain
55
     * @throws ValidationException
56
     */
57 13
    public static function createFromParams(
58
        $validSince = null,
59
        $validUntil = null,
60
        $customSlug = null,
61
        $maxVisits = null,
62
        $findIfExists = null,
63
        $domain = null
64
    ): self {
65
        // We do not type hint the arguments because that will be done by the validation process and we would get a
66
        // type error if any of them do not match
67 13
        $instance = new self();
68 13
        $instance->validate([
69 13
            ShortUrlMetaInputFilter::VALID_SINCE => $validSince,
70 13
            ShortUrlMetaInputFilter::VALID_UNTIL => $validUntil,
71 13
            ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug,
72 13
            ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits,
73 13
            ShortUrlMetaInputFilter::FIND_IF_EXISTS => $findIfExists,
74 13
            ShortUrlMetaInputFilter::DOMAIN => $domain,
75
        ]);
76 10
        return $instance;
77
    }
78
79
    /**
80
     * @param array $data
81
     * @throws ValidationException
82
     */
83 22
    private function validate(array $data): void
84
    {
85 22
        $inputFilter = new ShortUrlMetaInputFilter($data);
86 22
        if (! $inputFilter->isValid()) {
87 8
            throw ValidationException::fromInputFilter($inputFilter);
88
        }
89
90 14
        $this->validSince = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE));
91 14
        $this->validUntil = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL));
92 14
        $this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG);
0 ignored issues
show
Documentation Bug introduced by
It seems like $inputFilter->getValue(S...putFilter::CUSTOM_SLUG) can also be of type array. However, the property $customSlug is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
93 14
        $this->maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS);
0 ignored issues
show
Documentation Bug introduced by
It seems like $inputFilter->getValue(S...nputFilter::MAX_VISITS) can also be of type array. However, the property $maxVisits is declared as type integer|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
94 14
        $this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null;
95 14
        $this->findIfExists = $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS);
0 ignored issues
show
Documentation Bug introduced by
It seems like $inputFilter->getValue(S...Filter::FIND_IF_EXISTS) can also be of type array. However, the property $findIfExists is declared as type boolean|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
96 14
        $this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN);
0 ignored issues
show
Documentation Bug introduced by
It seems like $inputFilter->getValue(S...etaInputFilter::DOMAIN) can also be of type array. However, the property $domain is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
97
    }
98
99
    /**
100
     * @param string|DateTimeInterface|Chronos|null $date
101
     */
102 14
    private function parseDateField($date): ?Chronos
103
    {
104 14
        if ($date === null || $date instanceof Chronos) {
105 13
            return $date;
106
        }
107
108 2
        if ($date instanceof DateTimeInterface) {
109
            return Chronos::instance($date);
110
        }
111
112 2
        return Chronos::parse($date);
113
    }
114
115 57
    public function getValidSince(): ?Chronos
116
    {
117 57
        return $this->validSince;
118
    }
119
120 11
    public function hasValidSince(): bool
121
    {
122 11
        return $this->validSince !== null;
123
    }
124
125 58
    public function getValidUntil(): ?Chronos
126
    {
127 58
        return $this->validUntil;
128
    }
129
130 11
    public function hasValidUntil(): bool
131
    {
132 11
        return $this->validUntil !== null;
133
    }
134
135 58
    public function getCustomSlug(): ?string
136
    {
137 58
        return $this->customSlug;
138
    }
139
140 64
    public function hasCustomSlug(): bool
141
    {
142 64
        return $this->customSlug !== null;
143
    }
144
145 58
    public function getMaxVisits(): ?int
146
    {
147 58
        return $this->maxVisits;
148
    }
149
150 11
    public function hasMaxVisits(): bool
151
    {
152 11
        return $this->maxVisits !== null;
153
    }
154
155 14
    public function findIfExists(): bool
156
    {
157 14
        return (bool) $this->findIfExists;
158
    }
159
160 9
    public function hasDomain(): bool
161
    {
162 9
        return $this->domain !== null;
163
    }
164
165 56
    public function getDomain(): ?string
166
    {
167 56
        return $this->domain;
168
    }
169
}
170