Completed
Push — master ( 8b7748...d01484 )
by light
02:38
created

Group   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 301
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 37
c 3
b 0
f 0
lcom 3
cbo 1
dl 0
loc 301
ccs 0
cts 92
cp 0
rs 8.6

17 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 18 3
A detail() 0 11 3
A create() 0 23 2
A update() 0 12 3
A remove() 0 6 2
A members() 0 6 2
A join() 0 6 2
A batchJoin() 0 10 2
A out() 0 6 2
A batchOut() 0 7 2
A getUserGroups() 0 6 2
A transfer() 0 10 2
A blocks() 0 6 2
A block() 0 6 2
A batchBlock() 0 10 2
A unblock() 0 6 2
A batchUnblock() 0 7 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
class Group extends Rest
15
{
16
    /**
17
     * Get all groups.
18
     *
19
     * @param null|string $cursor
20
     * @param int         $limit
21
     *
22
     * @return array|bool
23
     */
24
    public function all($cursor = null, $limit = 20)
25
    {
26
        $response = $this->get('chatgroups', [
27
            'query' => [
28
                'limit' => $limit,
29
                'cursor' => $cursor,
30
            ],
31
        ]);
32
33
        if (false === $response) {
34
            return false;
35
        }
36
37
        return [
38
            'items' => $response['data'],
39
            'cursor' => isset($response['cursor']) ? $response['cursor'] : '',
40
        ];
41
    }
42
43
    /**
44
     * Fetch the group details.
45
     *
46
     * @return bool|array
47
     */
48
    public function detail()
49
    {
50
        $args = func_get_args();
51
        if (empty($args)) {
52
            throw new \BadMethodCallException('At last one parameter should be set.');
53
        }
54
        $groups = implode(',', $args);
55
        $response = $this->get("chatgroups/{$groups}");
56
57
        return $response ? $response['data'] : false;
58
    }
59
60
    /**
61
     * @param $name
62
     * @param $desc
63
     * @param $owner
64
     * @param array $members
65
     * @param bool  $is_public
66
     * @param int   $max_users 默认值200,最大值2000
67
     * @param bool  $approval  是否需要批准
68
     *
69
     * @return bool|array ['groupid' => 12312312]
70
     */
71
    public function create(
72
        $name,
73
        $desc,
74
        $owner,
75
        array $members = [],
76
        $is_public = false,
77
        $max_users = 200,
78
        $approval = false
79
    ) {
80
        $response = $this->post('chatgroups', [
81
            'body' => json_encode([
82
                'groupname' => $name,
83
                'desc' => $desc,
84
                'public' => $is_public,
85
                'maxusers' => $max_users,
86
                'approval' => $approval,
87
                'owner' => $owner,
88
                'members' => $members,
89
            ]),
90
        ]);
91
92
        return $response ? $response['data'] : false;
93
    }
94
95
    /**
96
     * Update group.
97
     *
98
     * @param string      $group_id
99
     * @param null|string $groupname
100
     * @param null|string $description
101
     * @param null|int    $maxusers
102
     *
103
     * @return bool
104
     */
105
    public function update($group_id, $groupname = null, $description = null, $maxusers = null)
106
    {
107
        $args = array_filter(compact('groupname', 'description', 'maxusers'));
108
        if (empty($args)) {
109
            throw new \BadMethodCallException('Empty parameter not allowed.');
110
        }
111
        $response = $this->put("chatgroups/{$group_id}", [
112
            'body' => json_encode($args),
113
        ]);
114
115
        return $response ? $response['data'] : false;
116
    }
117
118
    /**
119
     * Remove a group.
120
     *
121
     * @param string $group_id
122
     *
123
     * @return bool
124
     */
125
    public function remove($group_id)
126
    {
127
        $response = $this->delete("chatgroups/{$group_id}");
128
129
        return $response ? $response['data']['success'] : false;
130
    }
131
132
    /**
133
     * Get group's members.
134
     *
135
     * @param string $group_id
136
     *
137
     * @return bool|array
138
     */
139
    public function members($group_id)
140
    {
141
        $response = $this->get("chatgroups/{$group_id}/users");
142
143
        return $response ? $response['data'] : false;
144
    }
145
146
    /**
147
     * Join one user to a group.
148
     *
149
     * @param string $group_id
150
     * @param string $username
151
     *
152
     * @return bool
153
     */
154
    public function join($group_id, $username)
155
    {
156
        $response = $this->post("chatgroups/{$group_id}/users/{$username}");
157
158
        return $response ? $response['data']['result'] : false;
159
    }
160
161
    /**
162
     * Join multiple users to a group.
163
     *
164
     * @param string $group_id
165
     * @param array  $users
166
     *
167
     * @return bool|array
168
     */
169
    public function batchJoin($group_id, array $users)
170
    {
171
        $response = $this->post("chatgroups/{$group_id}/users", [
172
            'body' => json_encode([
173
                'usernames' => $users,
174
            ]),
175
        ]);
176
177
        return $response ? $response['data'] : false;
178
    }
179
180
    /**
181
     * Remove one member from group.
182
     *
183
     * @param string $group_id
184
     * @param string $username
185
     *
186
     * @return bool
187
     */
188
    public function out($group_id, $username)
189
    {
190
        $response = $this->delete("chatgroups/{$group_id}/users/{$username}");
191
192
        return $response ? $response['data']['result'] : false;
193
    }
194
195
    /**
196
     * @param string $group_id
197
     * @param array  $users
198
     *
199
     * @return bool|array
200
     */
201
    public function batchOut($group_id, array $users)
202
    {
203
        $users = implode(',', $users);
204
        $response = $this->delete("chatgroups/{$group_id}/users/{$users}");
205
206
        return $response ? $response['data'] : false;
207
    }
208
209
    /**
210
     * Get user joined groups.
211
     *
212
     * @param string $username
213
     *
214
     * @return bool|array
215
     */
216
    public function getUserGroups($username)
217
    {
218
        $response = $this->get("users/{$username}/joined_chatgroups");
219
220
        return $response ? $response['data'] : false;
221
    }
222
223
    /**
224
     * Transfer group owner.
225
     *
226
     * @param string $group_id
227
     * @param string $username
228
     *
229
     * @return bool
230
     */
231
    public function transfer($group_id, $username)
232
    {
233
        $response = $this->put("chatgroups/{$group_id}", [
234
            'body' => json_encode([
235
                'newowner' => $username,
236
            ]),
237
        ]);
238
239
        return $response ? $response['data']['newowner'] : false;
240
    }
241
242
    /**
243
     * Cat the group blocked users.
244
     *
245
     * @param string $group_id
246
     *
247
     * @return bool|array
248
     */
249
    public function blocks($group_id)
250
    {
251
        $response = $this->get("chatgroups/{$group_id}/blocks/users");
252
253
        return $response ? $response['data'] : false;
254
    }
255
256
    /**
257
     * Block user.
258
     *
259
     * @param string $group_id
260
     * @param string $username
261
     *
262
     * @return bool
263
     */
264
    public function block($group_id, $username)
265
    {
266
        $response = $this->post("chatgroups/{$group_id}/blocks/users/{$username}");
267
268
        return $response ? $response['data']['result'] : false;
269
    }
270
271
    /**
272
     * @param string $group_id
273
     * @param array  $users
274
     *
275
     * @return bool|array
276
     */
277
    public function batchBlock($group_id, array $users)
278
    {
279
        $response = $this->post("chatgroups/{$group_id}/blocks/users", [
280
            'body' => json_encode([
281
                'usernames' => $users,
282
            ]),
283
        ]);
284
285
        return $response ? $response['data'] : false;
286
    }
287
288
    /**
289
     * @param string $group_id
290
     * @param string $username
291
     *
292
     * @return bool
293
     */
294
    public function unblock($group_id, $username)
295
    {
296
        $response = $this->delete("chatgroups/{$group_id}/blocks/users/{$username}");
297
298
        return $response ? $response['data']['result'] : false;
299
    }
300
301
    /**
302
     * @param string $group_id
303
     * @param array  $users
304
     *
305
     * @return bool|array
306
     */
307
    public function batchUnblock($group_id, array $users)
308
    {
309
        $users = implode(',', $users);
310
        $response = $this->delete("chatgroups/{$group_id}/blocks/users/{$users}");
311
312
        return $response ? $response['data'] : false;
313
    }
314
}
315