Completed
Pull Request — master (#343)
by Alejandro
05:55
created

ShortUrlMeta   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 136
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0
wmc 19

15 Methods

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        $this->validSince = $this->parseDateField(/** @scrutinizer ignore-type */ $inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE));
Loading history...
84 12
        $this->validUntil = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL));
85 12
        $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...
86 12
        $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...
87 12
        $this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null;
88 12
        $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...
89
    }
90
91
    /**
92
     * @param string|Chronos|null $date
93
     */
94 12
    private function parseDateField($date): ?Chronos
95
    {
96 12
        if ($date === null || $date instanceof Chronos) {
97 11
            return $date;
98
        }
99
100 2
        return Chronos::parse($date);
101
    }
102
103 41
    public function getValidSince(): ?Chronos
104
    {
105 41
        return $this->validSince;
106
    }
107
108 9
    public function hasValidSince(): bool
109
    {
110 9
        return $this->validSince !== null;
111
    }
112
113 42
    public function getValidUntil(): ?Chronos
114
    {
115 42
        return $this->validUntil;
116
    }
117
118 9
    public function hasValidUntil(): bool
119
    {
120 9
        return $this->validUntil !== null;
121
    }
122
123 43
    public function getCustomSlug(): ?string
124
    {
125 43
        return $this->customSlug;
126
    }
127
128 11
    public function hasCustomSlug(): bool
129
    {
130 11
        return $this->customSlug !== null;
131
    }
132
133 42
    public function getMaxVisits(): ?int
134
    {
135 42
        return $this->maxVisits;
136
    }
137
138 9
    public function hasMaxVisits(): bool
139
    {
140 9
        return $this->maxVisits !== null;
141
    }
142
143 11
    public function findIfExists(): bool
144
    {
145 11
        return (bool) $this->findIfExists;
146
    }
147
}
148