Failed Conditions
Push — master ( 008a9e...af25b2 )
by Luís
11s
created

ValidAt::guardLeeway()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
crap 3.0416
1
<?php
2
declare(strict_types=1);
3
4
namespace Lcobucci\JWT\Validation\Constraint;
5
6
use DateInterval;
7
use DateTimeInterface;
8
use InvalidArgumentException;
9
use Lcobucci\Clock\Clock;
10
use Lcobucci\JWT\Token;
11
use Lcobucci\JWT\Validation\Constraint;
12
use Lcobucci\JWT\Validation\ConstraintViolation;
13
14
final class ValidAt implements Constraint
15
{
16
    /**
17
     * @var Clock
18
     */
19
    private $clock;
20
21
    /**
22
     * @var DateInterval
23
     */
24
    private $leeway;
25
26 4
    public function __construct(Clock $clock, ?DateInterval $leeway = null)
27
    {
28 4
        $this->clock  = $clock;
29 4
        $this->leeway = $this->guardLeeway($leeway);
30 3
    }
31
32 4
    private function guardLeeway(?DateInterval $leeway): DateInterval
33
    {
34 4
        if ($leeway === null) {
35 3
            return new DateInterval('PT0S');
36
        }
37
38 1
        if ($leeway->invert === 1) {
39 1
            throw new InvalidArgumentException('Leeway cannot be negative');
40
        }
41
42
        return $leeway;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 3
    public function assert(Token $token): void
49
    {
50 3
        $now = $this->clock->now();
51
52 3
        $this->assertIssueTime($token, $now->add($this->leeway));
53 2
        $this->assertMinimumTime($token, $now->add($this->leeway));
54 1
        $this->assertExpiration($token, $now->sub($this->leeway));
1 ignored issue
show
Bug introduced by Luís Cobucci
It seems like $now->sub($this->leeway) can also be of type false; however, parameter $now of Lcobucci\JWT\Validation\...dAt::assertExpiration() does only seem to accept DateTimeInterface, 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

54
        $this->assertExpiration($token, /** @scrutinizer ignore-type */ $now->sub($this->leeway));
Loading history...
55
    }
56
57
    /**
58
     * @throws ConstraintViolation
59
     */
60 1
    private function assertExpiration(Token $token, DateTimeInterface $now): void
61
    {
62 1
        if ($token->isExpired($now)) {
63 1
            throw new ConstraintViolation('The token is expired');
64
        }
65
    }
66
67
    /**
68
     * @throws ConstraintViolation
69
     */
70 2
    private function assertMinimumTime(Token $token, DateTimeInterface $now): void
71
    {
72 2
        if (! $token->isMinimumTimeBefore($now)) {
73 1
            throw new ConstraintViolation('The token cannot be used yet');
74
        }
75 1
    }
76
77
    /**
78
     * @throws ConstraintViolation
79
     */
80 3
    private function assertIssueTime(Token $token, DateTimeInterface $now): void
81
    {
82 3
        if (! $token->hasBeenIssuedBefore($now)) {
83 1
            throw new ConstraintViolation('The token was issued in the future');
84
        }
85 2
    }
86
}
87