Token::setScopeModified()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Passport\Entities;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use DateTimeImmutable;
22
use DateTimeInterface;
23
use Limoncello\Passport\Contracts\Entities\TokenInterface;
24
use function assert;
25
use function implode;
26
use function is_int;
27
use function is_string;
28
use function property_exists;
29
30
/**
31
 * @package Limoncello\Passport
32
 */
33
abstract class Token implements TokenInterface
34
{
35
    /**
36
     * @return string
37
     */
38
    abstract protected function getDbDateFormat(): string;
39
40
    /** Field name */
41
    const FIELD_ID = 'id_token';
42
43
    /** Field name */
44
    const FIELD_ID_CLIENT = Client::FIELD_ID;
45
46
    /** Field name */
47
    const FIELD_ID_USER = 'id_user';
48
49
    /** Field name */
50
    const FIELD_SCOPES = 'scopes';
51
52
    /** Field name */
53
    const FIELD_IS_SCOPE_MODIFIED = 'is_scope_modified';
54
55
    /** Field name */
56
    const FIELD_IS_ENABLED = 'is_enabled';
57
58
    /** Field name */
59
    const FIELD_REDIRECT_URI = 'redirect_uri';
60
61
    /** Field name */
62
    const FIELD_CODE = 'code';
63
64
    /** Field name */
65
    const FIELD_VALUE = 'value';
66
67
    /** Field name */
68
    const FIELD_TYPE = 'type';
69
70
    /** Field name */
71
    const FIELD_REFRESH = 'refresh';
72
73
    /** Field name */
74
    const FIELD_CODE_CREATED_AT = 'code_created_at';
75
76
    /** Field name */
77
    const FIELD_VALUE_CREATED_AT = 'value_created_at';
78
79
    /** Field name */
80
    const FIELD_REFRESH_CREATED_AT = 'refresh_created_at';
81
82
    /**
83
     * @var int|null
84
     */
85
    private $identifierField;
86
87
    /**
88
     * @var string
89
     */
90
    private $clientIdentifierField = '';
91
92
    /**
93
     * @var int|string|null
94
     */
95
    private $userIdentifierField;
96
97
    /**
98
     * @var string[]
99
     */
100
    private $scopeIdentifiers = [];
101
102
    /**
103
     * @var string|null
104
     */
105
    private $scopeList = null;
106
107
    /**
108
     * @var bool
109
     */
110
    private $isScopeModified = false;
111
112
    /**
113
     * @var bool
114
     */
115
    private $isEnabled = true;
116
117
    /**
118
     * @var string|null
119
     */
120
    private $redirectUriString = null;
121
122
    /**
123
     * @var string|null
124
     */
125
    private $codeField = null;
126
127
    /**
128
     * @var string|null
129
     */
130
    private $valueField = null;
131
132
    /**
133
     * @var string|null
134
     */
135
    private $typeField = null;
136
137
    /**
138
     * @var string|null
139
     */
140
    private $refreshValueField = null;
141
142
    /**
143
     * @var DateTimeInterface|null
144
     */
145
    private $codeCreatedAtField = null;
146
147
    /**
148
     * @var DateTimeInterface|null
149
     */
150
    private $valueCreatedAtField = null;
151
152
    /**
153
     * @var DateTimeInterface|null
154
     */
155
    private $refreshCreatedAtField = null;
156
157
    /**
158
     * Constructor.
159
     */
160 22
    public function __construct()
161
    {
162 22
        if ($this->hasDynamicProperty(static::FIELD_ID) === true) {
163
            $this
164 12
                ->setIdentifier((int)$this->{static::FIELD_ID})
165 12
                ->setClientIdentifier($this->{static::FIELD_ID_CLIENT})
166 12
                ->setUserIdentifier((int)$this->{static::FIELD_ID_USER})
167 12
                ->setRedirectUriString($this->{static::FIELD_REDIRECT_URI})
168 12
                ->setCode($this->{static::FIELD_CODE})
169 12
                ->setType($this->{static::FIELD_TYPE})
170 12
                ->setValue($this->{static::FIELD_VALUE})
171 12
                ->setRefreshValue($this->{static::FIELD_REFRESH});
172
            $this
173 12
                ->parseIsScopeModified($this->{static::FIELD_IS_SCOPE_MODIFIED})
174 12
                ->parseIsEnabled($this->{static::FIELD_IS_ENABLED});
175
        }
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181 13
    public function getIdentifier(): ?int
182
    {
183 13
        return $this->identifierField;
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189 13
    public function setIdentifier(int $identifier): TokenInterface
190
    {
191 13
        $this->identifierField = $identifier;
192
193 13
        return $this;
194
    }
195
196
    /**
197
     * @inheritdoc
198
     */
199 15
    public function getClientIdentifier(): string
200
    {
201 15
        return $this->clientIdentifierField;
202
    }
203
204
    /**
205
     * @inheritdoc
206
     */
207 15
    public function setClientIdentifier(string $identifier): TokenInterface
208
    {
209 15
        $this->clientIdentifierField = $identifier;
210
211 15
        return $this;
212
    }
213
214
    /**
215
     * @inheritdoc
216
     */
217 13
    public function getUserIdentifier()
218
    {
219 13
        return $this->userIdentifierField;
220
    }
221
222
    /**
223
     * @inheritdoc
224
     */
225 15
    public function setUserIdentifier($identifier): TokenInterface
226
    {
227 15
        assert(is_int($identifier) === true || is_string($identifier) === true);
228
229 15
        $this->userIdentifierField = $identifier;
230
231 15
        return $this;
232
    }
233
234
    /**
235
     * @inheritdoc
236
     */
237 15
    public function getScopeIdentifiers(): array
238
    {
239 15
        return $this->scopeIdentifiers;
240
    }
241
242
    /**
243
     * @inheritdoc
244
     */
245 15
    public function setScopeIdentifiers(array $identifiers): TokenInterface
246
    {
247 15
        $this->scopeIdentifiers = $identifiers;
248
249 15
        $this->scopeList = null;
250
251 15
        return $this;
252
    }
253
254
    /**
255
     * @inheritdoc
256
     */
257 6
    public function getScopeList(): string
258
    {
259 6
        if ($this->scopeList === null) {
260 6
            $this->scopeList = implode(' ', $this->getScopeIdentifiers());
261
        }
262
263 6
        return $this->scopeList;
264
    }
265
266
    /**
267
     * @inheritdoc
268
     */
269 4
    public function getRedirectUriString(): ?string
270
    {
271 4
        return $this->redirectUriString;
272
    }
273
274
    /**
275
     * @inheritdoc
276
     */
277 15
    public function setRedirectUriString(?string $uri): TokenInterface
278
    {
279 15
        $this->redirectUriString = $uri;
280
281 15
        return $this;
282
    }
283
284
    /**
285
     * @inheritdoc
286
     */
287 13
    public function isScopeModified(): bool
288
    {
289 13
        return $this->isScopeModified;
290
    }
291
292
    /**
293
     * @inheritdoc
294
     */
295 8
    public function setScopeModified(): TokenInterface
296
    {
297 8
        $this->isScopeModified = true;
298
299 8
        return $this;
300
    }
301
302
    /**
303
     * @inheritdoc
304
     */
305 7
    public function setScopeUnmodified(): TokenInterface
306
    {
307 7
        $this->isScopeModified = false;
308
309 7
        return $this;
310
    }
311
312
    /**
313
     * @inheritdoc
314
     */
315 10
    public function isEnabled(): bool
316
    {
317 10
        return $this->isEnabled;
318
    }
319
320
    /**
321
     * @inheritdoc
322
     */
323 11
    public function setEnabled(): TokenInterface
324
    {
325 11
        $this->isEnabled = true;
326
327 11
        return $this;
328
    }
329
330
    /**
331
     * @inheritdoc
332
     */
333 2
    public function setDisabled(): TokenInterface
334
    {
335 2
        $this->isEnabled = false;
336
337 2
        return $this;
338
    }
339
340
    /**
341
     * @inheritdoc
342
     */
343 4
    public function getCode(): string
344
    {
345 4
        return $this->codeField;
346
    }
347
348
    /**
349
     * @inheritdoc
350
     */
351 14
    public function setCode(?string $code): TokenInterface
352
    {
353 14
        $this->codeField = $code;
354
355 14
        return $this;
356
    }
357
358
    /**
359
     * @inheritdoc
360
     */
361 14
    public function getValue(): ?string
362
    {
363 14
        return $this->valueField;
364
    }
365
366
    /**
367
     * @inheritdoc
368
     */
369 13
    public function setValue(?string $value): TokenInterface
370
    {
371 13
        $this->valueField = $value;
372
373 13
        return $this;
374
    }
375
376
    /**
377
     * @inheritdoc
378
     */
379 13
    public function getType(): ?string
380
    {
381 13
        return $this->typeField;
382
    }
383
384
    /**
385
     * @inheritdoc
386
     */
387 13
    public function setType(?string $type): TokenInterface
388
    {
389 13
        $this->typeField = $type;
390
391 13
        return $this;
392
    }
393
394
    /**
395
     * @inheritdoc
396
     */
397 14
    public function getRefreshValue(): ?string
398
    {
399 14
        return $this->refreshValueField;
400
    }
401
402
    /**
403
     * @inheritdoc
404
     */
405 13
    public function setRefreshValue(?string $refreshValue): TokenInterface
406
    {
407 13
        $this->refreshValueField = $refreshValue;
408
409 13
        return $this;
410
    }
411
412
    /**
413
     * @inheritdoc
414
     */
415 1
    public function getCodeCreatedAt(): ?DateTimeInterface
416
    {
417 1
        if ($this->codeCreatedAtField === null && ($codeCreatedAt = $this->{static::FIELD_CODE_CREATED_AT}) !== null) {
418 1
            $this->codeCreatedAtField = $this->parseDateTime($codeCreatedAt);
419
        }
420
421 1
        return $this->codeCreatedAtField;
422
    }
423
424
    /**
425
     * @inheritdoc
426
     */
427 2
    public function setCodeCreatedAt(DateTimeInterface $codeCreatedAt): TokenInterface
428
    {
429 2
        $this->codeCreatedAtField = $codeCreatedAt;
430
431 2
        return $this;
432
    }
433
434
    /**
435
     * @inheritdoc
436
     */
437 2
    public function getValueCreatedAt(): ?DateTimeInterface
438
    {
439 2
        if ($this->valueCreatedAtField === null &&
440 2
            ($tokenCreatedAt = $this->{static::FIELD_VALUE_CREATED_AT}) !== null
441
        ) {
442 2
            $this->valueCreatedAtField = $this->parseDateTime($tokenCreatedAt);
443
        }
444
445 2
        return $this->valueCreatedAtField;
446
    }
447
448
    /**
449
     * @inheritdoc
450
     */
451 9
    public function setValueCreatedAt(DateTimeInterface $valueCreatedAt): TokenInterface
452
    {
453 9
        $this->valueCreatedAtField = $valueCreatedAt;
454
455 9
        return $this;
456
    }
457
458
    /**
459
     * @inheritdoc
460
     */
461 1
    public function getRefreshCreatedAt(): ?DateTimeInterface
462
    {
463 1
        if ($this->refreshCreatedAtField === null &&
464 1
            ($tokenCreatedAt = $this->{static::FIELD_VALUE_CREATED_AT}) !== null
465
        ) {
466 1
            $this->refreshCreatedAtField = $this->parseDateTime($tokenCreatedAt);
467
        }
468
469 1
        return $this->refreshCreatedAtField;
470
    }
471
472
    /**
473
     * @inheritdoc
474
     */
475 6
    public function setRefreshCreatedAt(DateTimeInterface $refreshCreatedAt): TokenInterface
476
    {
477 6
        $this->refreshCreatedAtField = $refreshCreatedAt;
478
479 6
        return $this;
480
    }
481
482
    /**
483
     * @inheritdoc
484
     */
485 1
    public function hasBeenUsedEarlier(): bool
486
    {
487 1
        return $this->getValueCreatedAt() !== null;
488
    }
489
490
    /**
491
     * @param string $value
492
     *
493
     * @return Token
494
     */
495 12
    protected function parseIsScopeModified(string $value): Token
496
    {
497 12
        $value === '1' ? $this->setScopeModified() : $this->setScopeUnmodified();
498
499 12
        return $this;
500
    }
501
502
    /**
503
     * @param string $value
504
     *
505
     * @return Token
506
     */
507 12
    protected function parseIsEnabled(string $value): Token
508
    {
509 12
        $value === '1' ? $this->setEnabled() : $this->setDisabled();
510
511 12
        return $this;
512
    }
513
514
    /**
515
     * @param string $createdAt
516
     *
517
     * @return DateTimeInterface
518
     *
519
     * @SuppressWarnings(PHPMD.StaticAccess)
520
     */
521 2
    protected function parseDateTime(string $createdAt): DateTimeInterface
522
    {
523 2
        return DateTimeImmutable::createFromFormat($this->getDbDateFormat(), $createdAt);
524
    }
525
526
    /**
527
     * @param string $name
528
     *
529
     * @return bool
530
     */
531 22
    protected function hasDynamicProperty(string $name): bool
532
    {
533 22
        return property_exists($this, $name);
534
    }
535
}
536