ChatRoom   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 3
cbo 1
dl 0
loc 154
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 6 2
A detail() 0 6 2
A create() 0 14 2
A update() 0 12 3
A remove() 0 6 2
A getUserRooms() 0 6 2
A join() 0 6 2
A batchJoin() 0 14 2
A out() 0 6 2
A batchOut() 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
/**
15
 * @see http://docs.easemob.com/doku.php?id=start:100serverintegration:70chatroommgmt#dokuwiki__top
16
 */
17
class ChatRoom extends Rest
18
{
19
    /**
20
     * @return bool|array
21
     */
22
    public function all()
23
    {
24
        $response = $this->get('chatrooms');
25
26
        return $response ? $response['data'] : false;
27
    }
28
29
    /**
30
     * @param string $room_id
31
     *
32
     * @return bool|array
33
     */
34
    public function detail($room_id)
35
    {
36
        $response = $this->get("chatrooms/{$room_id}");
37
38
        return $response ? $response['data'] : false;
39
    }
40
41
    /**
42
     * @param string $name
43
     * @param string $description
44
     * @param string $owner
45
     * @param int    $maxusers
46
     * @param array  $members
47
     *
48
     * @return bool|string Room id
49
     */
50
    public function create(
51
        $name,
52
        $description,
53
        $owner,
54
        $maxusers = 200,
55
        array $members = []
56
    ) {
57
        $args = compact('name', 'description', 'owner', 'maxusers', 'members');
58
        $response = $this->post('chatrooms', [
59
            'body' => json_encode($args),
60
        ]);
61
62
        return $response ? $response['data']['id'] : false;
63
    }
64
65
    /**
66
     * @param string      $room_id
67
     * @param null|string $name
68
     * @param null|string $description
69
     * @param null|int    $maxusers
70
     *
71
     * @return bool|array
72
     */
73
    public function update($room_id, $name = null, $description = null, $maxusers = null)
74
    {
75
        $args = array_filter(compact('name', 'description', 'maxusers'));
76
        if (empty($args)) {
77
            throw new \BadMethodCallException('Empty parameter not allowed.');
78
        }
79
        $response = $this->put("chatrooms/{$room_id}", [
80
            'body' => json_encode($args),
81
        ]);
82
83
        return $response ? $response['data'] : false;
84
    }
85
86
    /**
87
     * @param string $room_id
88
     *
89
     * @return bool
90
     */
91
    public function remove($room_id)
92
    {
93
        $response = $this->delete("chatrooms/{$room_id}");
94
95
        return $response ? $response['data']['success'] : false;
96
    }
97
98
    /**
99
     * @param string $username
100
     *
101
     * @return bool|array
102
     */
103
    public function getUserRooms($username)
104
    {
105
        $response = $this->get("users/{$username}/joined_chatrooms");
106
107
        return $response ? $response['data'] : false;
108
    }
109
110
    /**
111
     * @param string $room_id
112
     * @param string $username
113
     *
114
     * @return bool
115
     */
116
    public function join($room_id, $username)
117
    {
118
        $response = $this->post("chatrooms/{$room_id}/users/{$username}");
119
120
        return $response ? $response['data']['result'] : false;
121
    }
122
123
    /**
124
     * @param string $room_id
125
     * @param array  $users
126
     *
127
     * @return bool|array
128
     */
129
    public function batchJoin($room_id, array $users)
130
    {
131
        $response = $this->post("chatrooms/{$room_id}/users", [
132
            'body' => json_encode([
133
                'usernames' => $users,
134
            ]),
135
        ]);
136
137
        if (false === $response) {
138
            return false;
139
        }
140
141
        return $response['data']['newmembers'];
142
    }
143
144
    /**
145
     * @param string $room_id
146
     * @param string $username
147
     *
148
     * @return bool
149
     */
150
    public function out($room_id, $username)
151
    {
152
        $response = $this->delete("chatrooms/{$room_id}/users/{$username}");
153
154
        return $response ? $response['data']['result'] : false;
155
    }
156
157
    /**
158
     * @param string $room_id
159
     * @param array  $users
160
     *
161
     * @return bool|array
162
     */
163
    public function batchOut($room_id, array $users)
164
    {
165
        $users = implode(',', $users);
166
        $response = $this->delete("chatrooms/{$room_id}/users/{$users}");
167
168
        return $response ? $response['data'] : false;
169
    }
170
}
171