|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the slack-cli package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Cas Leentfaar <[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 CL\SlackCli\Command; |
|
13
|
|
|
|
|
14
|
|
|
use CL\Slack\Exception\SlackException; |
|
15
|
|
|
use CL\Slack\Model\AbstractModel; |
|
16
|
|
|
use CL\Slack\Model\Customizable; |
|
17
|
|
|
use CL\Slack\Payload\PayloadInterface; |
|
18
|
|
|
use CL\Slack\Payload\PayloadResponseInterface; |
|
19
|
|
|
use CL\Slack\Test\Transport\MockApiClient; |
|
20
|
|
|
use CL\Slack\Transport\ApiClient; |
|
21
|
|
|
use CL\Slack\Transport\Events\RequestEvent; |
|
22
|
|
|
use CL\Slack\Transport\Events\ResponseEvent; |
|
23
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
24
|
|
|
use JMS\Serializer\SerializerInterface; |
|
25
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
26
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
27
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
28
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @author Cas Leentfaar <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract class AbstractApiCommand extends AbstractCommand |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var SerializerInterface|null |
|
37
|
|
|
*/ |
|
38
|
|
|
private $serializer; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
19 |
|
protected function configure() |
|
44
|
|
|
{ |
|
45
|
19 |
|
parent::configure(); |
|
46
|
|
|
|
|
47
|
19 |
|
$this->addOption( |
|
48
|
19 |
|
'token', |
|
49
|
19 |
|
't', |
|
50
|
19 |
|
InputOption::VALUE_REQUIRED, |
|
51
|
1 |
|
'Token to use during the API-call (defaults to the one defined in the global configuration)' |
|
52
|
19 |
|
); |
|
53
|
19 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
19 |
|
public function execute(InputInterface $input, OutputInterface $output) |
|
59
|
1 |
|
{ |
|
60
|
19 |
|
$payload = $this->createPayload(); |
|
61
|
|
|
|
|
62
|
19 |
|
if (!($payload instanceof PayloadInterface)) { |
|
63
|
|
|
throw new \RuntimeException(sprintf( |
|
64
|
|
|
'%s::createPayload() should return an object implementing %s, got: %s', |
|
65
|
|
|
get_class($this), |
|
66
|
1 |
|
'CL\Slack\Payload\PayloadInterface', |
|
67
|
|
|
var_export($payload, true) |
|
68
|
1 |
|
)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
19 |
|
$response = $this->sendPayload($payload); |
|
72
|
19 |
|
$returnCode = $this->handleResponse($response); |
|
73
|
|
|
|
|
74
|
19 |
|
if (null === $returnCode) { |
|
75
|
18 |
|
return $response->isOk() ? 0 : 1; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
return (int) $returnCode; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param PayloadInterface $payload |
|
83
|
|
|
* |
|
84
|
|
|
* @return PayloadResponseInterface |
|
85
|
|
|
* |
|
86
|
|
|
* @throws SlackException |
|
87
|
|
|
*/ |
|
88
|
19 |
|
private function sendPayload(PayloadInterface $payload) |
|
89
|
|
|
{ |
|
90
|
19 |
|
if ($this->input->getOption('token')) { |
|
91
|
19 |
|
$token = $this->input->getOption('token'); |
|
92
|
19 |
|
} else { |
|
93
|
|
|
$token = $this->getConfig()->get('default_token'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
19 |
|
if (empty($token)) { |
|
97
|
|
|
throw new \RuntimeException( |
|
98
|
|
|
'No token provided by `--token` option and no value for `default_token` was found '. |
|
99
|
|
|
'in the global configuration. Use the `--token` option or set the token globally '. |
|
100
|
|
|
'by running `slack.phar config.set default_token your-token-here`' |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
19 |
|
if ($this->isTest()) { |
|
105
|
19 |
|
$apiClient = new MockApiClient(); |
|
106
|
|
|
|
|
107
|
19 |
|
return $apiClient->send($payload, $token, $this->isTestSuccess()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$apiClient = new ApiClient($token); |
|
111
|
|
|
|
|
112
|
|
|
$this->configureListeners($apiClient); |
|
113
|
|
|
|
|
114
|
|
|
return $apiClient->send($payload, $token); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param array|object|null $data |
|
119
|
|
|
* @param array $headers |
|
120
|
|
|
*/ |
|
121
|
7 |
|
protected function renderKeyValueTable($data, array $headers = []) |
|
122
|
|
|
{ |
|
123
|
7 |
|
if ($data === null) { |
|
124
|
|
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
7 |
|
if (is_object($data)) { |
|
128
|
|
|
$data = $this->serializeObjectToArray($data); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
7 |
|
$rows = []; |
|
132
|
7 |
|
foreach ($data as $key => $value) { |
|
133
|
7 |
|
$rows[] = [$key, $value]; |
|
134
|
7 |
|
} |
|
135
|
|
|
|
|
136
|
7 |
|
$this->renderTable($rows, $headers); |
|
137
|
7 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param array $rows |
|
141
|
|
|
* @param array|null $headers |
|
142
|
|
|
*/ |
|
143
|
9 |
|
protected function renderTable(array $rows, $headers = null) |
|
144
|
|
|
{ |
|
145
|
9 |
|
$table = new Table($this->output); |
|
146
|
|
|
|
|
147
|
9 |
|
if (!empty($headers)) { |
|
148
|
|
|
$table->setHeaders($headers); |
|
149
|
9 |
|
} elseif ($headers === null && !empty($rows)) { |
|
150
|
2 |
|
$firstRow = reset($rows); |
|
151
|
2 |
|
if ($firstRow instanceof AbstractModel || $firstRow instanceof PayloadResponseInterface) { |
|
152
|
1 |
|
$firstRow = $this->serializeObjectToArray($firstRow); |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
2 |
|
$table->setHeaders(array_keys($firstRow)); |
|
156
|
2 |
|
} |
|
157
|
|
|
|
|
158
|
9 |
|
$table->setRows($this->simplifyRows($rows)); |
|
159
|
|
|
|
|
160
|
9 |
|
$style = Table::getStyleDefinition('default'); |
|
161
|
9 |
|
$style->setCellRowFormat('<fg=yellow>%s</>'); |
|
162
|
9 |
|
$table->render(); |
|
163
|
9 |
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param array $rows |
|
167
|
|
|
* |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
9 |
|
protected function simplifyRows(array $rows) |
|
171
|
|
|
{ |
|
172
|
9 |
|
$simplified = []; |
|
173
|
9 |
|
foreach ($rows as $x => $row) { |
|
174
|
9 |
|
$row = $this->formatTableRow($row); |
|
175
|
9 |
|
foreach ($row as $column => $value) { |
|
176
|
9 |
|
if (!is_scalar($value)) { |
|
177
|
8 |
|
$value = $this->formatNonScalarColumnValue($value); |
|
178
|
9 |
|
} elseif (is_bool($value)) { |
|
179
|
|
|
$value = $value ? 'true' : 'false'; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
9 |
|
$simplified[$x][$column] = $value; |
|
183
|
9 |
|
} |
|
184
|
9 |
|
} |
|
185
|
|
|
|
|
186
|
9 |
|
return $simplified; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param object|array $row |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
9 |
|
protected function formatTableRow($row) |
|
195
|
|
|
{ |
|
196
|
9 |
|
if (is_object($row)) { |
|
197
|
1 |
|
$row = $this->serializeObjectToArray($row); |
|
198
|
1 |
|
} |
|
199
|
|
|
|
|
200
|
9 |
|
return $row; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param $nonScalar |
|
205
|
|
|
* |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
8 |
|
protected function formatNonScalarColumnValue($nonScalar) |
|
209
|
|
|
{ |
|
210
|
8 |
|
if ($nonScalar instanceof Customizable) { |
|
211
|
|
|
$nonScalar = $nonScalar->getValue(); |
|
212
|
|
|
} else { |
|
213
|
8 |
|
$nonScalar = json_encode($nonScalar); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
8 |
|
return $nonScalar; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param object|null $object |
|
221
|
|
|
* |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
7 |
|
protected function serializeObjectToArray($object) |
|
225
|
|
|
{ |
|
226
|
7 |
|
if ($object === null) { |
|
227
|
|
|
return []; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
7 |
|
$data = $this->getSerializer()->serialize($object, 'json'); |
|
231
|
|
|
|
|
232
|
7 |
|
if (!empty($data)) { |
|
233
|
7 |
|
return json_decode($data, true); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return []; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param ApiClient $apiClient |
|
241
|
|
|
*/ |
|
242
|
|
|
private function configureListeners(ApiClient $apiClient) |
|
243
|
|
|
{ |
|
244
|
|
|
$output = $this->output; |
|
245
|
|
|
if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) { |
|
246
|
|
|
$self = $this; |
|
247
|
|
|
|
|
248
|
|
|
$apiClient->addRequestListener(function (RequestEvent $event) use ($output, $self) { |
|
249
|
|
|
$rawRequest = $event->getRawPayload(); |
|
250
|
|
|
$output->writeln('<comment>Debug: sending payload...</comment>'); |
|
251
|
|
|
$self->renderKeyValueTable($rawRequest); |
|
252
|
|
|
}); |
|
253
|
|
|
|
|
254
|
|
|
$apiClient->addResponseListener(function (ResponseEvent $event) use ($output, $self) { |
|
255
|
|
|
$rawResponse = $event->getRawPayloadResponse(); |
|
256
|
|
|
$output->writeln('<comment>Debug: received payload response...</comment>'); |
|
257
|
|
|
$self->renderKeyValueTable($rawResponse); |
|
258
|
|
|
}); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @return SerializerInterface |
|
264
|
|
|
*/ |
|
265
|
7 |
|
private function getSerializer() |
|
266
|
|
|
{ |
|
267
|
7 |
|
if (!isset($this->payloadSerializer)) { |
|
268
|
7 |
|
$this->serializer = SerializerBuilder::create()->build(); |
|
269
|
7 |
|
} |
|
270
|
|
|
|
|
271
|
7 |
|
return $this->serializer; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @return PayloadInterface |
|
276
|
|
|
*/ |
|
277
|
|
|
abstract protected function createPayload(); |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @param PayloadResponseInterface $payloadResponse |
|
281
|
|
|
* |
|
282
|
|
|
* @return int|null |
|
283
|
|
|
*/ |
|
284
|
|
|
abstract protected function handleResponse($payloadResponse); |
|
285
|
|
|
} |
|
286
|
|
|
|