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 Rtm extends AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @param bool $simple_latest |
24
|
|
|
* @param bool $no_unreads |
25
|
|
|
* @param bool $mpim_aware |
26
|
|
|
* @return Reminders |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function start($simple_latest = NULL, $no_unreads = NULL, $mpim_aware = NULL) { |
|
|
|
|
29
|
|
|
|
30
|
|
|
// Check if the type of the variables is valid. |
31
|
|
|
if (($simple_latest != NULL) && !is_bool($simple_latest)) { |
32
|
|
|
throw new InvalidArgumentException("The type of the simple_latest variable is not valid."); |
33
|
|
|
} |
34
|
|
|
if (($no_unreads != NULL) && !is_bool($no_unreads)) { |
35
|
|
|
throw new InvalidArgumentException("The type of the no_unreads variable is not valid."); |
36
|
|
|
} |
37
|
|
|
if (($mpim_aware != NULL) && !is_bool($mpim_aware)) { |
38
|
|
|
throw new InvalidArgumentException("The type of the mpim_aware variable is not valid."); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Set the arguments of the request |
42
|
|
|
$arguments = array(); |
43
|
|
|
|
44
|
|
|
if ($simple_latest != NULL) { |
45
|
|
|
$arguments["simple_latest"] = $simple_latest; |
46
|
|
|
} |
47
|
|
|
if ($no_unreads != NULL) { |
48
|
|
|
$arguments["no_unreads"] = $no_unreads; |
49
|
|
|
} |
50
|
|
|
if ($mpim_aware != NULL) { |
51
|
|
|
$arguments["mpim_aware"] = $mpim_aware; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->setUrl("rtm.start", $arguments); |
55
|
|
|
|
56
|
|
|
// Send the request |
57
|
|
|
try { |
58
|
|
|
$client = new \GuzzleHttp\Client(); |
59
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
60
|
|
|
$response = json_decode( $json_response->getBody() ); |
61
|
|
|
} |
62
|
|
|
catch (RequestException $e) { |
63
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if($response->{'ok'} === FALSE) { |
67
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $json_response->getBody(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
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.