Blacklist::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tymon\JWTAuth;
4
5
use Tymon\JWTAuth\Providers\Storage\StorageInterface;
6
7
class Blacklist
8
{
9
    /**
10
     * @var \Tymon\JWTAuth\Providers\Storage\StorageInterface
11
     */
12
    protected $storage;
13
14
    /**
15
     * @param \Tymon\JWTAuth\Providers\Storage\StorageInterface  $storage
16
     */
17 15
    public function __construct(StorageInterface $storage)
18
    {
19 15
        $this->storage = $storage;
20 15
    }
21
22
    /**
23
     * Add the token (jti claim) to the blacklist
24
     *
25
     * @param  \Tymon\JWTAuth\Payload  $payload
26
     * @return boolean
27
     */
28 6
    public function add(Payload $payload)
29
    {
30 6
        $exp = Utils::timestamp($payload['exp']);
31
32
        // there is no need to add the token to the blacklist
33
        // if the token has already expired
34 6
        if ($exp->isPast()) {
35 6
            return false;
36
        }
37
38
        // add a minute to abate potential overlap
39
        $minutes = $exp->diffInMinutes(Utils::now()->subMinute());
40
41
        $this->storage->add($payload['jti'], [], $minutes);
42
43
        return true;
44
    }
45
46
    /**
47
     * Determine whether the token has been blacklisted
48
     *
49
     * @param  \Tymon\JWTAuth\Payload  $payload
50
     * @return boolean
51
     */
52 3
    public function has(Payload $payload)
53
    {
54 3
        return $this->storage->has($payload['jti']);
55
    }
56
57
    /**
58
     * Remove the token (jti claim) from the blacklist
59
     *
60
     * @param  \Tymon\JWTAuth\Payload  $payload
61
     * @return boolean
62
     */
63 3
    public function remove(Payload $payload)
64
    {
65 3
        return $this->storage->destroy($payload['jti']);
66
    }
67
68
    /**
69
     * Remove all tokens from the blacklist
70
     *
71
     * @return boolean
72
     */
73 3
    public function clear()
74
    {
75 3
        $this->storage->flush();
76
77 3
        return true;
78
    }
79
}
80