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 Chat extends AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @param float $ts |
24
|
|
|
* @param string $channel |
25
|
|
|
* @param string $as_user |
26
|
|
|
* @return Chat |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function delete($ts, $channel, $as_user = NULL) { |
|
|
|
|
29
|
|
|
|
30
|
|
|
// Check if the type of the variables is valid. |
31
|
|
|
if (!is_float($ts)) { |
32
|
|
|
throw new InvalidArgumentException("The type of the ts variable is not valid."); |
33
|
|
|
} |
34
|
|
|
if (!is_string($channel)) { |
35
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
36
|
|
|
} |
37
|
|
|
if (($as_user != NULL) && !is_bool($as_user)) { |
|
|
|
|
38
|
|
|
throw new InvalidArgumentException("The type of the as_user variable is not valid."); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Set the arguments of the request |
42
|
|
|
$arguments = array( |
43
|
|
|
"ts" => (string)$ts, |
44
|
|
|
"channel" => $channel |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
if($as_user != NULL) { |
48
|
|
|
$arguments["as_user"] = $as_user; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->setUrl("chat.delete", $arguments); |
52
|
|
|
|
53
|
|
|
// Send the request |
54
|
|
|
try { |
55
|
|
|
$client = new \GuzzleHttp\Client(); |
56
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
57
|
|
|
$response = json_decode( $json_response->getBody() ); |
58
|
|
|
} |
59
|
|
|
catch (RequestException $e) { |
60
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if($response->{'ok'} === FALSE) { |
64
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $json_response->getBody(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
* |
75
|
|
|
* @param string $channel |
76
|
|
|
* @param string $text |
77
|
|
|
* @return Chat |
78
|
|
|
*/ |
79
|
|
|
public function meMessage($channel, $text) { |
80
|
|
|
|
81
|
|
|
// Check if the type of the variables is valid. |
82
|
|
|
if (!is_string($channel)) { |
83
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
84
|
|
|
} |
85
|
|
|
if (!is_string($text)) { |
86
|
|
|
throw new InvalidArgumentException("The type of the text variable is not valid."); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Set the arguments of the request |
90
|
|
|
$arguments = array( |
91
|
|
|
"channel" => $channel, |
92
|
|
|
"text" => $text |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->setUrl("chat.meMessage", $arguments); |
96
|
|
|
|
97
|
|
|
// Send the request |
98
|
|
|
try { |
99
|
|
|
$client = new \GuzzleHttp\Client(); |
100
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
101
|
|
|
$response = json_decode( $json_response->getBody() ); |
102
|
|
|
} |
103
|
|
|
catch (RequestException $e) { |
104
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if($response->{'ok'} === FALSE) { |
108
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $json_response->getBody(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
* |
119
|
|
|
* @param string $channel |
120
|
|
|
* @param string $text |
121
|
|
|
* @param string $parse |
122
|
|
|
* @param integer $link_names |
123
|
|
|
* @param string $attachments |
124
|
|
|
* @param bool $unfurl_links |
125
|
|
|
* @param bool $unfurl_media |
126
|
|
|
* @param string $username |
127
|
|
|
* @param bool $as_user |
128
|
|
|
* @param string $icon_url |
129
|
|
|
* @param string $icon_emoji |
130
|
|
|
* @param float $thread_ts |
131
|
|
|
* @param string $reply_broadcast |
132
|
|
|
* @return Chat |
133
|
|
|
*/ |
134
|
|
|
public function postMessage($channel, $text, $parse = "none", $link_names = 1, $attachments = NULL, |
135
|
|
|
$unfurl_links = TRUE, $unfurl_media = FALSE, $username = NULL, $as_user = TRUE, $icon_url = NULL, |
136
|
|
|
$icon_emoji = NULL, $thread_ts = NULL, $reply_broadcast = TRUE) { |
137
|
|
|
|
138
|
|
|
// Check if the type of the variables is valid. |
139
|
|
|
if (!is_string($channel)) { |
140
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
141
|
|
|
} |
142
|
|
|
if (!is_string($text)) { |
143
|
|
|
throw new InvalidArgumentException("The type of the text variable is not valid."); |
144
|
|
|
} |
145
|
|
|
if (!is_string($parse)) { |
146
|
|
|
throw new InvalidArgumentException("The type of the parse variable is not valid."); |
147
|
|
|
} |
148
|
|
|
if (!is_integer($link_names)) { |
149
|
|
|
throw new InvalidArgumentException("The type of the link_names variable is not valid."); |
150
|
|
|
} |
151
|
|
|
if (($attachments != NULL) && !is_string($attachments)) { |
|
|
|
|
152
|
|
|
throw new InvalidArgumentException("The type of the attachments variable is not valid."); |
153
|
|
|
} |
154
|
|
|
if (!is_bool($unfurl_links)) { |
155
|
|
|
throw new InvalidArgumentException("The type of the unfurl_links variable is not valid."); |
156
|
|
|
} |
157
|
|
|
if (!is_bool($unfurl_media)) { |
158
|
|
|
throw new InvalidArgumentException("The type of the unfurl_media variable is not valid."); |
159
|
|
|
} |
160
|
|
|
if (($username != NULL) && !is_string($username)) { |
|
|
|
|
161
|
|
|
throw new InvalidArgumentException("The type of the username variable is not valid."); |
162
|
|
|
} |
163
|
|
|
if (!is_bool($as_user)) { |
164
|
|
|
throw new InvalidArgumentException("The type of the as_user variable is not valid."); |
165
|
|
|
} |
166
|
|
|
if (($icon_url != NULL) && !is_string($icon_url)) { |
|
|
|
|
167
|
|
|
throw new InvalidArgumentException("The type of the icon_url variable is not valid."); |
168
|
|
|
} |
169
|
|
|
if (($icon_emoji != NULL) && !is_string($icon_emoji)) { |
|
|
|
|
170
|
|
|
throw new InvalidArgumentException("The type of the icon_emoji variable is not valid."); |
171
|
|
|
} |
172
|
|
|
if (($thread_ts != NULL) && !is_float($thread_ts)) { |
173
|
|
|
throw new InvalidArgumentException("The type of the thread_ts variable is not valid."); |
174
|
|
|
} |
175
|
|
|
if (!is_bool($reply_broadcast)) { |
176
|
|
|
throw new InvalidArgumentException("The type of the reply_broadcast variable is not valid."); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Set the arguments of the request |
180
|
|
|
$arguments = array( |
181
|
|
|
"channel" => $channel, |
182
|
|
|
"text" => $text, |
183
|
|
|
"parse" => $parse, |
184
|
|
|
"link_names" => $link_names, |
185
|
|
|
"unfurl_links" => $unfurl_links, |
186
|
|
|
"unfurl_media" => $unfurl_media, |
187
|
|
|
"as_user" => $as_user, |
188
|
|
|
"reply_broadcast" => $reply_broadcast |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
if($attachments != NULL) { |
|
|
|
|
192
|
|
|
$arguments["attachments"] = $attachments; |
193
|
|
|
} |
194
|
|
|
if($username != NULL) { |
|
|
|
|
195
|
|
|
$arguments["username"] = $username; |
196
|
|
|
} |
197
|
|
|
if($icon_url != NULL) { |
|
|
|
|
198
|
|
|
$arguments["icon_url"] = $icon_url; |
199
|
|
|
} |
200
|
|
|
if($icon_emoji != NULL) { |
|
|
|
|
201
|
|
|
$arguments["icon_emoji"] = $icon_emoji; |
202
|
|
|
} |
203
|
|
|
if($thread_ts != NULL) { |
204
|
|
|
$arguments["thread_ts"] = (string)$thread_ts; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->setUrl("chat.postMessage", $arguments); |
208
|
|
|
|
209
|
|
|
// Send the request |
210
|
|
|
try { |
211
|
|
|
$client = new \GuzzleHttp\Client(); |
212
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
213
|
|
|
$response = json_decode( $json_response->getBody() ); |
214
|
|
|
} |
215
|
|
|
catch (RequestException $e) { |
216
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if($response->{'ok'} === FALSE) { |
220
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $json_response->getBody(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* {@inheritdoc} |
230
|
|
|
* |
231
|
|
|
* @param string $channel |
232
|
|
|
* @param string $text |
233
|
|
|
* @param float $ts |
234
|
|
|
* @param string $attachments |
235
|
|
|
* @param string $parse |
236
|
|
|
* @param integer $link_names |
237
|
|
|
* @param bool $as_user |
238
|
|
|
* @return Chat |
239
|
|
|
*/ |
240
|
|
|
public function update($ts, $channel, $text, $attachments = NULL, $parse = "none", $link_names = 1, $as_user = TRUE) { |
241
|
|
|
|
242
|
|
|
// Check if the type of the variables is valid. |
243
|
|
|
if (!is_float($ts)) { |
244
|
|
|
throw new InvalidArgumentException("The type of the ts variable is not valid."); |
245
|
|
|
} |
246
|
|
|
if (!is_string($channel)) { |
247
|
|
|
throw new InvalidArgumentException("The type of the channel variable is not valid."); |
248
|
|
|
} |
249
|
|
|
if (!is_string($text)) { |
250
|
|
|
throw new InvalidArgumentException("The type of the text variable is not valid."); |
251
|
|
|
} |
252
|
|
|
if (($attachments != NULL) && !is_string($attachments)) { |
|
|
|
|
253
|
|
|
throw new InvalidArgumentException("The type of the attachments variable is not valid."); |
254
|
|
|
} |
255
|
|
|
if (!is_string($parse)) { |
256
|
|
|
throw new InvalidArgumentException("The type of the parse variable is not valid."); |
257
|
|
|
} |
258
|
|
|
if (!is_integer($link_names)) { |
259
|
|
|
throw new InvalidArgumentException("The type of the link_names variable is not valid."); |
260
|
|
|
} |
261
|
|
|
if (!is_bool($as_user)) { |
262
|
|
|
throw new InvalidArgumentException("The type of the as_user variable is not valid."); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
// Set the arguments of the request |
266
|
|
|
$arguments = array( |
267
|
|
|
"ts" => (string)$ts, |
268
|
|
|
"channel" => $channel, |
269
|
|
|
"text" => $text, |
270
|
|
|
"parse" => $parse, |
271
|
|
|
"link_names" => $link_names, |
272
|
|
|
"as_user" => $as_user, |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
if($attachments != NULL) { |
|
|
|
|
276
|
|
|
$arguments["attachments"] = $attachments; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$this->setUrl("chat.update", $arguments); |
280
|
|
|
|
281
|
|
|
// Send the request |
282
|
|
|
try { |
283
|
|
|
$client = new \GuzzleHttp\Client(); |
284
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
285
|
|
|
$response = json_decode( $json_response->getBody() ); |
286
|
|
|
} |
287
|
|
|
catch (RequestException $e) { |
288
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if($response->{'ok'} === FALSE) { |
292
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return $json_response->getBody(); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
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.