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; |
10
|
|
|
|
11
|
|
|
use DateTimeZone; |
12
|
|
|
use Gojira\Api\Authentication\BasicAuthentication; |
13
|
|
|
use Gojira\Api\Authentication\JiraBasicAuthentication; |
14
|
|
|
use Gojira\Api\Configuration\Auth; |
15
|
|
|
use Gojira\Api\Configuration\AuthInterface; |
16
|
|
|
use Gojira\Api\Configuration\Configuration; |
17
|
|
|
use Gojira\Api\Configuration\ConfigurationInterface; |
18
|
|
|
use Gojira\Api\Configuration\Options; |
19
|
|
|
use Gojira\Api\Configuration\OptionsInterface; |
20
|
|
|
use Gojira\Api\Configuration\Path; |
21
|
|
|
use Gojira\Provider\Console\Command; |
22
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
23
|
|
|
use Symfony\Component\Console\Input\InputOption; |
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
26
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
27
|
|
|
use Symfony\Component\Console\Question\Question; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Config Command, check and set config before start using the application |
31
|
|
|
* |
32
|
|
|
* @package Gojira\Command |
33
|
|
|
* @author Toan Nguyen <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class ConfigCommand extends Command |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var \Gojira\Api\Authentication\JiraBasicAuthentication |
39
|
|
|
*/ |
40
|
|
|
protected $authentication = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Gojira\Api\Configuration\Configuration |
44
|
|
|
*/ |
45
|
|
|
protected $configuration = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Gojira\Api\Configuration\Path |
49
|
|
|
*/ |
50
|
|
|
protected $pathConfig = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Gojira\Api\Configuration\Auth |
54
|
|
|
*/ |
55
|
|
|
protected $authConfig = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \Gojira\Api\Configuration\Options |
59
|
|
|
*/ |
60
|
|
|
protected $optionConfig = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
protected function configure() |
66
|
|
|
{ |
67
|
|
|
$this->configuration = new Configuration(); |
68
|
|
|
$this->authConfig = new Auth(); |
69
|
|
|
$this->optionConfig = new Options(); |
70
|
|
|
$this->pathConfig = new Path(); |
71
|
|
|
$this->authentication = new JiraBasicAuthentication($this->configuration); |
72
|
|
|
|
73
|
|
|
$help = __( |
74
|
|
|
"Config Help:\n%1\n%2\n%3\n%4\n%5", |
75
|
|
|
' - Jira URL: https://foo.atlassian.net/', |
76
|
|
|
' - Username: user (for [email protected])', |
77
|
|
|
' - Password: Your password', |
78
|
|
|
' - Timezone: Choose your timezone', |
79
|
|
|
' - Use cache: Choose cache mode' |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$this |
83
|
|
|
->setName('config') |
84
|
|
|
->setDescription('Change configuration') |
85
|
|
|
->setHelp($help) |
86
|
|
|
->addOption('clear', 'c', InputOption::VALUE_NONE, 'Clear stored configuration'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
93
|
|
|
{ |
94
|
|
|
$helper = $this->getHelper('question'); |
95
|
|
|
|
96
|
|
|
if ($input->getOption('clear')) { |
97
|
|
|
$result = $this->configuration->clearConfig(); |
98
|
|
|
$output->writeln('<info>' . $result['msg'] . '</info>'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!$this->authentication->isAuth()) { |
102
|
|
|
$jiraUrl = $helper->ask($input, $output, $this->getJiraUrlQuestion()); |
103
|
|
|
$username = $helper->ask($input, $output, $this->getJiraUsernameQuestion()); |
104
|
|
|
$password = $helper->ask($input, $output, $this->getJiraPasswordQuestion()); |
105
|
|
|
$timezones = $helper->ask($input, $output, $this->chooseTimezoneQuestion()); |
106
|
|
|
$useCache = $helper->ask($input, $output, $this->chooseCacheModeQuestion()); |
107
|
|
|
|
108
|
|
|
if (isset($jiraUrl, $username, $password, $timezones, $useCache)) { |
109
|
|
|
$authenticate = new BasicAuthentication($username, $password); |
110
|
|
|
$authItems = [ |
111
|
|
|
AuthInterface::BASE_URI => $jiraUrl, |
112
|
|
|
AuthInterface::USERNAME => $username, |
113
|
|
|
AuthInterface::TOKEN_SECRET => $authenticate->getCredential(), |
114
|
|
|
AuthInterface::CONSUMER_SECRET => uniqid(), |
115
|
|
|
AuthInterface::BCRYPT_MODE => false |
116
|
|
|
]; |
117
|
|
|
$optionItems = array_merge( |
118
|
|
|
$this->optionConfig->initDefaultOptionItems(), |
119
|
|
|
[OptionsInterface::TIMEZONE => $timezones, OptionsInterface::IS_USE_CACHE => $useCache] |
120
|
|
|
); |
121
|
|
|
$pathItems = $this->pathConfig->initDefaultPaths(); |
122
|
|
|
|
123
|
|
|
$this->authConfig->setData($authItems); |
124
|
|
|
$this->optionConfig->setData($optionItems); |
125
|
|
|
$this->pathConfig->setData($pathItems); |
126
|
|
|
|
127
|
|
|
$this->configuration->setData(ConfigurationInterface::PATHS, $this->pathConfig->getData()); |
128
|
|
|
$this->configuration->setData(ConfigurationInterface::AUTH, $this->authConfig->getData()); |
129
|
|
|
$this->configuration->setData(ConfigurationInterface::OPTIONS, $this->optionConfig->getData()); |
130
|
|
|
$result = $this->configuration->saveConfig(); |
131
|
|
|
|
132
|
|
|
$output->writeln(__('<info>%1</info>', $result['msg'])); |
133
|
|
|
} |
134
|
|
|
} else { |
135
|
|
|
$output->writeln(__( |
136
|
|
|
'STATUS: [<comment>%1</comment>]', |
137
|
|
|
'Authorized' |
138
|
|
|
)); |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @var Command $command |
142
|
|
|
*/ |
143
|
|
|
$command = $this->getApplication()->getService('console')->find('list'); |
144
|
|
|
$command->run($input, $output); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get JIRA URL question |
150
|
|
|
* |
151
|
|
|
* @return Question |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
private function getJiraUrlQuestion() |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$question = new Question('<question>Please enter your Jira URL:</question> '); |
156
|
|
|
$question->setValidator(function ($value) { |
157
|
|
|
if (trim($value) === '') { |
158
|
|
|
throw new \Exception('The URL cannot be empty'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $value; |
162
|
|
|
}); |
163
|
|
|
|
164
|
|
|
return $question; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get JIRA username question |
169
|
|
|
* |
170
|
|
|
* @return Question |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
private function getJiraUsernameQuestion() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$question = new Question('<question>Please enter your Jira username:</question> '); |
175
|
|
|
$question->setValidator(function ($value) { |
176
|
|
|
if (trim($value) === '') { |
177
|
|
|
throw new \Exception('The username cannot be empty'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $value; |
181
|
|
|
}); |
182
|
|
|
|
183
|
|
|
return $question; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get JIRA password question |
188
|
|
|
* |
189
|
|
|
* @return Question |
190
|
|
|
*/ |
191
|
|
|
private function getJiraPasswordQuestion() |
192
|
|
|
{ |
193
|
|
|
$question = new Question('<question>Please enter your Jira password:</question> '); |
194
|
|
|
$question->setValidator(function ($value) { |
195
|
|
|
if (trim($value) === '') { |
196
|
|
|
throw new \Exception('The password cannot be empty'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $value; |
200
|
|
|
}); |
201
|
|
|
$question->setHidden(true); |
202
|
|
|
$question->setMaxAttempts(20); |
203
|
|
|
|
204
|
|
|
return $question; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Choose server timezone question |
209
|
|
|
* |
210
|
|
|
* @return ChoiceQuestion |
211
|
|
|
*/ |
212
|
|
|
private function chooseTimezoneQuestion() |
213
|
|
|
{ |
214
|
|
|
$timezones = DateTimeZone::listIdentifiers(); |
215
|
|
|
$question = new ChoiceQuestion( |
216
|
|
|
'<question>Please choose your server timezone (defaults to Australia/Sydney):</question> ', |
217
|
|
|
$timezones, |
218
|
|
|
313 |
219
|
|
|
); |
220
|
|
|
$question->setErrorMessage('Timezone %s is invalid'); |
221
|
|
|
|
222
|
|
|
return $question; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Select cache mode question |
227
|
|
|
* |
228
|
|
|
* @return ConfirmationQuestion |
229
|
|
|
*/ |
230
|
|
|
private function chooseCacheModeQuestion() |
231
|
|
|
{ |
232
|
|
|
$question = new ConfirmationQuestion('<question>Please select HttpClient cache mode:</question> ', false); |
233
|
|
|
|
234
|
|
|
return $question; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
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.