Completed
Push — master ( 7eb1a4...254269 )
by LEUNG
03:41
created

Blacklist   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A has() 0 5 1
A getAll() 0 3 1
A add() 0 6 2
A getStore() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace xiaodi\JWTAuth;
6
7
use Lcobucci\JWT\Token;
8
use think\App;
9
10
/**
11
 * 黑名单.
12
 */
13
class Blacklist
14
{
15
    private $app;
16
17
    private $store;
18
19
    protected $cacheName = 'blacklist';
20
21
    public function __construct(App $app)
22
    {
23
        $this->app = $app;
24
25
        $this->store = $this->getStore();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->store is correct as $this->getStore() targeting xiaodi\JWTAuth\Blacklist::getStore() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
26
    }
27
28
    /**
29
     * 获取 缓存驱动.
30
     *
31
     * @return void
32
     */
33
    public function getStore()
34
    {
35
        return $this->app->cache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->app->cache returns the type think\Cache which is incompatible with the documented return type void.
Loading history...
36
    }
37
38
    /**
39
     * 加入黑名单.
40
     *
41
     * @param Token $token
42
     *
43
     * @return void
44
     */
45
    public function add(Token $token)
46
    {
47
        if (false === $this->has($token)) {
48
            $claims = $token->getClaims();
49
            $exp = $claims['exp']->getValue() - time();
50
            $this->store->push($this->cacheName, (string) $token, $exp);
51
        }
52
    }
53
54
    /**
55
     * 是否存在黑名单.
56
     *
57
     * @param Token $token
58
     *
59
     * @return bool
60
     */
61
    public function has(Token $token): Bool
62
    {
63
        $blacklist = $this->getAll();
64
65
        return in_array((string) $token, $blacklist);
66
    }
67
68
    /**
69
     * 获取所有黑名单.
70
     *
71
     * @return array
72
     */
73
    public function getAll(): Array
74
    {
75
        return $this->store->get($this->cacheName, []);
76
    }
77
}
78