1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PCextreme\Cloudstack\Console; |
6
|
|
|
|
7
|
|
|
use PCextreme\Cloudstack\Client; |
8
|
|
|
use Symfony\Component\Cache\Simple\FilesystemCache; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Question\Question; |
14
|
|
|
|
15
|
|
|
class ApiListCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var FilesystemCache |
19
|
|
|
*/ |
20
|
|
|
private $cache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Configures the current command. |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this->setName('api:list') |
30
|
|
|
->setDescription('Generate API list cache.') |
31
|
|
|
->setHelp("This will generate the API list cache file usign the 'listApis' command."); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Executes the current command. |
36
|
|
|
* |
37
|
|
|
* @param InputInterface $input |
38
|
|
|
* @param OutputInterface $output |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : void |
42
|
|
|
{ |
43
|
|
|
$urlApi = $this->askUrlApi($input, $output); |
44
|
|
|
$apiKey = $this->askApiKey($input, $output); |
45
|
|
|
$secretKey = $this->askSecretKey($input, $output); |
46
|
|
|
|
47
|
|
|
$output->writeLn(''); |
48
|
|
|
$output->writeLn("<info>Processing API list. Please Wait...</info>"); |
49
|
|
|
|
50
|
|
|
$client = new Client([ |
51
|
|
|
'urlApi' => $urlApi, |
52
|
|
|
'apiKey' => $apiKey, |
53
|
|
|
'secretKey' => $secretKey, |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
$command = 'listApis'; |
57
|
|
|
$method = $client->getCommandMethod($command); |
58
|
|
|
$url = $client->getCommandUrl($command, []); |
59
|
|
|
$request = $client->getRequest($method, $url, []); |
60
|
|
|
|
61
|
|
|
$list = $client->getResponse($request); |
62
|
|
|
|
63
|
|
|
// We expect an array from the getResponse method, when this returns |
64
|
|
|
// with a string we most likely got a server error. |
65
|
|
|
if (is_string($list)) { |
66
|
|
|
throw new \RuntimeException(sprintf( |
67
|
|
|
"Invalid API response, received: %s", |
68
|
|
|
$list |
69
|
|
|
)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->processList($output, $list); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Ask for URL API. |
77
|
|
|
* |
78
|
|
|
* @param InputInterface $input |
79
|
|
|
* @param OutputInterface $output |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function askUrlApi(InputInterface $input, OutputInterface $output) : string |
83
|
|
|
{ |
84
|
|
|
$question = new Question( |
85
|
|
|
'Enter target API url [default: https://api.auroracompute.eu/ams]: ', |
86
|
|
|
'https://api.auroracompute.eu/ams' |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Ask for API key. |
94
|
|
|
* |
95
|
|
|
* @param InputInterface $input |
96
|
|
|
* @param OutputInterface $output |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
protected function askApiKey(InputInterface $input, OutputInterface $output) : string |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$question = (new Question('Enter API key: ')) |
102
|
|
|
->setValidator(function ($answer) { |
103
|
|
|
if (is_null($answer)) { |
104
|
|
|
throw new \InvalidArgumentException("API key can't be null."); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $answer; |
108
|
|
|
}) |
109
|
|
|
->setMaxAttempts(2); |
110
|
|
|
|
111
|
|
|
return $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Ask for secret key. |
116
|
|
|
* |
117
|
|
|
* @param InputInterface $input |
118
|
|
|
* @param OutputInterface $output |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
protected function askSecretKey(InputInterface $input, OutputInterface $output) : string |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$question = (new Question('Enter secret key: ')) |
124
|
|
|
->setValidator(function ($answer) { |
125
|
|
|
if (is_null($answer)) { |
126
|
|
|
throw new \InvalidArgumentException("Secret key can't be null."); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $answer; |
130
|
|
|
}) |
131
|
|
|
->setMaxAttempts(2); |
132
|
|
|
|
133
|
|
|
return $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Dump cache file of APIs list. |
138
|
|
|
* |
139
|
|
|
* @param OutputInterface $output |
140
|
|
|
* @param array $list |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
protected function processList(OutputInterface $output, array $list = []) : void |
144
|
|
|
{ |
145
|
|
|
if (empty($list)) { |
146
|
|
|
throw new \RuntimeException("API list is empty."); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$progress = new ProgressBar($output, $list['listapisresponse']['count']); |
150
|
|
|
$progress->start(); |
151
|
|
|
|
152
|
|
|
$commands = []; |
153
|
|
|
|
154
|
|
|
foreach ($list['listapisresponse']['api'] as $api) { |
155
|
|
|
$commands = array_merge($commands, $this->parseCommand($api)); |
156
|
|
|
|
157
|
|
|
$progress->advance(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->cache()->set('api.list', $commands); |
161
|
|
|
|
162
|
|
|
$progress->finish(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Parse command data into expected structure |
167
|
|
|
* @param array $command |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
|
protected function parseCommand(array $command) : array |
171
|
|
|
{ |
172
|
|
|
$params = []; |
173
|
|
|
|
174
|
|
|
foreach ($command['params'] as $param) { |
175
|
|
|
$params = array_merge($params, [ |
176
|
|
|
$param['name'] => [ |
177
|
|
|
'description' => $param['description'], |
178
|
|
|
'required' => $param['required'], |
179
|
|
|
'type' => $param['type'], |
180
|
|
|
] |
181
|
|
|
]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return [$command['name'] => [ |
185
|
|
|
'description' => $command['description'], |
186
|
|
|
'isasync' => $command['isasync'], |
187
|
|
|
'params' => $params |
188
|
|
|
]]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get cache driver instance |
193
|
|
|
* @return FilesystemCache |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
private function cache() : FilesystemCache |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
if (! isset($this->cache)) { |
198
|
|
|
$this->cache = new FilesystemCache('', 0, __DIR__.'/../../cache'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this->cache; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.