Completed
Push — master ( 8d9af3...515cf1 )
by monster
01:10
created

RedisClient::buildExtraKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: zjw
5
 * Date: 2017/8/25
6
 * Time: 下午3:59
7
 */
8
9
namespace monsterhunter\redis\relational;
10
11
class RedisClient
12
{
13
    /**
14
     * @var \Predis\Client  |  Redis
15
     */
16
    public $client;
17
18
    /**
19
     * @var array
20
     */
21
    public $params;
22
23
    /**
24
     *  初始化
25
     */
26 6
    public function init()
27
    {
28 6
        if($this->params['password'] == 'null' || $this->params['password']==''){
29 6
            unset($this->params['password']);
30 6
        }
31 6
        $this->client = new \Predis\Client($this->params);
32 6
    }
33
34
    /**
35
     * 设置记录
36
     * @param $token string
37
     * @param array $arr ['access_token'=>'','client_id' => 'client1', 'expires'=>1, 'union_id' => 'union_id', 'user_id' => '1', 'scope'=>1];
38
     */
39 6
    public function hset($token, array $arr)
40
    {
41 6
        $tokenKey = $this->getPrefix() . $token;
42 6
        $this->client->hmset($tokenKey, $arr);
43 6
        $extraKey = $this->buildExtraKey($token, $arr);
44 6
        $this->client->set($extraKey, $token);
45 6
        if (isset($arr['expires']) && intval($arr['expires']) > time()) {
46 6
            $this->client->expire($tokenKey, $arr['expires'] - time());
47 6
            $this->client->expire($extraKey, $arr['expires'] - time());
48 6
        }
49 6
    }
50
51
    /**
52
     * 通过键获取记录
53
     * @param $token
54
     * @return array
55
     */
56 6
    public function hget($token)
57 6
    {
58 6
        $key = $this->getPrefix() . $token;
59 6
        return $this->client->hgetall($key);
60
    }
61
62
    /**
63
     * 通过键删除
64
     * @param $token
65
     */
66 5
    public function deleteByToken($token)
67
    {
68 5
        $key = $this->getPrefix() . $token;
69 5
        $this->client->del([$key]);
70 5
    }
71
72
    /**
73
     * 删除与用户相关的所有记录
74
     * delete by user_id
75
     * @param $userId
76
     */
77 1
    public function deleteByUserId($userId)
78
    {
79 1
        $userIdKeys = $this->getKeysByUserId($userId);
80 1
        $this->delKeys($userIdKeys);
81 1
    }
82
83
    /**
84
     * 删除与clientId和userId相关的所有记录
85
     * @param $clientId
86
     * @param $userId
87
     */
88 1
    public function deleteByUserIdAndClientId($clientId, $userId)
89
    {
90 1
        $userIidClientIdKeys = $this->getKeysByClientIdAndUserId($clientId, $userId);
91 1
        $this->delKeys($userIidClientIdKeys);
92 1
    }
93
94
    /**
95
     * 删除与clientId有关的所有token
96
     * @param $clientId
97
     * @return void
98
     */
99 1
    public function deleteByClientId($clientId)
100
    {
101 1
        $clientIdKeys = $this->getKeysByClientId($clientId);
102 1
        $this->delKeys($clientIdKeys);
103 1
    }
104
105
    /**
106
     * 删除当前记录之外的所有记录
107
     * @param $currentToken
108
     * @param $clientId
109
     * @param $userId
110
     */
111 1
    public function revokeOldToken($currentToken, $clientId, $userId)
112
    {
113 1
        $clientIidIUserIdKeys = $this->getKeysByClientIdAndUserId($clientId, $userId);
114 1
        if (count($clientIidIUserIdKeys) > 0) {
115 1
            foreach ($clientIidIUserIdKeys as $key => $v) {
116 1
                $token = $this->client->get($v);
117 1
                if ($token !== $currentToken) {
118 1
                    $this->deleteByToken($token);
119 1
                    $this->client->del($v);
120 1
                }
121 1
            }
122 1
        }
123 1
    }
124
125
    /**
126
     * 生成关联键值对
127
     * @param $token
128
     * @param array $arr ['access_token'=>'','client_id' => 'client1', 'expires'=>1, 'union_id' => 'union_id', 'user_id' => '1', 'scope'=>1];
129
     * @return string
130
     */
131 6
    private function buildExtraKey($token, array $arr)
132
    {
133 6
        $clientIdUserIdKey = "client_id" . "_" . "{$arr['client_id']}" . "-" . "user_id" . "_" . "{$arr['user_id']}" . "-" . "access_token" . "_" . $token . "-" . "union_id" . "_" . "{$arr['union_id']}";
134 6
        return $this->getPrefix() . $clientIdUserIdKey;
135
    }
136
137
    /**
138
     * 客户端id和用户id 模糊匹配出所有键
139
     * @param $clientId
140
     * @param $userId
141
     * @return array
142
     */
143 2
    private function getKeysByClientIdAndUserId($clientId, $userId)
144
    {
145 2
        $clientIdUserIdKeys = $this->getPrefix() . "client_id" . "_" . "{$clientId}" . "-" . "user_id" . "_" . "{$userId}" . "-*";
146 2
        return $this->client->keys($clientIdUserIdKeys);
147
    }
148
149
    /**
150
     * 通过clientId取出clientId对应的与主记录关联的键数组
151
     * @param $clientId
152
     * @return array
153
     */
154 1
    private function getKeysByClientId($clientId)
155
    {
156 1
        $clientIdKey = $this->getPrefix() . "client_id" . "_" . "{$clientId}" . "-*";
157 1
        return $this->client->keys($clientIdKey);
158
    }
159
160
    /**
161
     * 通过用户ID取出用户对应的与主记录关联的键数组
162
     * @param $userId
163
     * @return array
164
     */
165 1
    private function getKeysByUserId($userId)
166
    {
167 1
        $userIdKey = $this->getPrefix() . "*-user_id" . "_" . "{$userId}" . "-*";
168 1
        return $this->client->keys($userIdKey);
169
    }
170
171
    /**
172
     * 删除与key有关的所有记录
173
     * @param $keys
174
     */
175 3
    private function delKeys($keys)
176
    {
177 3
        if (count($keys) > 0) {
178 3
            foreach ($keys as $key => $v) {
179 3
                $token = $this->client->get($v);
180 3
                $this->deleteByToken($token);
181 3
                $this->client->del($v);
182 3
            }
183 3
        }
184 3
    }
185
186
    /**
187
     * 所有记录的key值前缀
188
     * @return string
189
     */
190 6
    private function getPrefix()
191
    {
192 6
        return "";
193
    }
194
}
195