User   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 312
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 14.29%

Importance

Changes 0
Metric Value
wmc 34
lcom 3
cbo 1
dl 0
loc 312
ccs 11
cts 77
cp 0.1429
rs 9.2
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 2
A all() 0 13 3
A one() 0 6 2
A remove() 0 6 1
A batchRemove() 0 6 2
A resetPassword() 0 11 1
A updateNickname() 0 11 2
A addFriend() 0 8 2
A removeFriend() 0 6 2
A friends() 0 6 2
A blocks() 0 6 2
A block() 0 11 2
A unblock() 0 6 1
A isOnline() 0 10 2
A offlineMsgCount() 0 10 2
A offlineMsgStatus() 0 6 2
A disable() 0 6 1
A enable() 0 6 1
A disconnect() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the light/easemob.
5
 *
6
 * (c) lichunqiang <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace light\Easemob\Rest;
13
14
/**
15
 * User operation.
16
 */
17
class User extends Rest
18
{
19
    /**
20
     * Register users.
21
     *
22
     * 通过「授权注册」的方式注册用户.
23
     *
24
     * @param array $users
25
     *
26
     *  [['username' => '123123', 'password' => 'password', 'nickname' => 'niamee']]
27
     *
28
     * @return array|bool The entities of registed user
29
     */
30 2
    public function register($users)
31
    {
32 2
        $response = $this->post('users', $users);
33
34 2
        return $response ? $response['entities'] : false;
35
    }
36
37
    /**
38
     * Fetch all user records.
39
     *
40
     * @param string|null $cursor
41
     * @param int         $limit
42
     *
43
     * @return array|bool
44
     */
45
    public function all($cursor = null, $limit = 20)
46
    {
47
        $result = $this->get('users', ['limit' => $limit, 'cursor' => $cursor]);
48
49
        if (false === $result) {
50
            return false;
51
        }
52
53
        return [
54
            'items' => $result['entities'],
55
            'cursor' => isset($result['cursor']) ? $result['cursor'] : null,
56
        ];
57
    }
58
59
    /**
60
     * 获取单个用户信息.
61
     *
62
     * @param string $username
63
     *
64
     * @return mixed
65
     */
66 2
    public function one($username)
67
    {
68 2
        $result = $this->get("users/{$username}");
69
70 2
        return $result ? array_shift($result['entities']) : false;
71
    }
72
73
    /**
74
     * 删除单个用户.
75
     *
76
     * @param string $username
77
     *
78
     * @return bool
79
     */
80 3
    public function remove($username)
81
    {
82 3
        $response = $this->delete("users/{$username}");
83
84 3
        return $response !== false;
85
    }
86
87
    /**
88
     * 批量删除用户, 可一次删除N个用户, N建议值在100~500之间.
89
     *
90
     * 需要通过返回值来确定哪些用户被删除了.
91
     *
92
     * @param int $count 删除个数
93
     *
94
     * @return array|bool 成功删除用户信息列表
95
     */
96 2
    public function batchRemove($count = 100)
97 2
    {
98
        $result = $this->delete('users', ['query' => ['limit' => $count]]);
99
100
        return $result ? $result['entities'] : false;
101
    }
102
103
    /**
104
     * 重置用户密码
105
     *
106
     * @param string $username
107
     * @param string $password
108
     *
109
     * @return bool
110
     */
111
    public function resetPassword($username, $password)
112
    {
113
        $response = $this->put(
114
            "users/{$username}/password",
115
            [
116
                'body' => json_encode(['newpassword' => $password]),
117
            ]
118
        );
119
120
        return $response !== false;
121
    }
122
123
    /**
124
     * 更新用户昵称.
125
     *
126
     * @param string $username
127
     * @param string $nickname
128
     *
129
     * @return bool
130
     */
131
    public function updateNickname($username, $nickname)
132
    {
133
        $response = $this->put(
134
            "users/{$username}",
135
            [
136
                'body' => json_encode(['nickname' => $nickname]),
137
            ]
138
        );
139
140
        return $response ? !empty($response['entities']) : false;
141
    }
142
143
    /**
144
     * 为用户添加好友.
145
     *
146
     * @param string $owner_name      用户名
147
     * @param string $friend_username 被添加为好友的用户名
148
     *
149
     * @return bool
150
     */
151
    public function addFriend($owner_name, $friend_username)
152
    {
153
        $response = $this->post(
154
            "users/{$owner_name}/contacts/users/{$friend_username}"
155
        );
156
157
        return $response ? !empty($response['entities']) : false;
158
    }
159
160
    /**
161
     * 解除用户好友关系.
162
     *
163
     * @param string $owner_name
164
     * @param string $friend_username
165
     *
166
     * @return bool
167
     */
168
    public function removeFriend($owner_name, $friend_username)
169
    {
170
        $response = $this->delete("users/{$owner_name}/contacts/users/{$friend_username}");
171
172
        return $response ? !empty($response['entities']) : false;
173
    }
174
175
    /**
176
     * 查看某个用户的好友.
177
     *
178
     * @param string $username
179
     *
180
     * @return array|bool 好友用户名列表
181
     */
182
    public function friends($username)
183
    {
184
        $response = $this->get("users/{$username}/contacts/users");
185
186
        return $response ? $response['data'] : false;
187
    }
188
189
    /**
190
     * 查看某一个IM用户的黑名单.
191
     *
192
     * @param string $username
193
     *
194
     * @return array|bool 黑名单中的用户名列表
195
     */
196
    public function blocks($username)
197
    {
198
        $response = $this->get("users/{$username}/blocks/users");
199
200
        return $response ? $response['data'] : false;
201
    }
202
203
    /**
204
     * 为用户加入黑名单.
205
     *
206
     * @param string $username
207
     * @param array  $users    需要加入黑名单中的用户名
208
     *
209
     * @return array|bool 已经加入到黑名单中的用户
210
     */
211
    public function block($username, $users)
212
    {
213
        $response = $this->post(
214
            "users/{$username}/blocks/users",
215
            [
216
                'usernames' => $users,
217
            ]
218
        );
219
220
        return $response ? $response['data'] : false;
221
    }
222
223
    /**
224
     * 解除黑名单.
225
     *
226
     * @param string $username
227
     * @param string $target
228
     *
229
     * @return bool
230
     */
231
    public function unblock($username, $target)
232
    {
233
        $response = $this->delete("users/{$username}/blocks/users/{$target}");
234
235
        return $response !== false;
236
    }
237
238
    /**
239
     * 查看用户是否在线
240
     *
241
     * @param string $username
242
     *
243
     * @return bool
244
     */
245
    public function isOnline($username)
246
    {
247
        $response = $this->get("users/{$username}/status");
248
249
        if (false === $response) {
250
            return false;
251
        }
252
253
        return 'online' === $response['data'][$username];
254
    }
255
256
    /**
257
     * 获取用户离线消息数.
258
     *
259
     * @param string $username
260
     *
261
     * @return int
262
     */
263
    public function offlineMsgCount($username)
264
    {
265
        $response = $this->get("users/{$username}/offline_msg_count");
266
267
        if (false === $response) {
268
            return false;
269
        }
270
271
        return (int) $response['data'][$username];
272
    }
273
274
    /**
275
     * @param string $username
276
     * @param string $msg_id
277
     *
278
     * @return string|bool delivered or undelivered
279
     */
280
    public function offlineMsgStatus($username, $msg_id)
281
    {
282
        $response = $this->get("users/{$username}/offline_msg_status/{$msg_id}");
283
284
        return $response ? $response['data'][$username] : false;
285
    }
286
287
    /**
288
     * 禁用账户.
289
     *
290
     * @param string $username
291
     *
292
     * @return bool
293
     */
294
    public function disable($username)
295
    {
296
        $response = $this->post("users/{$username}/deactivate");
297
298
        return $response !== false;
299
    }
300
301
    /**
302
     * 解禁账户.
303
     *
304
     * @param string $username
305
     *
306
     * @return bool
307
     */
308
    public function enable($username)
309
    {
310
        $response = $this->post("users/{$username}/activate");
311
312
        return $response !== false;
313
    }
314
315
    /**
316
     * 强制下线用户.
317
     *
318
     * @param string $username
319
     *
320
     * @return bool
321
     */
322
    public function disconnect($username)
323
    {
324
        $response = $this->get("users/{$username}/disconnect");
325
326
        return $response ? $response['data']['result'] : false;
327
    }
328
}
329