RoomAPI   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 232
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRooms() 0 10 2
A getRoom() 0 6 1
A getRecentHistory() 0 10 2
A createRoom() 0 6 1
A updateRoom() 0 4 1
A deleteRoom() 0 4 1
A sendRoomNotification() 0 5 1
A addMember() 0 4 1
A removeMember() 0 4 1
A inviteUser() 0 4 1
A setTopic() 0 4 1
A createWebhook() 0 6 1
A deleteWebhook() 0 4 1
A getAllWebhooks() 0 10 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
     * Creates a room
82
     * More info: https://www.hipchat.com/docs/apiv2/method/create_room
83
     *
84
     * @param Room $room New room to be persisted
85
     *
86
     * @return integer Just created room id
87
     */
88
    public function createRoom(Room $room)
89
    {
90
        $response = $this->client->post("/v2/room", $room->toJson());
91
92
        return $response['id'];
93
    }
94
95
    /**
96
     * Updates a room
97
     * More info: https://www.hipchat.com/docs/apiv2/method/update_room
98
     *
99
     * @param Room $room Existing room to be updated
100
     *
101
     * @return void
102
     */
103
    public function updateRoom(Room $room)
104
    {
105
        $this->client->put(sprintf("/v2/room/%s", $room->getId()), $room->toJson());
106
    }
107
108
    /**
109
     * Deletes a room and kicks the current participants.
110
     * More info: https://www.hipchat.com/docs/apiv2/method/delete_room
111
     *
112
     * @param string $id The id or name of the room.
113
     *
114
     * @return void
115
     */
116
    public function deleteRoom($id)
117
    {
118
        $this->client->delete(sprintf('/v2/room/%s', $id));
119
    }
120
121
    /**
122
     * Send a room a notification
123
     * More info: https://www.hipchat.com/docs/apiv2/method/send_room_notification
124
     *
125
     * @param string $id The id or name of the room
126
     * @param Message $message The message to be sent
127
     *
128
     * @return void
129
     */
130
    public function sendRoomNotification($id, Message $message)
131
    {
132
        $id = rawurlencode($id);
133
        $this->client->post("/v2/room/$id/notification", $message->toJson());
134
    }
135
136
    /**
137
     * Adds a member to a private room
138
     * More info: https://www.hipchat.com/docs/apiv2/method/add_member
139
     *
140
     * @param string $roomId The id or name of the room
141
     * @param string $memberId The id, email address, or mention name (beginning with an '@') of the user
142
     *
143
     * @return void
144
     */
145
    public function addMember($roomId, $memberId)
146
    {
147
        $this->client->put(sprintf('/v2/room/%s/member/%s', $roomId, $memberId));
148
    }
149
150
    /**
151
     * Removes a member from a private room.
152
     * More info: https://www.hipchat.com/docs/apiv2/method/remove_member
153
     *
154
     * @param string $roomId The id, email address, or mention name (beginning with an '@') of the user
155
     * @param string $memberId The id or name of the room
156
     *
157
     * @return void
158
     */
159
    public function removeMember($roomId, $memberId)
160
    {
161
        $this->client->delete(sprintf('/v2/room/%s/member/%s', $roomId, $memberId));
162
    }
163
164
    /**
165
     * Invites a member to a public room
166
     * More info: https://www.hipchat.com/docs/apiv2/method/invite_user
167
     *
168
     * @param string $roomId The id or name of the room
169
     * @param string $memberId The id, email address, or mention name (beginning with an '@') of the user
170
     * @param string $reason The message displayed to a user when they are invited
171
     *
172
     * @return void
173
     */
174
    public function inviteUser($roomId, $memberId, $reason)
175
    {
176
        $this->client->post(sprintf('/v2/room/%s/invite/%s', $roomId, $memberId), array('reason' => $reason));
177
    }
178
179
    /**
180
     * Set a topic on a room
181
     * More info: https://www.hipchat.com/docs/apiv2/method/set_topic
182
     *
183
     * @param string $roomId The id or name of the room
184
     * @param string $topic The topic to be set
185
     *
186
     * @return void
187
     */
188
    public function setTopic($roomId, $topic)
189
    {
190
        $this->client->put(sprintf('/v2/room/%s/topic', $roomId), array('topic' => $topic));
191
    }
192
193
    /**
194
     * Creates a new webhook
195
     * More info: https://www.hipchat.com/docs/apiv2/method/create_webhook
196
     *
197
     * @param string $roomId The id or name of the room
198
     * @param Webhook $webhook The webhook to create
199
     *
200
     * @return int Just created webhook id
201
     */
202
    public function createWebhook($roomId, Webhook $webhook)
203
    {
204
        $response = $this->client->post(sprintf('/v2/room/%s/webhook', $roomId), $webhook->toJson());
205
206
        return $response['id'];
207
    }
208
209
    /**
210
     * Deletes a new webhook
211
     * More info: https://www.hipchat.com/docs/apiv2/method/delete_webhook
212
     *
213
     * @param string $roomId The id or name of the room
214
     * @param string $webhookId The id of the webhook to delete
215
     *
216
     * @return void
217
     */
218
    public function deleteWebhook($roomId, $webhookId)
219
    {
220
        $this->client->delete(sprintf('/v2/room/%s/webhook/%s', $roomId, $webhookId));
221
    }
222
223
    /**
224
     * Gets all webhooks for this room
225
     * More info: https://www.hipchat.com/docs/apiv2/method/get_all_webhooks
226
     *
227
     * @param string $roomId The id or name of the room
228
     *
229
     * @TODO should return a Collection
230
     * @return array Array of Webhook
231
     */
232
    public function getAllWebhooks($roomId)
233
    {
234
        $webhooks = array();
235
        $response = $this->client->get(sprintf('/v2/room/%s/webhook', $roomId));
236
        foreach ($response['items'] as $item) {
237
            $webhooks[] = new Webhook($item);
238
        }
239
240
        return $webhooks;
241
    }
242
}
243