1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Slackify. |
5
|
|
|
* |
6
|
|
|
* (c) Strime <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Strime\Slackify\Api; |
13
|
|
|
|
14
|
|
|
use Strime\Slackify\Exception\RuntimeException; |
15
|
|
|
use Strime\Slackify\Exception\InvalidArgumentException; |
16
|
|
|
use GuzzleHttp\Exception\RequestException; |
17
|
|
|
|
18
|
|
|
class UserGroups extends AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @param string $name |
24
|
|
|
* @param string $handle |
25
|
|
|
* @param string $description |
26
|
|
|
* @param string $channels |
27
|
|
|
* @param bool $include_count |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function create($name, $handle = NULL, $description = NULL, $channels = NULL, $include_count = NULL) { |
31
|
|
|
|
32
|
|
|
// Check if the type of the variables is valid. |
33
|
|
|
if (!is_string($name) || ($name == NULL)) { |
34
|
|
|
throw new InvalidArgumentException("The type of the name variable is not valid."); |
35
|
|
|
} |
36
|
|
|
if (!is_string($handle) && ($name != NULL)) { |
37
|
|
|
throw new InvalidArgumentException("The type of the handle variable is not valid."); |
38
|
|
|
} |
39
|
|
|
if (!is_string($description) && ($description != NULL)) { |
40
|
|
|
throw new InvalidArgumentException("The type of the description variable is not valid."); |
41
|
|
|
} |
42
|
|
|
if (!is_string($channels) && ($channels != NULL)) { |
43
|
|
|
throw new InvalidArgumentException("The type of the channels variable is not valid."); |
44
|
|
|
} |
45
|
|
|
if (!is_bool($include_count) && ($include_count != NULL)) { |
46
|
|
|
throw new InvalidArgumentException("The type of the channels variable is not valid."); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Set the arguments of the request |
50
|
|
|
$arguments = array( |
51
|
|
|
"name" => $name |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
if ($handle != NULL) { |
|
|
|
|
55
|
|
|
$arguments["handle"] = $handle; |
56
|
|
|
} |
57
|
|
|
if ($description != NULL) { |
|
|
|
|
58
|
|
|
$arguments["description"] = $description; |
59
|
|
|
} |
60
|
|
|
if ($channels != NULL) { |
|
|
|
|
61
|
|
|
$arguments["channels"] = $channels; |
62
|
|
|
} |
63
|
|
|
if ($include_count != NULL) { |
64
|
|
|
$arguments["include_count"] = $include_count; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->setUrl("usergroups.create", $arguments); |
68
|
|
|
|
69
|
|
|
// Send the request |
70
|
|
|
try { |
71
|
|
|
$client = new \GuzzleHttp\Client(); |
72
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
73
|
|
|
$response = json_decode( $json_response->getBody() ); |
74
|
|
|
} |
75
|
|
|
catch (RequestException $e) { |
76
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if($response->{'ok'} === FALSE) { |
80
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $json_response->getBody(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
* |
92
|
|
|
* @param string $usergroup |
93
|
|
|
* @param bool $include_count |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function disable($usergroup, $include_count = NULL) { |
|
|
|
|
97
|
|
|
|
98
|
|
|
// Check if the type of the variables is valid. |
99
|
|
|
if (!is_string($usergroup) || ($usergroup != NULL)) { |
100
|
|
|
throw new InvalidArgumentException("The type of the usergroup variable is not valid."); |
101
|
|
|
} |
102
|
|
|
if (!is_bool($include_count) && ($include_count != NULL)) { |
103
|
|
|
throw new InvalidArgumentException("The type of the include_count variable is not valid."); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Set the arguments of the request |
107
|
|
|
$arguments = array( |
108
|
|
|
"usergroup" => $usergroup |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
if ($include_count != NULL) { |
112
|
|
|
$arguments["include_count"] = $include_count; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->setUrl("usergroups.disable", $arguments); |
116
|
|
|
|
117
|
|
|
// Send the request |
118
|
|
|
try { |
119
|
|
|
$client = new \GuzzleHttp\Client(); |
120
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
121
|
|
|
$response = json_decode( $json_response->getBody() ); |
122
|
|
|
} |
123
|
|
|
catch (RequestException $e) { |
124
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if($response->{'ok'} === FALSE) { |
128
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $json_response->getBody(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
* |
140
|
|
|
* @param string $usergroup |
141
|
|
|
* @param bool $include_count |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function enable($usergroup, $include_count = NULL) { |
|
|
|
|
145
|
|
|
|
146
|
|
|
// Check if the type of the variables is valid. |
147
|
|
|
if (!is_string($usergroup) || ($usergroup != NULL)) { |
148
|
|
|
throw new InvalidArgumentException("The type of the usergroup variable is not valid."); |
149
|
|
|
} |
150
|
|
|
if (!is_bool($include_count) && ($include_count != NULL)) { |
151
|
|
|
throw new InvalidArgumentException("The type of the include_count variable is not valid."); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Set the arguments of the request |
155
|
|
|
$arguments = array( |
156
|
|
|
"usergroup" => $usergroup |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
if ($include_count != NULL) { |
160
|
|
|
$arguments["include_count"] = $include_count; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$this->setUrl("usergroups.enable", $arguments); |
164
|
|
|
|
165
|
|
|
// Send the request |
166
|
|
|
try { |
167
|
|
|
$client = new \GuzzleHttp\Client(); |
168
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
169
|
|
|
$response = json_decode( $json_response->getBody() ); |
170
|
|
|
} |
171
|
|
|
catch (RequestException $e) { |
172
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if($response->{'ok'} === FALSE) { |
176
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $json_response->getBody(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
|
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
* |
189
|
|
|
* @param bool $include_disabled |
190
|
|
|
* @param bool $include_count |
191
|
|
|
* @param bool $include_users |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
View Code Duplication |
public function list_usergroups($include_disabled = NULL, $include_count = NULL, $include_users = NULL) { |
|
|
|
|
195
|
|
|
|
196
|
|
|
// Check if the type of the variables is valid. |
197
|
|
|
if (!is_bool($include_disabled) && ($include_disabled != NULL)) { |
198
|
|
|
throw new InvalidArgumentException("The type of the include_disabled variable is not valid."); |
199
|
|
|
} |
200
|
|
|
if (!is_bool($include_count) && ($include_count != NULL)) { |
201
|
|
|
throw new InvalidArgumentException("The type of the include_count variable is not valid."); |
202
|
|
|
} |
203
|
|
|
if (!is_bool($include_users) && ($include_users != NULL)) { |
204
|
|
|
throw new InvalidArgumentException("The type of the include_users variable is not valid."); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// Set the arguments of the request |
208
|
|
|
$arguments = array(); |
209
|
|
|
|
210
|
|
|
if ($include_disabled != NULL) { |
211
|
|
|
$arguments["include_disabled"] = $include_disabled; |
212
|
|
|
} |
213
|
|
|
if ($include_count != NULL) { |
214
|
|
|
$arguments["include_count"] = $include_count; |
215
|
|
|
} |
216
|
|
|
if ($include_users != NULL) { |
217
|
|
|
$arguments["include_users"] = $include_users; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->setUrl("usergroups.list", $arguments); |
221
|
|
|
|
222
|
|
|
// Send the request |
223
|
|
|
try { |
224
|
|
|
$client = new \GuzzleHttp\Client(); |
225
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
226
|
|
|
$response = json_decode( $json_response->getBody() ); |
227
|
|
|
} |
228
|
|
|
catch (RequestException $e) { |
229
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if($response->{'ok'} === FALSE) { |
233
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $json_response->getBody(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
|
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
* |
245
|
|
|
* @param string $usergroup |
246
|
|
|
* @param string $name |
247
|
|
|
* @param string $handle |
248
|
|
|
* @param string $description |
249
|
|
|
* @param string $channels |
250
|
|
|
* @param bool $include_count |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function update($usergroup, $name = NULL, $handle = NULL, $description = NULL, $channels = NULL, $include_count = NULL) { |
254
|
|
|
|
255
|
|
|
// Check if the type of the variables is valid. |
256
|
|
|
if (!is_string($usergroup) || ($usergroup == NULL)) { |
257
|
|
|
throw new InvalidArgumentException("The type of the usergroup variable is not valid."); |
258
|
|
|
} |
259
|
|
|
if (!is_string($name) && ($name != NULL)) { |
260
|
|
|
throw new InvalidArgumentException("The type of the name variable is not valid."); |
261
|
|
|
} |
262
|
|
|
if (!is_string($handle) && ($name != NULL)) { |
|
|
|
|
263
|
|
|
throw new InvalidArgumentException("The type of the handle variable is not valid."); |
264
|
|
|
} |
265
|
|
|
if (!is_string($description) && ($description != NULL)) { |
266
|
|
|
throw new InvalidArgumentException("The type of the description variable is not valid."); |
267
|
|
|
} |
268
|
|
|
if (!is_string($channels) && ($channels != NULL)) { |
269
|
|
|
throw new InvalidArgumentException("The type of the channels variable is not valid."); |
270
|
|
|
} |
271
|
|
|
if (!is_bool($include_count) && ($include_count != NULL)) { |
272
|
|
|
throw new InvalidArgumentException("The type of the include_count variable is not valid."); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
// Set the arguments of the request |
276
|
|
|
$arguments = array( |
277
|
|
|
"usergroup" => $usergroup |
278
|
|
|
); |
279
|
|
|
|
280
|
|
|
if ($name != NULL) { |
|
|
|
|
281
|
|
|
$arguments["name"] = $name; |
282
|
|
|
} |
283
|
|
|
if ($handle != NULL) { |
|
|
|
|
284
|
|
|
$arguments["handle"] = $handle; |
285
|
|
|
} |
286
|
|
|
if ($description != NULL) { |
|
|
|
|
287
|
|
|
$arguments["description"] = $description; |
288
|
|
|
} |
289
|
|
|
if ($channels != NULL) { |
|
|
|
|
290
|
|
|
$arguments["channels"] = $channels; |
291
|
|
|
} |
292
|
|
|
if ($include_count != NULL) { |
293
|
|
|
$arguments["include_count"] = $include_count; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$this->setUrl("usergroups.update", $arguments); |
297
|
|
|
|
298
|
|
|
// Send the request |
299
|
|
|
try { |
300
|
|
|
$client = new \GuzzleHttp\Client(); |
301
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
302
|
|
|
$response = json_decode( $json_response->getBody() ); |
303
|
|
|
} |
304
|
|
|
catch (RequestException $e) { |
305
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if($response->{'ok'} === FALSE) { |
309
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $json_response->getBody(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
|
316
|
|
|
|
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* {@inheritdoc} |
320
|
|
|
* |
321
|
|
|
* @param string $usergroup |
322
|
|
|
* @param bool $include_disabled |
323
|
|
|
* @return string |
324
|
|
|
*/ |
325
|
|
View Code Duplication |
public function usersList($usergroup, $include_disabled = NULL) { |
|
|
|
|
326
|
|
|
|
327
|
|
|
// Check if the type of the variables is valid. |
328
|
|
|
if (!is_string($usergroup) || ($usergroup == NULL)) { |
329
|
|
|
throw new InvalidArgumentException("The type of the usergroup variable is not valid."); |
330
|
|
|
} |
331
|
|
|
if (!is_bool($include_disabled) && ($include_disabled != NULL)) { |
332
|
|
|
throw new InvalidArgumentException("The type of the include_disabled variable is not valid."); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// Set the arguments of the request |
336
|
|
|
$arguments = array( |
337
|
|
|
"usergroup" => $usergroup |
338
|
|
|
); |
339
|
|
|
|
340
|
|
|
if ($include_disabled != NULL) { |
341
|
|
|
$arguments["include_disabled"] = $include_disabled; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
$this->setUrl("usergroups.users.list", $arguments); |
345
|
|
|
|
346
|
|
|
// Send the request |
347
|
|
|
try { |
348
|
|
|
$client = new \GuzzleHttp\Client(); |
349
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
350
|
|
|
$response = json_decode( $json_response->getBody() ); |
351
|
|
|
} |
352
|
|
|
catch (RequestException $e) { |
353
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if($response->{'ok'} === FALSE) { |
357
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $json_response->getBody(); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
|
364
|
|
|
|
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* {@inheritdoc} |
368
|
|
|
* |
369
|
|
|
* @param string $usergroup |
370
|
|
|
* @param string $users |
371
|
|
|
* @param bool $include_count |
|
|
|
|
372
|
|
|
* @return string |
373
|
|
|
*/ |
374
|
|
|
public function usersUpdate($usergroup, $users, $include_disabled = NULL) { |
|
|
|
|
375
|
|
|
|
376
|
|
|
// Check if the type of the variables is valid. |
377
|
|
|
if (!is_string($usergroup) || ($usergroup == NULL)) { |
378
|
|
|
throw new InvalidArgumentException("The type of the usergroup variable is not valid."); |
379
|
|
|
} |
380
|
|
|
if (!is_string($users) || ($users == NULL)) { |
381
|
|
|
throw new InvalidArgumentException("The type of the users variable is not valid."); |
382
|
|
|
} |
383
|
|
|
if (!is_bool($include_count) && ($include_count != NULL)) { |
|
|
|
|
384
|
|
|
throw new InvalidArgumentException("The type of the include_count variable is not valid."); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
// Set the arguments of the request |
388
|
|
|
$arguments = array( |
389
|
|
|
"usergroup" => $usergroup, |
390
|
|
|
"users" => $users |
391
|
|
|
); |
392
|
|
|
|
393
|
|
|
if ($include_count != NULL) { |
394
|
|
|
$arguments["include_count"] = $include_count; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
$this->setUrl("usergroups.users.update", $arguments); |
398
|
|
|
|
399
|
|
|
// Send the request |
400
|
|
|
try { |
401
|
|
|
$client = new \GuzzleHttp\Client(); |
402
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
403
|
|
|
$response = json_decode( $json_response->getBody() ); |
404
|
|
|
} |
405
|
|
|
catch (RequestException $e) { |
406
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
if($response->{'ok'} === FALSE) { |
410
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return $json_response->getBody(); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|