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 Channels extends AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @param string $channel |
24
|
|
|
* @return Channels |
25
|
|
|
*/ |
26
|
|
|
public function archive($channel) { |
27
|
|
|
|
28
|
|
|
// Check if the type of the variables is valid. |
29
|
|
|
if (!is_string($channel)) { |
30
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Set the arguments of the request |
34
|
|
|
$arguments = array( |
35
|
|
|
"channel" => $channel |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$this->setUrl("channels.archive", $arguments); |
39
|
|
|
|
40
|
|
|
// Send the request |
41
|
|
|
try { |
42
|
|
|
$client = new \GuzzleHttp\Client(); |
43
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
44
|
|
|
$response = json_decode( $json_response->getBody() ); |
45
|
|
|
} |
46
|
|
|
catch (RequestException $e) { |
47
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if($response->{'ok'} === FALSE) { |
51
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
* |
62
|
|
|
* @param string $name |
63
|
|
|
* @return Channels |
64
|
|
|
*/ |
65
|
|
|
public function create($name) { |
66
|
|
|
|
67
|
|
|
// Check if the type of the variables is valid. |
68
|
|
|
if (!is_string($name)) { |
69
|
|
|
throw new InvalidArgumentException("The type of the name variable is not valid."); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Set the arguments of the request |
73
|
|
|
$arguments = array( |
74
|
|
|
"name" => $name |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->setUrl("channels.create", $arguments); |
78
|
|
|
|
79
|
|
|
// Send the request |
80
|
|
|
try { |
81
|
|
|
$client = new \GuzzleHttp\Client(); |
82
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
83
|
|
|
$response = json_decode( $json_response->getBody() ); |
84
|
|
|
} |
85
|
|
|
catch (RequestException $e) { |
86
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if($response->{'ok'} === FALSE) { |
90
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $json_response->getBody(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
* |
101
|
|
|
* @param string $channel |
102
|
|
|
* @param string $latest |
103
|
|
|
* @param integer $oldest |
104
|
|
|
* @param integer $inclusive |
105
|
|
|
* @param integer $count |
106
|
|
|
* @param integer $unreads |
107
|
|
|
* @return Channels |
108
|
|
|
*/ |
109
|
|
|
public function history($channel, $latest = "now", $oldest = 0, $inclusive = 0, $count = 100, $unreads = 0) { |
110
|
|
|
|
111
|
|
|
// Check if the type of the variables is valid. |
112
|
|
|
if (!is_string($channel)) { |
113
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
114
|
|
|
} |
115
|
|
|
if (!is_integer($oldest)) { |
116
|
|
|
throw new InvalidArgumentException("The type of the oldest variable is not valid."); |
117
|
|
|
} |
118
|
|
|
if (!is_integer($inclusive)) { |
119
|
|
|
throw new InvalidArgumentException("The type of the inclusive variable is not valid."); |
120
|
|
|
} |
121
|
|
|
if (!is_integer($count)) { |
122
|
|
|
throw new InvalidArgumentException("The type of the count variable is not valid."); |
123
|
|
|
} |
124
|
|
|
if (!is_integer($unreads)) { |
125
|
|
|
throw new InvalidArgumentException("The type of the unreads variable is not valid."); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Set the arguments of the request |
129
|
|
|
$arguments = array( |
130
|
|
|
"channel" => $channel, |
131
|
|
|
"latest" => $latest, |
132
|
|
|
"oldest" => $oldest, |
133
|
|
|
"inclusive" => $inclusive, |
134
|
|
|
"count" => $count, |
135
|
|
|
"unreads" => $unreads |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$this->setUrl("channels.history", $arguments); |
139
|
|
|
|
140
|
|
|
// Send the request |
141
|
|
|
try { |
142
|
|
|
$client = new \GuzzleHttp\Client(); |
143
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
144
|
|
|
$response = json_decode( $json_response->getBody() ); |
145
|
|
|
} |
146
|
|
|
catch (RequestException $e) { |
147
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if($response->{'ok'} === FALSE) { |
151
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $json_response->getBody(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
* |
162
|
|
|
* @param string $channel |
163
|
|
|
* @return Channels |
164
|
|
|
*/ |
165
|
|
|
public function info($channel) { |
166
|
|
|
|
167
|
|
|
// Check if the type of the variables is valid. |
168
|
|
|
if (!is_string($channel)) { |
169
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Set the arguments of the request |
173
|
|
|
$arguments = array( |
174
|
|
|
"channel" => $channel |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$this->setUrl("channels.info", $arguments); |
178
|
|
|
|
179
|
|
|
// Send the request |
180
|
|
|
try { |
181
|
|
|
$client = new \GuzzleHttp\Client(); |
182
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
183
|
|
|
$response = json_decode( $json_response->getBody() ); |
184
|
|
|
} |
185
|
|
|
catch (RequestException $e) { |
186
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if($response->{'ok'} === FALSE) { |
190
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $json_response->getBody(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritdoc} |
200
|
|
|
* |
201
|
|
|
* @param string $channel |
202
|
|
|
* @param string $user |
203
|
|
|
* @return Channels |
204
|
|
|
*/ |
205
|
|
|
public function invite($channel, $user) { |
206
|
|
|
|
207
|
|
|
// Check if the type of the variables is valid. |
208
|
|
|
if (!is_string($channel)) { |
209
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
210
|
|
|
} |
211
|
|
|
if (!is_string($user)) { |
212
|
|
|
throw new InvalidArgumentException("The type of the user variable is not valid."); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Set the arguments of the request |
216
|
|
|
$arguments = array( |
217
|
|
|
"channel" => $channel, |
218
|
|
|
"user" => $user |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
$this->setUrl("channels.invite", $arguments); |
222
|
|
|
|
223
|
|
|
// Send the request |
224
|
|
|
try { |
225
|
|
|
$client = new \GuzzleHttp\Client(); |
226
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
227
|
|
|
$response = json_decode( $json_response->getBody() ); |
228
|
|
|
} |
229
|
|
|
catch (RequestException $e) { |
230
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if($response->{'ok'} === FALSE) { |
234
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $json_response->getBody(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
* |
245
|
|
|
* @param string $channel |
246
|
|
|
* @return Channels |
247
|
|
|
*/ |
248
|
|
|
public function join($channel) { |
249
|
|
|
|
250
|
|
|
// Check if the type of the variables is valid. |
251
|
|
|
if (!is_string($channel)) { |
252
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// Set the arguments of the request |
256
|
|
|
$arguments = array( |
257
|
|
|
"name" => $channel |
258
|
|
|
); |
259
|
|
|
|
260
|
|
|
$this->setUrl("channels.join", $arguments); |
261
|
|
|
|
262
|
|
|
// Send the request |
263
|
|
|
try { |
264
|
|
|
$client = new \GuzzleHttp\Client(); |
265
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
266
|
|
|
$response = json_decode( $json_response->getBody() ); |
267
|
|
|
} |
268
|
|
|
catch (RequestException $e) { |
269
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if($response->{'ok'} === FALSE) { |
273
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $json_response->getBody(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* {@inheritdoc} |
283
|
|
|
* |
284
|
|
|
* @param string $channel |
285
|
|
|
* @param string $user |
286
|
|
|
* @return Channels |
287
|
|
|
*/ |
288
|
|
|
public function kick($channel, $user) { |
289
|
|
|
|
290
|
|
|
// Check if the type of the variables is valid. |
291
|
|
|
if (!is_string($channel)) { |
292
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
293
|
|
|
} |
294
|
|
|
if (!is_string($user)) { |
295
|
|
|
throw new InvalidArgumentException("The type of the user variable is not valid."); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// Set the arguments of the request |
299
|
|
|
$arguments = array( |
300
|
|
|
"channel" => $channel, |
301
|
|
|
"user" => $user |
302
|
|
|
); |
303
|
|
|
|
304
|
|
|
$this->setUrl("channels.kick", $arguments); |
305
|
|
|
|
306
|
|
|
// Send the request |
307
|
|
|
try { |
308
|
|
|
$client = new \GuzzleHttp\Client(); |
309
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
310
|
|
|
$response = json_decode( $json_response->getBody() ); |
311
|
|
|
} |
312
|
|
|
catch (RequestException $e) { |
313
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
if($response->{'ok'} === FALSE) { |
317
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return $this; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
|
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* {@inheritdoc} |
327
|
|
|
* |
328
|
|
|
* @param string $channel |
329
|
|
|
* @return Channels |
330
|
|
|
*/ |
331
|
|
|
public function leave($channel) { |
332
|
|
|
|
333
|
|
|
// Check if the type of the variables is valid. |
334
|
|
|
if (!is_string($channel)) { |
335
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
// Set the arguments of the request |
339
|
|
|
$arguments = array( |
340
|
|
|
"channel" => $channel |
341
|
|
|
); |
342
|
|
|
|
343
|
|
|
$this->setUrl("channels.leave", $arguments); |
344
|
|
|
|
345
|
|
|
// Send the request |
346
|
|
|
try { |
347
|
|
|
$client = new \GuzzleHttp\Client(); |
348
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
349
|
|
|
$response = json_decode( $json_response->getBody() ); |
350
|
|
|
} |
351
|
|
|
catch (RequestException $e) { |
352
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
if($response->{'ok'} === FALSE) { |
356
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
return $this; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
|
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* {@inheritdoc} |
366
|
|
|
* |
367
|
|
|
* @param string $exclude_archived |
368
|
|
|
* @return Channels |
369
|
|
|
*/ |
370
|
|
|
public function list_channels($exclude_archived = 0) { |
371
|
|
|
|
372
|
|
|
// Check if the type of the variables is valid. |
373
|
|
|
if (!is_integer($exclude_archived)) { |
374
|
|
|
throw new InvalidArgumentException("The type of the exclude_archived variable is not valid."); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
// Set the arguments of the request |
378
|
|
|
$arguments = array( |
379
|
|
|
"exclude_archived" => $exclude_archived |
380
|
|
|
); |
381
|
|
|
|
382
|
|
|
$this->setUrl("channels.list", $arguments); |
383
|
|
|
|
384
|
|
|
// Send the request |
385
|
|
|
try { |
386
|
|
|
$client = new \GuzzleHttp\Client(); |
387
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
388
|
|
|
$response = json_decode( $json_response->getBody() ); |
389
|
|
|
} |
390
|
|
|
catch (RequestException $e) { |
391
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
if($response->{'ok'} === FALSE) { |
395
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return $json_response->getBody(); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
|
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* {@inheritdoc} |
405
|
|
|
* |
406
|
|
|
* @param string $channel |
407
|
|
|
* @param float $ts |
408
|
|
|
* @return Channels |
409
|
|
|
*/ |
410
|
|
|
public function mark($channel, $ts) { |
411
|
|
|
|
412
|
|
|
// Check if the type of the variables is valid. |
413
|
|
|
if (!is_string($channel)) { |
414
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
415
|
|
|
} |
416
|
|
|
if (!is_float($ts)) { |
417
|
|
|
throw new InvalidArgumentException("The type of the ts variable is not valid."); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
// Set the arguments of the request |
421
|
|
|
$arguments = array( |
422
|
|
|
"channel" => $channel, |
423
|
|
|
"ts" => (string)$ts |
424
|
|
|
); |
425
|
|
|
|
426
|
|
|
$this->setUrl("channels.mark", $arguments); |
427
|
|
|
|
428
|
|
|
// Send the request |
429
|
|
|
try { |
430
|
|
|
$client = new \GuzzleHttp\Client(); |
431
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
432
|
|
|
$response = json_decode( $json_response->getBody() ); |
433
|
|
|
} |
434
|
|
|
catch (RequestException $e) { |
435
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
if($response->{'ok'} === FALSE) { |
439
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
return $this; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
|
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* {@inheritdoc} |
449
|
|
|
* |
450
|
|
|
* @param string $channel |
451
|
|
|
* @param string $name |
452
|
|
|
* @param boolean $validate |
453
|
|
|
* @return Channels |
454
|
|
|
*/ |
455
|
|
View Code Duplication |
public function rename($channel, $name, $validate = TRUE) { |
|
|
|
|
456
|
|
|
|
457
|
|
|
// Check if the type of the variables is valid. |
458
|
|
|
if (!is_string($channel)) { |
459
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
460
|
|
|
} |
461
|
|
|
if (!is_string($name)) { |
462
|
|
|
throw new InvalidArgumentException("The type of the name variable is not valid."); |
463
|
|
|
} |
464
|
|
|
if (!is_bool($validate)) { |
465
|
|
|
throw new InvalidArgumentException("The type of the validate variable is not valid."); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
// Set the arguments of the request |
469
|
|
|
$arguments = array( |
470
|
|
|
"channel" => $channel, |
471
|
|
|
"name" => $name, |
472
|
|
|
"validate" => $validate |
473
|
|
|
); |
474
|
|
|
|
475
|
|
|
$this->setUrl("channels.rename", $arguments); |
476
|
|
|
|
477
|
|
|
// Send the request |
478
|
|
|
try { |
479
|
|
|
$client = new \GuzzleHttp\Client(); |
480
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
481
|
|
|
$response = json_decode( $json_response->getBody() ); |
482
|
|
|
} |
483
|
|
|
catch (RequestException $e) { |
484
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
if($response->{'ok'} === FALSE) { |
488
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
return $json_response->getBody(); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
|
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* {@inheritdoc} |
498
|
|
|
* |
499
|
|
|
* @param string $channel |
500
|
|
|
* @param float $thread_ts |
501
|
|
|
* @return Channels |
502
|
|
|
*/ |
503
|
|
|
public function replies($channel, $thread_ts) { |
504
|
|
|
|
505
|
|
|
// Check if the type of the variables is valid. |
506
|
|
|
if (!is_string($channel)) { |
507
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
508
|
|
|
} |
509
|
|
|
if (!is_float($thread_ts)) { |
510
|
|
|
throw new InvalidArgumentException("The type of the thread_ts variable is not valid."); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
// Set the arguments of the request |
514
|
|
|
$arguments = array( |
515
|
|
|
"channel" => $channel, |
516
|
|
|
"thread_ts" => (string)$thread_ts |
517
|
|
|
); |
518
|
|
|
|
519
|
|
|
$this->setUrl("channels.replies", $arguments); |
520
|
|
|
|
521
|
|
|
// Send the request |
522
|
|
|
try { |
523
|
|
|
$client = new \GuzzleHttp\Client(); |
524
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
525
|
|
|
$response = json_decode( $json_response->getBody() ); |
526
|
|
|
} |
527
|
|
|
catch (RequestException $e) { |
528
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
if($response->{'ok'} === FALSE) { |
532
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
return $json_response->getBody(); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
|
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* {@inheritdoc} |
542
|
|
|
* |
543
|
|
|
* @param string $channel |
544
|
|
|
* @param string $purpose |
545
|
|
|
* @return Channels |
546
|
|
|
*/ |
547
|
|
|
public function setPurpose($channel, $purpose) { |
548
|
|
|
|
549
|
|
|
// Check if the type of the variables is valid. |
550
|
|
|
if (!is_string($channel)) { |
551
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
552
|
|
|
} |
553
|
|
|
if (!is_string($purpose)) { |
554
|
|
|
throw new InvalidArgumentException("The type of the purpose variable is not valid."); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
// Set the arguments of the request |
558
|
|
|
$arguments = array( |
559
|
|
|
"channel" => $channel, |
560
|
|
|
"purpose" => $purpose |
561
|
|
|
); |
562
|
|
|
|
563
|
|
|
$this->setUrl("channels.setPurpose", $arguments); |
564
|
|
|
|
565
|
|
|
// Send the request |
566
|
|
|
try { |
567
|
|
|
$client = new \GuzzleHttp\Client(); |
568
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
569
|
|
|
$response = json_decode( $json_response->getBody() ); |
570
|
|
|
} |
571
|
|
|
catch (RequestException $e) { |
572
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
if($response->{'ok'} === FALSE) { |
576
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
return $this; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
|
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* {@inheritdoc} |
586
|
|
|
* |
587
|
|
|
* @param string $channel |
588
|
|
|
* @param string $topic |
589
|
|
|
* @return Channels |
590
|
|
|
*/ |
591
|
|
|
public function setTopic($channel, $topic) { |
592
|
|
|
|
593
|
|
|
// Check if the type of the variables is valid. |
594
|
|
|
if (!is_string($channel)) { |
595
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
596
|
|
|
} |
597
|
|
|
if (!is_string($topic)) { |
598
|
|
|
throw new InvalidArgumentException("The type of the topic variable is not valid."); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
// Set the arguments of the request |
602
|
|
|
$arguments = array( |
603
|
|
|
"channel" => $channel, |
604
|
|
|
"topic" => $topic |
605
|
|
|
); |
606
|
|
|
|
607
|
|
|
$this->setUrl("channels.setTopic", $arguments); |
608
|
|
|
|
609
|
|
|
// Send the request |
610
|
|
|
try { |
611
|
|
|
$client = new \GuzzleHttp\Client(); |
612
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
613
|
|
|
$response = json_decode( $json_response->getBody() ); |
614
|
|
|
} |
615
|
|
|
catch (RequestException $e) { |
616
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
if($response->{'ok'} === FALSE) { |
620
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
return $this; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
|
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* {@inheritdoc} |
630
|
|
|
* |
631
|
|
|
* @param string $channel |
632
|
|
|
* @return Channels |
633
|
|
|
*/ |
634
|
|
|
public function unarchive($channel) { |
635
|
|
|
|
636
|
|
|
// Check if the type of the variables is valid. |
637
|
|
|
if (!is_string($channel)) { |
638
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
// Set the arguments of the request |
642
|
|
|
$arguments = array( |
643
|
|
|
"channel" => $channel |
644
|
|
|
); |
645
|
|
|
|
646
|
|
|
$this->setUrl("channels.unarchive", $arguments); |
647
|
|
|
|
648
|
|
|
// Send the request |
649
|
|
|
try { |
650
|
|
|
$client = new \GuzzleHttp\Client(); |
651
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
652
|
|
|
$response = json_decode( $json_response->getBody() ); |
653
|
|
|
} |
654
|
|
|
catch (RequestException $e) { |
655
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
if($response->{'ok'} === FALSE) { |
659
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
return $this; |
663
|
|
|
} |
664
|
|
|
} |
665
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.