Completed
Push — master ( 42b611...b70ab3 )
by light
02:44
created

User::batchRemove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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