Completed
Push — master ( f7ceef...6986d0 )
by Alejandro
16s
created

ShortUrlMeta::hasMaxVisits()   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
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
use function is_string;
10
11
final class ShortUrlMeta
12
{
13
    /**
14
     * @var Chronos|null
15
     */
16
    private $validSince;
17
    /**
18
     * @var Chronos|null
19
     */
20
    private $validUntil;
21
    /**
22
     * @var string|null
23
     */
24
    private $customSlug;
25
    /**
26
     * @var int|null
27
     */
28
    private $maxVisits;
29
30
    // Force named constructors
31 43
    private function __construct()
32
    {
33 43
    }
34
35 32
    public static function createEmpty(): self
36
    {
37 32
        return new self();
38
    }
39
40
    /**
41
     * @param array $data
42
     * @throws ValidationException
43
     */
44 5
    public static function createFromRawData(array $data): self
45
    {
46 5
        $instance = new self();
47 5
        $instance->validate($data);
48 2
        return $instance;
49
    }
50
51
    /**
52
     * @param string|Chronos|null $validSince
53
     * @param string|Chronos|null $validUntil
54
     * @param string|null $customSlug
55
     * @param int|null $maxVisits
56
     * @throws ValidationException
57
     */
58 10
    public static function createFromParams(
59
        $validSince = null,
60
        $validUntil = null,
61
        $customSlug = null,
62
        $maxVisits = null
63
    ): self {
64
        // We do not type hint the arguments because that will be done by the validation process
65 10
        $instance = new self();
66 10
        $instance->validate([
67 10
            ShortUrlMetaInputFilter::VALID_SINCE => $validSince,
68 10
            ShortUrlMetaInputFilter::VALID_UNTIL => $validUntil,
69 10
            ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug,
70 10
            ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits,
71
        ]);
72 10
        return $instance;
73
    }
74
75
    /**
76
     * @param array $data
77
     * @throws ValidationException
78
     */
79 15
    private function validate(array $data): void
80
    {
81 15
        $inputFilter = new ShortUrlMetaInputFilter($data);
82 15
        if (! $inputFilter->isValid()) {
83 3
            throw ValidationException::fromInputFilter($inputFilter);
84
        }
85
86 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

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