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
|
|
View Code Duplication |
class Mpim extends AbstractApi |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @param string $channel |
24
|
|
|
* @return Mpim |
25
|
|
|
*/ |
26
|
|
|
public function close($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("mpim.close", $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
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
* |
63
|
|
|
* @param string $channel |
64
|
|
|
* @param string $latest |
65
|
|
|
* @param string $oldest |
66
|
|
|
* @param integer $inclusive |
67
|
|
|
* @param integer $count |
68
|
|
|
* @param integer $unreads |
69
|
|
|
* @return Mpim |
70
|
|
|
*/ |
71
|
|
|
public function history($channel, $latest = "now", $oldest = "0", $inclusive = 0, $count = 100, $unreads = 0) { |
72
|
|
|
|
73
|
|
|
// Check if the type of the variables is valid. |
74
|
|
|
if (!is_string($channel)) { |
75
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
76
|
|
|
} |
77
|
|
|
if (!is_string($latest)) { |
78
|
|
|
throw new InvalidArgumentException("The type of the latest variable is not valid."); |
79
|
|
|
} |
80
|
|
|
if (!is_string($oldest)) { |
81
|
|
|
throw new InvalidArgumentException("The type of the oldest variable is not valid."); |
82
|
|
|
} |
83
|
|
|
if (!is_integer($inclusive)) { |
84
|
|
|
throw new InvalidArgumentException("The type of the inclusive variable is not valid."); |
85
|
|
|
} |
86
|
|
|
if (!is_integer($count)) { |
87
|
|
|
throw new InvalidArgumentException("The type of the count variable is not valid."); |
88
|
|
|
} |
89
|
|
|
if (!is_integer($unreads)) { |
90
|
|
|
throw new InvalidArgumentException("The type of the unreads variable is not valid."); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Set the arguments of the request |
94
|
|
|
$arguments = array( |
95
|
|
|
"channel" => $channel, |
96
|
|
|
"latest" => $latest, |
97
|
|
|
"oldest" => $oldest, |
98
|
|
|
"inclusive" => (string)$inclusive, |
99
|
|
|
"count" => (string)$count, |
100
|
|
|
"unreads" => (string)$unreads |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$this->setUrl("mpim.history", $arguments); |
104
|
|
|
|
105
|
|
|
// Send the request |
106
|
|
|
try { |
107
|
|
|
$client = new \GuzzleHttp\Client(); |
108
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
109
|
|
|
$response = json_decode( $json_response->getBody() ); |
110
|
|
|
} |
111
|
|
|
catch (RequestException $e) { |
112
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if($response->{'ok'} === FALSE) { |
116
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $json_response->getBody(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
* |
128
|
|
|
* @return Mpim |
129
|
|
|
*/ |
130
|
|
|
public function list_mpim() { |
131
|
|
|
|
132
|
|
|
$this->setUrl("mpim.list"); |
133
|
|
|
|
134
|
|
|
// Send the request |
135
|
|
|
try { |
136
|
|
|
$client = new \GuzzleHttp\Client(); |
137
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
138
|
|
|
$response = json_decode( $json_response->getBody() ); |
139
|
|
|
} |
140
|
|
|
catch (RequestException $e) { |
141
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if($response->{'ok'} === FALSE) { |
145
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $json_response->getBody(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
* |
157
|
|
|
* @param string $channel |
158
|
|
|
* @param float $ts |
159
|
|
|
* @return Mpim |
160
|
|
|
*/ |
161
|
|
|
public function mark($channel, $ts) { |
162
|
|
|
|
163
|
|
|
// Check if the type of the variables is valid. |
164
|
|
|
if (!is_string($channel)) { |
165
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
166
|
|
|
} |
167
|
|
|
if (!is_float($ts)) { |
168
|
|
|
throw new InvalidArgumentException("The type of the ts variable is not valid."); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// Set the arguments of the request |
172
|
|
|
$arguments = array( |
173
|
|
|
"channel" => $channel, |
174
|
|
|
"ts" => (string)$ts |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$this->setUrl("mpim.mark", $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 $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
* |
202
|
|
|
* @param string $users |
203
|
|
|
* @param boolean $return_im |
204
|
|
|
* @return Mpim |
205
|
|
|
*/ |
206
|
|
|
public function open($users, $return_im = NULL) { |
207
|
|
|
|
208
|
|
|
// Check if the type of the variables is valid. |
209
|
|
|
if (!is_string($users)) { |
210
|
|
|
throw new InvalidArgumentException("The type of the users variable is not valid."); |
211
|
|
|
} |
212
|
|
|
if (!is_bool($return_im)) { |
213
|
|
|
throw new InvalidArgumentException("The type of the return_im variable is not valid."); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// Set the arguments of the request |
217
|
|
|
$arguments = array( |
218
|
|
|
"users" => $users |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
if(($return_im != NULL) && ($return_im === TRUE)) { |
222
|
|
|
$arguments["return_im"] = 1; |
223
|
|
|
} |
224
|
|
|
elseif(($return_im != NULL) && ($return_im === FALSE)) { |
225
|
|
|
$arguments["return_im"] = 0; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$this->setUrl("mpim.open", $arguments); |
229
|
|
|
|
230
|
|
|
// Send the request |
231
|
|
|
try { |
232
|
|
|
$client = new \GuzzleHttp\Client(); |
233
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
234
|
|
|
$response = json_decode( $json_response->getBody() ); |
235
|
|
|
} |
236
|
|
|
catch (RequestException $e) { |
237
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if($response->{'ok'} === FALSE) { |
241
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $json_response->getBody(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
* |
253
|
|
|
* @param string $channel |
254
|
|
|
* @param float $thread_ts |
255
|
|
|
* @return Mpim |
256
|
|
|
*/ |
257
|
|
|
public function replies($channel, $thread_ts) { |
258
|
|
|
|
259
|
|
|
// Check if the type of the variables is valid. |
260
|
|
|
if (!is_string($channel)) { |
261
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
262
|
|
|
} |
263
|
|
|
if (!is_float($thread_ts)) { |
264
|
|
|
throw new InvalidArgumentException("The type of the thread_ts variable is not valid."); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// Set the arguments of the request |
268
|
|
|
$arguments = array( |
269
|
|
|
"channel" => $channel, |
270
|
|
|
"thread_ts" => (string)$thread_ts |
271
|
|
|
); |
272
|
|
|
|
273
|
|
|
$this->setUrl("mpim.replies", $arguments); |
274
|
|
|
|
275
|
|
|
// Send the request |
276
|
|
|
try { |
277
|
|
|
$client = new \GuzzleHttp\Client(); |
278
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
279
|
|
|
$response = json_decode( $json_response->getBody() ); |
280
|
|
|
} |
281
|
|
|
catch (RequestException $e) { |
282
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if($response->{'ok'} === FALSE) { |
286
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $json_response->getBody(); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
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.