Completed
Pull Request — master (#35)
by
unknown
05:36
created

RoomAPI::getHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
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
    /**
61
     * Fetch latest chat history for this room.
62
     * More info: https://www.hipchat.com/docs/apiv2/method/view_recent_room_history
63
     *
64
     * @param string $id The id or name of the room
65
     * @param array $params Query string parameter(s), for example: array('max-results' => 30)
66
     *
67
     * @return array
68
     */
69
    public function getRecentHistory($id, $params = array())
70
    {
71
        $response = $this->client->get(sprintf('/v2/room/%s/history/latest', $id), $params);
72
73
        $messages = array();
74
        foreach ($response['items'] as $response) {
75
            $messages[] = new Message($response);
76
        }
77
        return $messages;
78
    }
79
80
    /**
81
     * Fetch latest chat history for this room.
82
     * More info: https://www.hipchat.com/docs/apiv2/method/view_recent_room_history
83
     *
84
     * @param string $id The id or name of the room
85
     * @param array $params Query string parameter(s), for example: array('max-results' => 30)
86
     *
87
     * @return array
88
     */
89
    public function getHistory($id, $params = array())
90
    {
91
        $response = $this->client->get(sprintf('/v2/room/%s/history', $id), $params);
92
93
        $messages = array();
94
        foreach ($response['items'] as $response) {
95
            $messages[] = new Message($response);
96
        }
97
        return $messages;
98
    }
99
100
    /**
101
     * Creates a room
102
     * More info: https://www.hipchat.com/docs/apiv2/method/create_room
103
     *
104
     * @param Room $room New room to be persisted
105
     *
106
     * @return integer Just created room id
107
     */
108
    public function createRoom(Room $room)
109
    {
110
        $response = $this->client->post("/v2/room", $room->toJson());
111
112
        return $response['id'];
113
    }
114
115
    /**
116
     * Updates a room
117
     * More info: https://www.hipchat.com/docs/apiv2/method/update_room
118
     *
119
     * @param Room $room Existing room to be updated
120
     *
121
     * @return void
122
     */
123
    public function updateRoom(Room $room)
124
    {
125
        $this->client->put(sprintf("/v2/room/%s", $room->getId()), $room->toJson());
126
    }
127
128
    /**
129
     * Deletes a room and kicks the current participants.
130
     * More info: https://www.hipchat.com/docs/apiv2/method/delete_room
131
     *
132
     * @param string $id The id or name of the room.
133
     *
134
     * @return void
135
     */
136
    public function deleteRoom($id)
137
    {
138
        $this->client->delete(sprintf('/v2/room/%s', $id));
139
    }
140
141
    /**
142
     * Send a room a notification
143
     * More info: https://www.hipchat.com/docs/apiv2/method/send_room_notification
144
     *
145
     * @param string $id The id or name of the room
146
     * @param Message $message The message to be sent
147
     *
148
     * @return void
149
     */
150
    public function sendRoomNotification($id, Message $message)
151
    {
152
        $id = rawurlencode($id);
153
        $this->client->post("/v2/room/$id/notification", $message->toJson());
154
    }
155
156
    /**
157
     * Adds a member to a private room
158
     * More info: https://www.hipchat.com/docs/apiv2/method/add_member
159
     *
160
     * @param string $roomId The id or name of the room
161
     * @param string $memberId The id, email address, or mention name (beginning with an '@') of the user
162
     *
163
     * @return void
164
     */
165
    public function addMember($roomId, $memberId)
166
    {
167
        $this->client->put(sprintf('/v2/room/%s/member/%s', $roomId, $memberId));
168
    }
169
170
    /**
171
     * Removes a member from a private room.
172
     * More info: https://www.hipchat.com/docs/apiv2/method/remove_member
173
     *
174
     * @param string $roomId The id, email address, or mention name (beginning with an '@') of the user
175
     * @param string $memberId The id or name of the room
176
     *
177
     * @return void
178
     */
179
    public function removeMember($roomId, $memberId)
180
    {
181
        $this->client->delete(sprintf('/v2/room/%s/member/%s', $roomId, $memberId));
182
    }
183
184
    /**
185
     * Invites a member to a public room
186
     * More info: https://www.hipchat.com/docs/apiv2/method/invite_user
187
     *
188
     * @param string $roomId The id or name of the room
189
     * @param string $memberId The id, email address, or mention name (beginning with an '@') of the user
190
     * @param string $reason The message displayed to a user when they are invited
191
     *
192
     * @return void
193
     */
194
    public function inviteUser($roomId, $memberId, $reason)
195
    {
196
        $this->client->post(sprintf('/v2/room/%s/invite/%s', $roomId, $memberId), array('reason' => $reason));
197
    }
198
199
    /**
200
     * Set a topic on a room
201
     * More info: https://www.hipchat.com/docs/apiv2/method/set_topic
202
     *
203
     * @param string $roomId The id or name of the room
204
     * @param string $topic The topic to be set
205
     *
206
     * @return void
207
     */
208
    public function setTopic($roomId, $topic)
209
    {
210
        $this->client->put(sprintf('/v2/room/%s/topic', $roomId), array('topic' => $topic));
211
    }
212
213
    /**
214
     * Creates a new webhook
215
     * More info: https://www.hipchat.com/docs/apiv2/method/create_webhook
216
     *
217
     * @param string $roomId The id or name of the room
218
     * @param Webhook $webhook The webhook to create
219
     *
220
     * @return int Just created webhook id
221
     */
222
    public function createWebhook($roomId, Webhook $webhook)
223
    {
224
        $response = $this->client->post(sprintf('/v2/room/%s/webhook', $roomId), $webhook->toJson());
225
226
        return $response['id'];
227
    }
228
229
    /**
230
     * Deletes a new webhook
231
     * More info: https://www.hipchat.com/docs/apiv2/method/delete_webhook
232
     *
233
     * @param string $roomId The id or name of the room
234
     * @param string $webhookId The id of the webhook to delete
235
     *
236
     * @return void
237
     */
238
    public function deleteWebhook($roomId, $webhookId)
239
    {
240
        $this->client->delete(sprintf('/v2/room/%s/webhook/%s', $roomId, $webhookId));
241
    }
242
243
    /**
244
     * Gets all webhooks for this room
245
     * More info: https://www.hipchat.com/docs/apiv2/method/get_all_webhooks
246
     *
247
     * @param string $roomId The id or name of the room
248
     *
249
     * @TODO should return a Collection
250
     * @return array Array of Webhook
251
     */
252
    public function getAllWebhooks($roomId)
253
    {
254
        $webhooks = array();
255
        $response = $this->client->get(sprintf('/v2/room/%s/webhook', $roomId));
256
        foreach ($response['items'] as $item) {
257
            $webhooks[] = new Webhook($item);
258
        }
259
260
        return $webhooks;
261
    }
262
}
263