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 Users extends AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @return Users |
24
|
|
|
*/ |
25
|
|
|
public function deletePhoto() { |
26
|
|
|
|
27
|
|
|
$this->setUrl("users.deletePhoto"); |
28
|
|
|
|
29
|
|
|
// Send the request |
30
|
|
|
try { |
31
|
|
|
$client = new \GuzzleHttp\Client(); |
32
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
33
|
|
|
$response = json_decode( $json_response->getBody() ); |
34
|
|
|
} |
35
|
|
|
catch (RequestException $e) { |
36
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if($response->{'ok'} === FALSE) { |
40
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
* |
52
|
|
|
* @param string $user |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getPresence($user) { |
56
|
|
|
|
57
|
|
|
// Check if the type of the variables is valid. |
58
|
|
|
if (!is_string($user) || ($user != NULL)) { |
59
|
|
|
throw new InvalidArgumentException("The type of the user variable is not valid."); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Set the arguments of the request |
63
|
|
|
$arguments = array( |
64
|
|
|
"user" => $user |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$this->setUrl("users.getPresence", $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
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function identity() { |
95
|
|
|
|
96
|
|
|
$this->setUrl("users.identity", $arguments); |
|
|
|
|
97
|
|
|
|
98
|
|
|
// Send the request |
99
|
|
|
try { |
100
|
|
|
$client = new \GuzzleHttp\Client(); |
101
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
102
|
|
|
$response = json_decode( $json_response->getBody() ); |
103
|
|
|
} |
104
|
|
|
catch (RequestException $e) { |
105
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if($response->{'ok'} === FALSE) { |
109
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $json_response->getBody(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
* |
122
|
|
|
* @param string $user |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function info($user) { |
126
|
|
|
|
127
|
|
|
// Check if the type of the variables is valid. |
128
|
|
|
if (!is_bool($user) || ($user == NULL)) { |
129
|
|
|
throw new InvalidArgumentException("The type of the user variable is not valid."); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Set the arguments of the request |
133
|
|
|
$arguments = array( |
134
|
|
|
"user" => $users |
|
|
|
|
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->setUrl("users.info", $arguments); |
138
|
|
|
|
139
|
|
|
// Send the request |
140
|
|
|
try { |
141
|
|
|
$client = new \GuzzleHttp\Client(); |
142
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
143
|
|
|
$response = json_decode( $json_response->getBody() ); |
144
|
|
|
} |
145
|
|
|
catch (RequestException $e) { |
146
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if($response->{'ok'} === FALSE) { |
150
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $json_response->getBody(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
* |
162
|
|
|
* @param bool $presence |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function list_users($presence = NULL) { |
|
|
|
|
166
|
|
|
|
167
|
|
|
// Check if the type of the variables is valid. |
168
|
|
|
if (!is_string($presence) && ($presence != NULL)) { |
169
|
|
|
throw new InvalidArgumentException("The type of the presence variable is not valid."); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Set the arguments of the request |
173
|
|
|
$arguments = array(); |
174
|
|
|
|
175
|
|
|
if ($presence != NULL) { |
176
|
|
|
$arguments["presence"] = $presence; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->setUrl("users.list", $arguments); |
180
|
|
|
|
181
|
|
|
// Send the request |
182
|
|
|
try { |
183
|
|
|
$client = new \GuzzleHttp\Client(); |
184
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
185
|
|
|
$response = json_decode( $json_response->getBody() ); |
186
|
|
|
} |
187
|
|
|
catch (RequestException $e) { |
188
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if($response->{'ok'} === FALSE) { |
192
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $json_response->getBody(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
* |
204
|
|
|
* @return Users |
205
|
|
|
*/ |
206
|
|
|
public function setActive() { |
207
|
|
|
|
208
|
|
|
$this->setUrl("users.setActive", $arguments); |
|
|
|
|
209
|
|
|
|
210
|
|
|
// Send the request |
211
|
|
|
try { |
212
|
|
|
$client = new \GuzzleHttp\Client(); |
213
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
214
|
|
|
$response = json_decode( $json_response->getBody() ); |
215
|
|
|
} |
216
|
|
|
catch (RequestException $e) { |
217
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if($response->{'ok'} === FALSE) { |
221
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
* |
233
|
|
|
* @param string $image |
234
|
|
|
* @param integer $crop_x |
235
|
|
|
* @param integer $crop_y |
236
|
|
|
* @param integer $crop_w |
237
|
|
|
* @return Users |
238
|
|
|
*/ |
239
|
|
|
public function setPhoto($image, $crop_x = NULL, $crop_y = NULL, $crop_w = NULL) { |
240
|
|
|
|
241
|
|
|
// Check if the type of the variables is valid. |
242
|
|
|
if (!is_string($image) || ($image == NULL)) { |
243
|
|
|
throw new InvalidArgumentException("The type of the image variable is not valid."); |
244
|
|
|
} |
245
|
|
|
if (!is_string($crop_x) && ($crop_x != NULL)) { |
|
|
|
|
246
|
|
|
throw new InvalidArgumentException("The type of the crop_x variable is not valid."); |
247
|
|
|
} |
248
|
|
|
if (!is_string($crop_y) && ($crop_y != NULL)) { |
|
|
|
|
249
|
|
|
throw new InvalidArgumentException("The type of the crop_y variable is not valid."); |
250
|
|
|
} |
251
|
|
|
if (!is_string($crop_w) && ($crop_w != NULL)) { |
|
|
|
|
252
|
|
|
throw new InvalidArgumentException("The type of the crop_w variable is not valid."); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// Set the arguments of the request |
256
|
|
|
$arguments = array( |
257
|
|
|
array( |
258
|
|
|
"name" => "file", |
259
|
|
|
"contents" => fopen($video, "r"), |
|
|
|
|
260
|
|
|
"filename" => $image |
261
|
|
|
) |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
if ($crop_x != NULL) { |
265
|
|
|
$arguments[] = array("name" => "crop_x", "content" => $crop_x); |
266
|
|
|
} |
267
|
|
|
if ($crop_y != NULL) { |
268
|
|
|
$arguments[] = array("name" => "crop_y", "content" => $crop_y); |
269
|
|
|
} |
270
|
|
|
if ($crop_w != NULL) { |
271
|
|
|
$arguments[] = array("name" => "crop_w", "content" => $crop_w); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$this->setUrl("users.setPhoto"); |
275
|
|
|
|
276
|
|
|
// Send the request |
277
|
|
|
try { |
278
|
|
|
$client = new \GuzzleHttp\Client(); |
279
|
|
|
$json_response = $client->request('POST', $this->getUrl(), [ |
280
|
|
|
'http_errors' => false, |
281
|
|
|
'multipart' => $arguments |
282
|
|
|
]); |
283
|
|
|
$response = json_decode( $json_response->getBody() ); |
284
|
|
|
} |
285
|
|
|
catch (RequestException $e) { |
286
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
if($response->{'ok'} === FALSE) { |
290
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
|
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* {@inheritdoc} |
301
|
|
|
* |
302
|
|
|
* @param string $presence |
303
|
|
|
* @return Users |
304
|
|
|
*/ |
305
|
|
|
public function setPresence($presence) { |
306
|
|
|
|
307
|
|
|
// Check if the type of the variables is valid. |
308
|
|
|
if (!is_string($presence) || ($presence == NULL)) { |
309
|
|
|
throw new InvalidArgumentException("The type of the presence variable is not valid."); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// Set the arguments of the request |
313
|
|
|
$arguments = array( |
314
|
|
|
"presence" => $presence |
315
|
|
|
); |
316
|
|
|
|
317
|
|
|
$this->setUrl("users.setPresence", $arguments); |
318
|
|
|
|
319
|
|
|
// Send the request |
320
|
|
|
try { |
321
|
|
|
$client = new \GuzzleHttp\Client(); |
322
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
323
|
|
|
$response = json_decode( $json_response->getBody() ); |
324
|
|
|
} |
325
|
|
|
catch (RequestException $e) { |
326
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
if($response->{'ok'} === FALSE) { |
330
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $this; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.