|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright © 2017 Toan Nguyen. All rights reserved. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Gojira\Command\Jira; |
|
10
|
|
|
|
|
11
|
|
|
use Gojira\Api\Client\GuzzleClient; |
|
12
|
|
|
use Gojira\Framework\App\Configuration\AuthInterface; |
|
13
|
|
|
use Gojira\Framework\App\Configuration\ConfigurationInterface; |
|
14
|
|
|
use Gojira\Framework\App\Configuration\OptionsInterface; |
|
15
|
|
|
use Gojira\Framework\App\Console\TableInterface; |
|
16
|
|
|
use Gojira\Api\Exception\ApiException; |
|
17
|
|
|
use Gojira\Api\Exception\HttpNotFoundException; |
|
18
|
|
|
use Gojira\Api\Exception\UnauthorizedException; |
|
19
|
|
|
use Gojira\Api\Request\StatusCodes; |
|
20
|
|
|
use Gojira\Provider\Console\Command; |
|
21
|
|
|
use Gojira\Provider\Console\Table; |
|
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Base class for all JIRA commands. |
|
26
|
|
|
* |
|
27
|
|
|
* @package Gojira\Command\Jira |
|
28
|
|
|
* @author Toan Nguyen <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class AbstractCommand extends Command |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var \Gojira\Api\Client\GuzzleClient |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $apiClient = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var \Gojira\Provider\Console\Table |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $table = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $optionItems = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get response data w/ specified endpoint instance |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $filters Filters passing to endpoint |
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
abstract protected function getResponse($filters = []); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Render response data for outputting |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $response REST API response after deserialized |
|
60
|
|
|
* @param string|null $type Result type to render |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
abstract protected function renderResult($response = [], $type = null); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Abstract method for all execute() commands |
|
68
|
|
|
* |
|
69
|
|
|
* @param OutputInterface $output Symfony console output |
|
70
|
|
|
* @param int $acceptedStatusCode HTTP status code to continue |
|
71
|
|
|
* @param array $filters Payload/Params passing to endpoint |
|
72
|
|
|
* @param array $tableHeaders Console table header |
|
73
|
|
|
* @param string $infoMessage Info message after table rendered successfully |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function doExecute( |
|
78
|
|
|
OutputInterface $output, |
|
79
|
|
|
$acceptedStatusCode = StatusCodes::HTTP_OK, |
|
80
|
|
|
array $filters = [], |
|
81
|
|
|
array $tableHeaders = [], |
|
82
|
|
|
$infoMessage = '' |
|
83
|
|
|
) { |
|
84
|
|
|
try { |
|
85
|
|
|
$response = $this->getResponse($filters); |
|
86
|
|
|
$rows = $this->renderResult($response, $this->getName()); |
|
87
|
|
|
|
|
88
|
|
|
if ($this->getApiClient()->getResultHttpCode() === $acceptedStatusCode) { |
|
89
|
|
|
if (!empty($tableHeaders)) { |
|
90
|
|
|
$this->renderTable($output, [ |
|
91
|
|
|
TableInterface::HEADERS => $tableHeaders, |
|
92
|
|
|
TableInterface::ROWS => Table::buildRows($rows) |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (!empty($infoMessage)) { |
|
97
|
|
|
$output->writeln($infoMessage); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} catch (ApiException $e) { |
|
101
|
|
|
$message = 'Something went wrong.'; |
|
102
|
|
|
if ($e instanceof HttpNotFoundException || $e instanceof UnauthorizedException) { |
|
103
|
|
|
$message = StatusCodes::getMessageForCode($this->getApiClient()->getResultHttpCode()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$output->writeln(__('<error>%1</error>', $message)); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Render table faster |
|
112
|
|
|
* |
|
113
|
|
|
* @param OutputInterface $output |
|
114
|
|
|
* @param array $data |
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function renderTable(OutputInterface $output, array $data) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->table = new Table($output, $data); |
|
121
|
|
|
$this->table->render(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get Base URI |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getBaseUri() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->configuration->getData(ConfigurationInterface::AUTH . '/' . AuthInterface::BASE_URI); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns ApiClient object instance |
|
136
|
|
|
* |
|
137
|
|
|
* @param bool $debug Debug current request |
|
138
|
|
|
* |
|
139
|
|
|
* @return GuzzleClient |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function getApiClient($debug = false) |
|
142
|
|
|
{ |
|
143
|
|
|
if ($this->apiClient === null) { |
|
144
|
|
|
$this->apiClient = new GuzzleClient( |
|
145
|
|
|
$this->getBaseUri(), |
|
146
|
|
|
$this->authentication, |
|
147
|
|
|
(bool)$debug, |
|
148
|
|
|
(bool)$this->optionItems[OptionsInterface::IS_USE_CACHE] |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $this->apiClient; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get available issue statues |
|
157
|
|
|
* |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getAvailableStatuses() |
|
161
|
|
|
{ |
|
162
|
|
|
$configItems = $this->configuration->getData(ConfigurationInterface::OPTIONS); |
|
163
|
|
|
|
|
164
|
|
|
return implode('", "', $configItems[OptionsInterface::AVAILABLE_ISSUES]); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get option item |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $key |
|
171
|
|
|
* |
|
172
|
|
|
* @return array|mixed|null |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getOptionItem($key = null) |
|
175
|
|
|
{ |
|
176
|
|
|
$optionItems = $this->configuration->getData(ConfigurationInterface::OPTIONS); |
|
177
|
|
|
if ($key === null) { |
|
178
|
|
|
return $optionItems; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $optionItems[$key]; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get application container throught Pimple |
|
186
|
|
|
* |
|
187
|
|
|
* @return mixed|null |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function getApp() |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->getApplication()->getService('console'); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|