Token::getLastUsage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
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 integer $allowedUsages
83
     * @param array   $payload
84
     * @param array   $information
85
     */
86 12
    public function __construct(
87
        $userClass,
88
        $userId,
89
        $value,
90
        $purpose,
91
        $validDuration,
92
        $keepDuration,
93
        $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
112
     */
113
    public function getId()
114
    {
115
        return $this->id;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 2
    public function getUserClass()
122
    {
123 2
        return $this->userClass;
124
    }
125
126
    /**
127
     * @return string
128
     */
129 2
    public function getUserId()
130
    {
131 2
        return $this->userId;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 1
    public function getValue()
138
    {
139 1
        return $this->value;
140
    }
141
142
    /**
143
     * @return string
144
     */
145 1
    public function getPurpose()
146
    {
147 1
        return $this->purpose;
148
    }
149
150
    /**
151
     * @return array
152
     */
153 1
    public function getPayload()
154
    {
155 1
        return $this->payload;
156
    }
157
158
    /**
159
     * @return DateTime
160
     */
161 1
    public function getCreatedAt()
162
    {
163 1
        return $this->createdAt;
164
    }
165
166
    /**
167
     * @return array
168
     */
169 1
    public function getCreatedInformation()
170
    {
171 1
        return $this->createdInformation;
172
    }
173
174
    /**
175
     * @return DateTime
176
     */
177 2
    public function getExpiresAt()
178
    {
179 2
        return $this->expiresAt;
180
    }
181
182
    /**
183
     * @return DateTime
184
     */
185 1
    public function getKeepUntil()
186
    {
187 1
        return $this->keepUntil;
188
    }
189
190
    /**
191
     * @return DateTime|null
192
     *
193
     * @deprecated since version 2.2 and will be removed in 3.0
194
     */
195
    public function getUsedAt()
196
    {
197
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
198
            'The '.__METHOD__
199
            .' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.',
200
            E_USER_DEPRECATED
201
        );
202
203
        $usage = $this->getLastUsage();
204
        if (null === $usage) {
205
            return null;
206
        }
207
208
        return $usage->getCreatedAt();
209
    }
210
211
    /**
212
     * @param DateTime $usedAt
213
     *
214
     * @deprecated since version 2.2 and will be removed in 3.0
215
     */
216
    public function setUsedAt($usedAt)
217
    {
218
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
219
            'The '.__METHOD__
220
            .' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.',
221
            E_USER_DEPRECATED
222
        );
223
224
        $this->consume([], $usedAt);
225
    }
226
227
    /**
228
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
229
     *
230
     * @deprecated since version 2.2 and will be removed in 3.0
231
     */
232
    public function getUsedInformation()
233
    {
234
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
235
            'The '.__METHOD__
236
            .' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.',
237
            E_USER_DEPRECATED
238
        );
239
240
        $usage = $this->getLastUsage();
241
        if (null === $usage) {
242
            return null;
243
        }
244
245
        return $usage->getInformation();
246
    }
247
248
    /**
249
     * @param array $usedInformation
250
     *
251
     * @deprecated since version 2.2 and will be removed in 3.0
252
     */
253
    public function setUsedInformation($usedInformation)
254
    {
255
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
256
            'The '.__METHOD__
257
            .' method is deprecated since version 2.2 and will be removed in 3.0. Use the getLastUsage() method instead.',
258
            E_USER_DEPRECATED
259
        );
260
261
        $this->consume($usedInformation);
262
    }
263
264
    /**
265
     * @return boolean
266
     */
267 4
    public function isExpired()
268
    {
269 4
        return $this->expiresAt < new DateTime();
270
    }
271
272
    /**
273
     * @deprecated since 2.3 and will be removed in 3.0. Use isConsumed instead.
274
     * @return boolean
275
     */
276
    public function isUsed()
277
    {
278
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
279
            __METHOD__.' is deprecated. Use '.__CLASS__.'::isConsumed instead',
280
            E_USER_DEPRECATED
281
        );
282
283
        return $this->isConsumed();
284
    }
285
286
    /**
287
     * @return boolean
288
     */
289 7
    public function isConsumed()
290
    {
291 7
        $allowed = $this->getAllowedUsages();
292 7
        if ($allowed === 0) {
293 1
            return false;
294
        }
295
296 6
        return $this->getCountUsages() >= $allowed;
297
    }
298
299
    /**
300
     * @return int
301
     */
302 7
    public function getAllowedUsages()
303
    {
304 7
        return $this->allowedUsages;
305
    }
306
307
    /**
308
     * @return int
309
     */
310 7
    public function getCountUsages()
311
    {
312 7
        return count($this->usages);
313
    }
314
315
    /**
316
     * @return TokenUsage[]
317
     */
318 4
    public function getUsages()
319
    {
320 4
        return $this->usages->toArray();
321
    }
322
323
    /**
324
     * @return TokenUsage|null
325
     */
326 3
    public function getLastUsage()
327
    {
328 3
        return $this->usages->last();
329
    }
330
331
    /**
332
     * @param array         $information
333
     * @param DateTime|null $date
334
     */
335 5
    public function consume(array $information, DateTime $date = null)
336
    {
337 5
        if ($this->isConsumed()) {
338
            throw new LogicException(
339
                sprintf('Token "%d" is already consumed.', $this->id)
340
            );
341
        }
342
343 5
        $this->usages->add(new TokenUsage($this, $information, $date));
344 5
    }
345
}
346