Completed
Pull Request — master (#33)
by
unknown
04:44
created

RoomAPI::getRoomMembers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace GorkaLaucirica\HipchatAPIv2Client\API;
4
5
use GorkaLaucirica\HipchatAPIv2Client\Client;
6
use GorkaLaucirica\HipchatAPIv2Client\Model\Message;
7
use GorkaLaucirica\HipchatAPIv2Client\Model\Room;
8
use GorkaLaucirica\HipchatAPIv2Client\Model\Webhook;
9
10
11
class RoomAPI
12
{
13
    /** @var Client */
14
    protected $client;
15
16
    /**
17
     * Room api constructor
18
     *
19
     * @param Client $client that will be used to connect the server
20
     */
21
    public function __construct(Client $client)
22
    {
23
        $this->client = $client;
24
    }
25
26
    /**
27
     * List non-archived rooms for this group
28
     * More info: https://www.hipchat.com/docs/apiv2/method/get_all_rooms
29
     *
30
     * @param array $params Query string parameter(s), for example: array('max-results' => 30)
31
     *
32
     * @return array
33
     */
34
    public function getRooms($params = array())
35
    {
36
        $response = $this->client->get("/v2/room", $params);
37
38
        $rooms = array();
39
        foreach ($response['items'] as $response) {
40
            $rooms[] = new Room($response);
41
        }
42
        return $rooms;
43
    }
44
45
    /**
46
     * Gets room by id or name
47
     * More info: https://www.hipchat.com/docs/apiv2/method/get_room
48
     *
49
     * @param string $id The id or name of the room
50
     *
51
     * @return Room
52
     */
53
    public function getRoom($id)
54
    {
55
        $response = $this->client->get("/v2/room/$id");
56
57
        return new Room($response);
58
    }
59
    
60
    public function getRoomMembers($id)
61
    {
62
        $response = $this->client->get("/v2/room/$id/member");
63
        
64
        return $response['items'];
65
    }
66
67
    /**
68
     * Fetch latest chat history for this room.
69
     * More info: https://www.hipchat.com/docs/apiv2/method/view_recent_room_history
70
     *
71
     * @param string $id The id or name of the room
72
     * @param array $params Query string parameter(s), for example: array('max-results' => 30)
73
     *
74
     * @return array
75
     */
76
    public function getRecentHistory($id, $params = array())
77
    {
78
        $response = $this->client->get(sprintf('/v2/room/%s/history/latest', $id), $params);
79
80
        $messages = array();
81
        foreach ($response['items'] as $response) {
82
            $messages[] = new Message($response);
83
        }
84
        return $messages;
85
    }
86
87
    /**
88
     * Creates a room
89
     * More info: https://www.hipchat.com/docs/apiv2/method/create_room
90
     *
91
     * @param Room $room New room to be persisted
92
     *
93
     * @return integer Just created room id
94
     */
95
    public function createRoom(Room $room)
96
    {
97
        $response = $this->client->post("/v2/room", $room->toJson());
98
99
        return $response['id'];
100
    }
101
102
    /**
103
     * Updates a room
104
     * More info: https://www.hipchat.com/docs/apiv2/method/update_room
105
     *
106
     * @param Room $room Existing room to be updated
107
     *
108
     * @return void
109
     */
110
    public function updateRoom(Room $room)
111
    {
112
        $this->client->put(sprintf("/v2/room/%s", $room->getId()), $room->toJson());
113
    }
114
115
    /**
116
     * Deletes a room and kicks the current participants.
117
     * More info: https://www.hipchat.com/docs/apiv2/method/delete_room
118
     *
119
     * @param string $id The id or name of the room.
120
     *
121
     * @return void
122
     */
123
    public function deleteRoom($id)
124
    {
125
        $this->client->delete(sprintf('/v2/room/%s', $id));
126
    }
127
128
    /**
129
     * Send a room a notification
130
     * More info: https://www.hipchat.com/docs/apiv2/method/send_room_notification
131
     *
132
     * @param string $id The id or name of the room
133
     * @param Message $message The message to be sent
134
     *
135
     * @return void
136
     */
137
    public function sendRoomNotification($id, Message $message)
138
    {
139
        $this->client->post("/v2/room/$id/notification", $message->toJson());
140
    }
141
142
    /**
143
     * Adds a member to a private room
144
     * More info: https://www.hipchat.com/docs/apiv2/method/add_member
145
     *
146
     * @param string $roomId The id or name of the room
147
     * @param string $memberId The id, email address, or mention name (beginning with an '@') of the user
148
     *
149
     * @return void
150
     */
151
    public function addMember($roomId, $memberId)
152
    {
153
        $this->client->put(sprintf('/v2/room/%s/member/%s', $roomId, $memberId));
154
    }
155
156
    /**
157
     * Removes a member from a private room.
158
     * More info: https://www.hipchat.com/docs/apiv2/method/remove_member
159
     *
160
     * @param string $roomId The id, email address, or mention name (beginning with an '@') of the user
161
     * @param string $memberId The id or name of the room
162
     *
163
     * @return void
164
     */
165
    public function removeMember($roomId, $memberId)
166
    {
167
        $this->client->delete(sprintf('/v2/room/%s/member/%s', $roomId, $memberId));
168
    }
169
170
    /**
171
     * Invites a member to a public room
172
     * More info: https://www.hipchat.com/docs/apiv2/method/invite_user
173
     *
174
     * @param string $roomId The id or name of the room
175
     * @param string $memberId The id, email address, or mention name (beginning with an '@') of the user
176
     * @param string $reason The message displayed to a user when they are invited
177
     *
178
     * @return void
179
     */
180
    public function inviteUser($roomId, $memberId, $reason)
181
    {
182
        $this->client->post(sprintf('/v2/room/%s/invite/%s', $roomId, $memberId), array('reason' => $reason));
183
    }
184
185
    /**
186
     * Set a topic on a room
187
     * More info: https://www.hipchat.com/docs/apiv2/method/set_topic
188
     *
189
     * @param string $roomId The id or name of the room
190
     * @param string $topic The topic to be set
191
     *
192
     * @return void
193
     */
194
    public function setTopic($roomId, $topic)
195
    {
196
        $this->client->put(sprintf('/v2/room/%s/topic', $roomId), array('topic' => $topic));
197
    }
198
199
    /**
200
     * Creates a new webhook
201
     * More info: https://www.hipchat.com/docs/apiv2/method/create_webhook
202
     *
203
     * @param string $roomId The id or name of the room
204
     * @param Webhook $webhook The webhook to create
205
     *
206
     * @return int Just created webhook id
207
     */
208
    public function createWebhook($roomId, Webhook $webhook)
209
    {
210
        $response = $this->client->post(sprintf('/v2/room/%s/webhook', $roomId), $webhook->toJson());
211
212
        return $response['id'];
213
    }
214
215
    /**
216
     * Deletes a new webhook
217
     * More info: https://www.hipchat.com/docs/apiv2/method/delete_webhook
218
     *
219
     * @param string $roomId The id or name of the room
220
     * @param string $webhookId The id of the webhook to delete
221
     *
222
     * @return void
223
     */
224
    public function deleteWebhook($roomId, $webhookId)
225
    {
226
        $this->client->delete(sprintf('/v2/room/%s/webhook/%s', $roomId, $webhookId));
227
    }
228
229
    /**
230
     * Gets all webhooks for this room
231
     * More info: https://www.hipchat.com/docs/apiv2/method/get_all_webhooks
232
     *
233
     * @param string $roomId The id or name of the room
234
     *
235
     * @TODO should return a Collection
236
     * @return array Array of Webhook
237
     */
238
    public function getAllWebhooks($roomId)
239
    {
240
        $webhooks = array();
241
        $response = $this->client->get(sprintf('/v2/room/%s/webhook', $roomId));
242
        foreach ($response['items'] as $item) {
243
            $webhooks[] = new Webhook($item);
244
        }
245
246
        return $webhooks;
247
    }
248
}
249