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 Stars extends AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @param string $file |
24
|
|
|
* @param string $file_comment |
25
|
|
|
* @param string $channel |
26
|
|
|
* @param float $timestamp |
27
|
|
|
* @return Stars |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function add($file = NULL, $file_comment = NULL, $channel = NULL, $timestamp = NULL) { |
|
|
|
|
30
|
|
|
|
31
|
|
|
// Check if the type of the variables is valid. |
32
|
|
|
if (!is_string($file) && ($file != NULL)) { |
33
|
|
|
throw new InvalidArgumentException("The type of the file variable is not valid."); |
34
|
|
|
} |
35
|
|
|
if (!is_string($file_comment) && ($file_comment != NULL)) { |
36
|
|
|
throw new InvalidArgumentException("The type of the file_comment variable is not valid."); |
37
|
|
|
} |
38
|
|
|
if (!is_string($channel) && ($channel != NULL)) { |
39
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
40
|
|
|
} |
41
|
|
|
if (!is_float($timestamp) && ($timestamp != NULL)) { |
42
|
|
|
throw new InvalidArgumentException("The type of the timestamp variable is not valid."); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Set the arguments of the request |
46
|
|
|
$arguments = array(); |
47
|
|
|
|
48
|
|
|
if ($file != NULL) { |
|
|
|
|
49
|
|
|
$arguments["file"] = $file; |
50
|
|
|
} |
51
|
|
|
if ($file_comment != NULL) { |
|
|
|
|
52
|
|
|
$arguments["file_comment"] = $file_comment; |
53
|
|
|
} |
54
|
|
|
if ($channel != NULL) { |
|
|
|
|
55
|
|
|
$arguments["channel"] = $channel; |
56
|
|
|
} |
57
|
|
|
if ($timestamp != NULL) { |
58
|
|
|
$arguments["timestamp"] = (string)$timestamp; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->setUrl("stars.add", $arguments); |
62
|
|
|
|
63
|
|
|
// Send the request |
64
|
|
|
try { |
65
|
|
|
$client = new \GuzzleHttp\Client(); |
66
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
67
|
|
|
$response = json_decode( $json_response->getBody() ); |
68
|
|
|
} |
69
|
|
|
catch (RequestException $e) { |
70
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if($response->{'ok'} === FALSE) { |
74
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
* |
86
|
|
|
* @param int $count |
87
|
|
|
* @param int $page |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function list_stars($count = 100, $page = 1) { |
91
|
|
|
|
92
|
|
|
// Check if the type of the variables is valid. |
93
|
|
|
if (!is_integer($count)) { |
94
|
|
|
throw new InvalidArgumentException("The type of the count variable is not valid."); |
95
|
|
|
} |
96
|
|
|
if (!is_integer($page)) { |
97
|
|
|
throw new InvalidArgumentException("The type of the page variable is not valid."); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Set the arguments of the request |
101
|
|
|
$arguments = array( |
102
|
|
|
"count" => $count, |
103
|
|
|
"page" => $page |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$this->setUrl("stars.list", $arguments); |
107
|
|
|
|
108
|
|
|
// Send the request |
109
|
|
|
try { |
110
|
|
|
$client = new \GuzzleHttp\Client(); |
111
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
112
|
|
|
$response = json_decode( $json_response->getBody() ); |
113
|
|
|
} |
114
|
|
|
catch (RequestException $e) { |
115
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if($response->{'ok'} === FALSE) { |
119
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $json_response->getBody(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
* |
131
|
|
|
* @param string $file |
132
|
|
|
* @param string $file_comment |
133
|
|
|
* @param string $channel |
134
|
|
|
* @param float $timestamp |
135
|
|
|
* @return Stars |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function remove($file = NULL, $file_comment = NULL, $channel = NULL, $timestamp = NULL) { |
|
|
|
|
138
|
|
|
|
139
|
|
|
// Check if the type of the variables is valid. |
140
|
|
|
if (!is_string($file) && ($file != NULL)) { |
141
|
|
|
throw new InvalidArgumentException("The type of the file variable is not valid."); |
142
|
|
|
} |
143
|
|
|
if (!is_string($file_comment) && ($file_comment != NULL)) { |
144
|
|
|
throw new InvalidArgumentException("The type of the file_comment variable is not valid."); |
145
|
|
|
} |
146
|
|
|
if (!is_string($channel) && ($channel != NULL)) { |
147
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
148
|
|
|
} |
149
|
|
|
if (!is_float($timestamp) && ($timestamp != NULL)) { |
150
|
|
|
throw new InvalidArgumentException("The type of the timestamp variable is not valid."); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Set the arguments of the request |
154
|
|
|
$arguments = array(); |
155
|
|
|
|
156
|
|
|
if ($file != NULL) { |
|
|
|
|
157
|
|
|
$arguments["file"] = $file; |
158
|
|
|
} |
159
|
|
|
if ($file_comment != NULL) { |
|
|
|
|
160
|
|
|
$arguments["file_comment"] = $file_comment; |
161
|
|
|
} |
162
|
|
|
if ($channel != NULL) { |
|
|
|
|
163
|
|
|
$arguments["channel"] = $channel; |
164
|
|
|
} |
165
|
|
|
if ($timestamp != NULL) { |
166
|
|
|
$arguments["timestamp"] = (float)$timestamp; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->setUrl("stars.remove", $arguments); |
170
|
|
|
|
171
|
|
|
// Send the request |
172
|
|
|
try { |
173
|
|
|
$client = new \GuzzleHttp\Client(); |
174
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
175
|
|
|
$response = json_decode( $json_response->getBody() ); |
176
|
|
|
} |
177
|
|
|
catch (RequestException $e) { |
178
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if($response->{'ok'} === FALSE) { |
182
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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.