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 Files extends AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @param string $file |
24
|
|
|
* @return Files |
25
|
|
|
*/ |
26
|
|
|
public function delete($file) { |
27
|
|
|
|
28
|
|
|
// Check if the type of the variables is valid. |
29
|
|
|
if (!is_string($file)) { |
30
|
|
|
throw new InvalidArgumentException("The type of the file variable is not valid."); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Set the arguments of the request |
34
|
|
|
$arguments = array( |
35
|
|
|
"file" => $file |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$this->setUrl("files.delete", $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 $file |
63
|
|
|
* @param integer $count |
64
|
|
|
* @param integer $page |
65
|
|
|
* @return Files |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function info($file, $count = 100, $page = 1) { |
|
|
|
|
68
|
|
|
|
69
|
|
|
// Check if the type of the variables is valid. |
70
|
|
|
if (!is_string($file)) { |
71
|
|
|
throw new InvalidArgumentException("The type of the file variable is not valid."); |
72
|
|
|
} |
73
|
|
|
if (!is_integer($count)) { |
74
|
|
|
throw new InvalidArgumentException("The type of the count variable is not valid."); |
75
|
|
|
} |
76
|
|
|
if (!is_integer($page)) { |
77
|
|
|
throw new InvalidArgumentException("The type of the page variable is not valid."); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Set the arguments of the request |
81
|
|
|
$arguments = array( |
82
|
|
|
"file" => $file, |
83
|
|
|
"count" => $count, |
84
|
|
|
"page" => $page |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$this->setUrl("files.info", $arguments); |
88
|
|
|
|
89
|
|
|
// Send the request |
90
|
|
|
try { |
91
|
|
|
$client = new \GuzzleHttp\Client(); |
92
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
93
|
|
|
$response = json_decode( $json_response->getBody() ); |
94
|
|
|
} |
95
|
|
|
catch (RequestException $e) { |
96
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if($response->{'ok'} === FALSE) { |
100
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $json_response->getBody(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
* |
111
|
|
|
* @param string $user |
112
|
|
|
* @param string $channel |
113
|
|
|
* @param integer $ts_from |
114
|
|
|
* @param integer $ts_to |
115
|
|
|
* @param string $types |
116
|
|
|
* @param integer $count |
117
|
|
|
* @param integer $page |
118
|
|
|
* @return Files |
119
|
|
|
*/ |
120
|
|
|
public function list_files($user = NULL, $channel = NULL, $ts_from = "now", $ts_to = "all", $types = "all", $count = 100, $page = 1) { |
121
|
|
|
|
122
|
|
|
// Check if the type of the variables is valid. |
123
|
|
|
if (($user != NULL) && !is_string($user)) { |
|
|
|
|
124
|
|
|
throw new InvalidArgumentException("The type of the user variable is not valid."); |
125
|
|
|
} |
126
|
|
|
if (($channel != NULL) && !is_string($channel)) { |
|
|
|
|
127
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
128
|
|
|
} |
129
|
|
|
if (!is_int($ts_from)) { |
130
|
|
|
throw new InvalidArgumentException("The type of the ts_from variable is not valid."); |
131
|
|
|
} |
132
|
|
|
if (!is_int($ts_to)) { |
133
|
|
|
throw new InvalidArgumentException("The type of the ts_to variable is not valid."); |
134
|
|
|
} |
135
|
|
|
if (!is_string($types)) { |
136
|
|
|
throw new InvalidArgumentException("The type of the types variable is not valid."); |
137
|
|
|
} |
138
|
|
|
if (!is_integer($count)) { |
139
|
|
|
throw new InvalidArgumentException("The type of the count variable is not valid."); |
140
|
|
|
} |
141
|
|
|
if (!is_integer($page)) { |
142
|
|
|
throw new InvalidArgumentException("The type of the page variable is not valid."); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Set the arguments of the request |
146
|
|
|
$arguments = array( |
147
|
|
|
"ts_from" => (string)$ts_from, |
148
|
|
|
"ts_to" => (string)$ts_to, |
149
|
|
|
"types" => $types, |
150
|
|
|
"count" => $count, |
151
|
|
|
"page" => $page |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
if ($user != NULL) { |
|
|
|
|
155
|
|
|
$arguments["user"] = $user; |
156
|
|
|
} |
157
|
|
|
if ($channel != NULL) { |
|
|
|
|
158
|
|
|
$arguments["channel"] = $channel; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$this->setUrl("files.list", $arguments); |
162
|
|
|
|
163
|
|
|
// Send the request |
164
|
|
|
try { |
165
|
|
|
$client = new \GuzzleHttp\Client(); |
166
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
167
|
|
|
$response = json_decode( $json_response->getBody() ); |
168
|
|
|
} |
169
|
|
|
catch (RequestException $e) { |
170
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if($response->{'ok'} === FALSE) { |
174
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $json_response->getBody(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
* |
185
|
|
|
* @param string $file |
186
|
|
|
* @return Files |
187
|
|
|
*/ |
188
|
|
|
public function revokePublicURL($file) { |
189
|
|
|
|
190
|
|
|
// Check if the type of the variables is valid. |
191
|
|
|
if (!is_string($file)) { |
192
|
|
|
throw new InvalidArgumentException("The type of the file variable is not valid."); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Set the arguments of the request |
196
|
|
|
$arguments = array( |
197
|
|
|
"file" => $file |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$this->setUrl("files.revokePublicURL", $arguments); |
201
|
|
|
|
202
|
|
|
// Send the request |
203
|
|
|
try { |
204
|
|
|
$client = new \GuzzleHttp\Client(); |
205
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
206
|
|
|
$response = json_decode( $json_response->getBody() ); |
207
|
|
|
} |
208
|
|
|
catch (RequestException $e) { |
209
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if($response->{'ok'} === FALSE) { |
213
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritdoc} |
223
|
|
|
* |
224
|
|
|
* @param string $file |
225
|
|
|
* @return Files |
226
|
|
|
*/ |
227
|
|
|
public function sharedPublicURL($file) { |
228
|
|
|
|
229
|
|
|
// Check if the type of the variables is valid. |
230
|
|
|
if (!is_string($file)) { |
231
|
|
|
throw new InvalidArgumentException("The type of the file variable is not valid."); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// Set the arguments of the request |
235
|
|
|
$arguments = array( |
236
|
|
|
"file" => $file |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
$this->setUrl("files.sharedPublicURL", $arguments); |
240
|
|
|
|
241
|
|
|
// Send the request |
242
|
|
|
try { |
243
|
|
|
$client = new \GuzzleHttp\Client(); |
244
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
245
|
|
|
$response = json_decode( $json_response->getBody() ); |
246
|
|
|
} |
247
|
|
|
catch (RequestException $e) { |
248
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if($response->{'ok'} === FALSE) { |
252
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $json_response->getBody(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* {@inheritdoc} |
262
|
|
|
* |
263
|
|
|
* @param string $filename |
264
|
|
|
* @param string $path_to_file |
265
|
|
|
* @param string $filetype |
266
|
|
|
* @param string $title |
267
|
|
|
* @param string $initial_comment |
268
|
|
|
* @param string $channels |
269
|
|
|
* @return Files |
270
|
|
|
*/ |
271
|
|
|
public function upload($filename, $path_to_file = NULL, $filetype = NULL, $title = NULL, |
272
|
|
|
$initial_comment = NULL, $channels = NULL) { |
273
|
|
|
|
274
|
|
|
// Check if the type of the variables is valid. |
275
|
|
|
if (!is_string($filename)) { |
276
|
|
|
throw new InvalidArgumentException("The type of the filename variable is not valid."); |
277
|
|
|
} |
278
|
|
|
if (($path_to_file != NULL) && !is_string($path_to_file)) { |
|
|
|
|
279
|
|
|
throw new InvalidArgumentException("The type of the path_to_file variable is not valid."); |
280
|
|
|
} |
281
|
|
|
if (($filetype != NULL) && !is_string($filetype)) { |
|
|
|
|
282
|
|
|
throw new InvalidArgumentException("The type of the filetype variable is not valid."); |
283
|
|
|
} |
284
|
|
|
if (($title != NULL) && !is_string($title)) { |
|
|
|
|
285
|
|
|
throw new InvalidArgumentException("The type of the title variable is not valid."); |
286
|
|
|
} |
287
|
|
|
if (($initial_comment != NULL) && !is_string($initial_comment)) { |
|
|
|
|
288
|
|
|
throw new InvalidArgumentException("The type of the initial_comment variable is not valid."); |
289
|
|
|
} |
290
|
|
|
if (($channels != NULL) && !is_string($channels)) { |
|
|
|
|
291
|
|
|
throw new InvalidArgumentException("The type of the channels variable is not valid."); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// Get the content of the file |
295
|
|
|
if(file_exists( $path_to_file )) { |
296
|
|
|
$content = file_get_contents( $path_to_file ); |
297
|
|
|
} |
298
|
|
|
else { |
299
|
|
|
throw new RuntimeException("The path to the file is not valid."); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
// Set the arguments of the request |
303
|
|
|
$arguments = array( |
304
|
|
|
"filename" => $filename, |
305
|
|
|
"filetype" => $filetype, |
306
|
|
|
"title" => $title, |
307
|
|
|
"initial_comment" => $initial_comment, |
308
|
|
|
"channels" => $channels |
309
|
|
|
); |
310
|
|
|
|
311
|
|
|
$post_content = array( |
312
|
|
|
"content" => $content |
313
|
|
|
); |
314
|
|
|
|
315
|
|
|
$this->setUrl("files.upload", $arguments); |
316
|
|
|
|
317
|
|
|
// Send the request |
318
|
|
|
try { |
319
|
|
|
$client = new \GuzzleHttp\Client(); |
320
|
|
|
$json_response = $client->request('POST', $this->getUrl(), [ |
321
|
|
|
'headers' => $headers, |
|
|
|
|
322
|
|
|
'http_errors' => false, |
323
|
|
|
'json' => $post_content |
324
|
|
|
]); |
325
|
|
|
$response = json_decode( $json_response->getBody() ); |
326
|
|
|
} |
327
|
|
|
catch (RequestException $e) { |
328
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
if($response->{'ok'} === FALSE) { |
332
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
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.