Test Failed
Push — master ( da5486...b4738e )
by LEUNG
06:50
created

Blacklist   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A push() 0 6 2
A getStore() 0 3 1
A has() 0 5 1
A __construct() 0 5 1
A getAll() 0 3 1
1
<?php
2
3
namespace xiaodi;
4
5
use think\App;
6
use Lcobucci\JWT\Token;
7
8
/**
9
 * 黑名单
10
 *
11
 */
12
class Blacklist
13
{
14
    private $app;
15
16
    private $store;
17
18
    protected $cacheName = 'blacklist';
19
20
    public function __construct(App $app)
21
    {
22
        $this->app = $app;
23
24
        $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\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...
25
    }
26
27
    /**
28
     * 获取 缓存驱动
29
     *
30
     * @return void
31
     */
32
    public function getStore()
33
    {
34
        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...
35
    }
36
37
    /**
38
     * 加入黑名单
39
     *
40
     * @param Token $token
41
     * @return void
42
     */
43
    public function push(Token $token)
44
    {
45
        if (false === $this->has($token)) {
46
            $claims = $token->getClaims();
47
            $exp = $claims['exp']->getValue() - time();
48
            $this->store->push($this->cacheName, (string)$token, $exp);
49
        }
50
    }
51
52
    /**
53
     * 是否存在黑名单
54
     *
55
     * @param Token $token
56
     * @return boolean
57
     */
58
    public function has(Token $token)
59
    {
60
        $blacklist = $this->getAll();
61
        
62
        return in_array((string)$token, $blacklist);
63
    }
64
65
    /**
66
     * 获取所有黑名单
67
     *
68
     * @return array
69
     */
70
    public function getAll()
71
    {
72
        return $this->store->get($this->cacheName, []);
73
    }
74
}
75