TokenDispatcher::assign()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 2
1
<?php
2
/**
3
 * @filename TokenDispatcher.php
4
 * @touch    09/11/2016 11:29
5
 * @author   wudege <[email protected]>
6
 * @version  1.0.0
7
 */
8
9
namespace TokenAssistant;
10
11
use Predis\Client;
12
13
class TokenDispatcher implements TokenDispatcherInterface
14
{
15
    const TOKEN_HASH_NAMESPACE   = 'token:';
16
    const USER_ID_HASH_NAMESPACE = 'user-id:';
17
18
    /**
19
     * @var Client
20
     */
21
    private $redisClient;
22
23
    /**
24
     * TokenDispatcher constructor.
25
     *
26
     * @param Client $client
27
     */
28
    public function __construct(Client $client)
29
    {
30
        $this->redisClient = $client;
31
    }
32
33
    /**
34
     * Assign a new token or gain existed token.
35
     * @author wudege <[email protected]>
36
     *
37
     * @param string $userId
38
     * @param bool   $forceRefresh
39
     *
40
     * @return string
41
     */
42
    public function assign($userId, $forceRefresh = true)
43
    {
44
        $token = $this->getTokenByUserId($userId);
45
        if ($token && $forceRefresh) {
46
            $this->redisClient->hdel(static::TOKEN_HASH_NAMESPACE, $token);
47
        }
48
        $token = $token ? $token : Util::genToken();
49
        $this->redisClient->hset(static::TOKEN_HASH_NAMESPACE, $token, $userId);
50
        $this->redisClient->hset(static::USER_ID_HASH_NAMESPACE, $userId, $token);
51
52
        return $token;
53
    }
54
55
    /**
56
     * Recycle token if exist.
57
     * @author wudege <[email protected]>
58
     *
59
     * @param string $userId
60
     *
61
     * @return bool
62
     */
63
    public function recycle($userId)
64
    {
65
        $token = $this->getTokenByUserId($userId);
66
        if ($token) {
67
            $this->redisClient->hdel(static::TOKEN_HASH_NAMESPACE, $token);
68
        }
69
        $this->redisClient->hdel(static::USER_ID_HASH_NAMESPACE, $userId);
70
71
        return true;
72
    }
73
74
    /**
75
     * Recycle the old token if exist, then assign new token.
76
     * @author wudege <[email protected]>
77
     *
78
     * @param $userId
79
     *
80
     * @return string
81
     */
82
    public function reassign($userId)
83
    {
84
        $this->recycle($userId);
85
86
        return $this->assign($userId);
87
    }
88
89
    /**
90
     * Get user id from token.
91
     * @author wudege <[email protected]>
92
     *
93
     * @param string $token
94
     *
95
     * @return string
96
     */
97
    public function getUserIdByToken($token)
98
    {
99
        return $this->redisClient->hget(static::TOKEN_HASH_NAMESPACE, $token);
100
    }
101
102
    /**
103
     *
104
     * @author wudege <[email protected]>
105
     *
106
     * @param string $userId
107
     *
108
     * @return string
109
     */
110
    public function getTokenByUserId($userId)
111
    {
112
        return $this->redisClient->hget(static::USER_ID_HASH_NAMESPACE, $userId);
113
    }
114
}