Group   B
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 301
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 0%

Importance

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

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