Blacklist   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A has() 0 5 1
A getAll() 0 3 1
A add() 0 6 2
A getStore() 0 3 1
A getConfig() 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
        $configs = $this->getConfig();
28
29
        foreach ($configs as $key => $config) {
30
            $this->$key = $config;
31
        }
32
    }
33
34
    public function getConfig()
35
    {
36
        return $this->app->config->get('jwt.blacklist', []);
37
    }
38
39
    /**
40
     * 获取 缓存驱动.
41
     *
42
     * @return void
43
     */
44
    public function getStore()
45
    {
46
        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...
47
    }
48
49
    /**
50
     * 加入黑名单.
51
     *
52
     * @param Token $token
53
     *
54
     * @return void
55
     */
56
    public function add(Token $token)
57
    {
58
        if (false === $this->has($token)) {
59
            $claims = $token->getClaims();
60
            $exp = $claims['exp']->getValue() - time();
61
            $this->store->push($this->cacheName, (string) $token, $exp);
62
        }
63
    }
64
65
    /**
66
     * 是否存在黑名单.
67
     *
68
     * @param Token $token
69
     *
70
     * @return bool
71
     */
72
    public function has(Token $token): bool
73
    {
74
        $blacklist = $this->getAll();
75
76
        return in_array((string) $token, $blacklist);
77
    }
78
79
    /**
80
     * 获取所有黑名单.
81
     *
82
     * @return array
83
     */
84
    public function getAll(): array
85
    {
86
        return $this->store->get($this->cacheName, []);
87
    }
88
}
89