Completed
Pull Request — master (#32)
by Yann
01:30
created

Token::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.552
cc 1
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Entity;
4
5
use DateTime;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use LogicException;
9
10
/**
11
 * @author Yann Eugoné <[email protected]>
12
 */
13
class Token
14
{
15
    /**
16
     * @var int|null
17
     */
18
    private $id;
19
20
    /**
21
     * @var string
22
     */
23
    private $userClass;
24
25
    /**
26
     * @var string
27
     */
28
    private $userId;
29
30
    /**
31
     * @var string
32
     */
33
    private $value;
34
35
    /**
36
     * @var string
37
     */
38
    private $purpose;
39
40
    /**
41
     * @var array
42
     */
43
    private $payload = [];
44
45
    /**
46
     * @var DateTime
47
     */
48
    private $createdAt;
49
50
    /**
51
     * @var array
52
     */
53
    private $createdInformation = [];
54
55
    /**
56
     * @var integer
57
     */
58
    private $allowedUsages;
59
60
    /**
61
     * @var DateTime
62
     */
63
    private $expiresAt;
64
65
    /**
66
     * @var DateTime
67
     */
68
    private $keepUntil;
69
70
    /**
71
     * @var Collection|TokenUsage[]
72
     */
73
    private $usages;
74
75
    /**
76
     * @param string  $userClass
77
     * @param string  $userId
78
     * @param string  $value
79
     * @param string  $purpose
80
     * @param string  $validDuration
81
     * @param string  $keepDuration
82
     * @param int     $allowedUsages
83
     * @param array   $payload
84
     * @param array   $information
85
     */
86 12
    public function __construct(
87
        string $userClass,
88
        string $userId,
89
        string $value,
90
        string $purpose,
91
        string $validDuration,
92
        string $keepDuration,
93
        int $allowedUsages = 1,
94
        array $payload = [],
95
        array $information = []
96
    ) {
97 12
        $this->userClass = $userClass;
98 12
        $this->userId = $userId;
99 12
        $this->value = $value;
100 12
        $this->purpose = $purpose;
101 12
        $this->createdAt = new DateTime();
102 12
        $this->expiresAt = (new DateTime())->modify($validDuration);
103 12
        $this->keepUntil = (clone $this->expiresAt)->modify($keepDuration);
104 12
        $this->allowedUsages = $allowedUsages;
105 12
        $this->payload = $payload;
106 12
        $this->createdInformation = $information;
107 12
        $this->usages = new ArrayCollection();
108 12
    }
109
110
    /**
111
     * @return int|null
112
     */
113
    public function getId(): ?int
114
    {
115
        return $this->id;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 2
    public function getUserClass(): string
122
    {
123 2
        return $this->userClass;
124
    }
125
126
    /**
127
     * @return string
128
     */
129 2
    public function getUserId(): string
130
    {
131 2
        return $this->userId;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 1
    public function getValue(): string
138
    {
139 1
        return $this->value;
140
    }
141
142
    /**
143
     * @return string
144
     */
145 1
    public function getPurpose(): string
146
    {
147 1
        return $this->purpose;
148
    }
149
150
    /**
151
     * @return array
152
     */
153 1
    public function getPayload(): array
154
    {
155 1
        return $this->payload;
156
    }
157
158
    /**
159
     * @return DateTime
160
     */
161 1
    public function getCreatedAt(): DateTime
162
    {
163 1
        return $this->createdAt;
164
    }
165
166
    /**
167
     * @return array
168
     */
169 1
    public function getCreatedInformation(): array
170
    {
171 1
        return $this->createdInformation;
172
    }
173
174
    /**
175
     * @return DateTime
176
     */
177 2
    public function getExpiresAt(): DateTime
178
    {
179 2
        return $this->expiresAt;
180
    }
181
182
    /**
183
     * @return DateTime
184
     */
185 1
    public function getKeepUntil(): DateTime
186
    {
187 1
        return $this->keepUntil;
188
    }
189
190
    /**
191
     * @return bool
192
     */
193 4
    public function isExpired(): bool
194
    {
195 4
        return $this->expiresAt < new DateTime();
196
    }
197
198
    /**
199
     * @return bool
200
     */
201 7
    public function isConsumed(): bool
202
    {
203 7
        $allowed = $this->getAllowedUsages();
204 7
        if ($allowed === 0) {
205 1
            return false;
206
        }
207
208 6
        return $this->getCountUsages() >= $allowed;
209
    }
210
211
    /**
212
     * @return int
213
     */
214 7
    public function getAllowedUsages(): int
215
    {
216 7
        return $this->allowedUsages;
217
    }
218
219
    /**
220
     * @return int
221
     */
222 7
    public function getCountUsages(): int
223
    {
224 7
        return count($this->usages);
225
    }
226
227
    /**
228
     * @return TokenUsage[]
229
     */
230 4
    public function getUsages(): array
231
    {
232 4
        return $this->usages->toArray();
233
    }
234
235
    /**
236
     * @return TokenUsage|null
237
     */
238 3
    public function getLastUsage(): ?TokenUsage
239
    {
240 3
        return $this->usages->last();
241
    }
242
243
    /**
244
     * @param array         $information
245
     * @param DateTime|null $date
246
     *
247
     * @throws LogicException
248
     */
249 5
    public function consume(array $information, DateTime $date = null): void
250
    {
251 5
        if ($this->isConsumed()) {
252
            throw new LogicException(
253
                sprintf('Token "%d" is already consumed.', $this->id)
254
            );
255
        }
256
257 5
        $this->usages->add(new TokenUsage($this, $information, $date));
258 5
    }
259
}
260