GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 6a2eee...16b05e )
by Marc
11s
created

ConfigureCommand::saveParameters()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 40
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-28
7
 *
8
 */
9
10
namespace Chapi\Commands;
11
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Question\Question;
16
use Symfony\Component\Filesystem\Filesystem;
17
use Symfony\Component\Yaml\Dumper;
18
use Symfony\Component\Yaml\Parser;
19
20
class ConfigureCommand extends AbstractCommand
21
{
22
    /**
23
     * Configures the current command.
24
     */
25 4
    protected function configure()
26
    {
27 4
        $this->setName('configure')
28 4
            ->setDescription('Configure application and add necessary configs')
29 4
            ->addOption('cache_dir', 'd', InputOption::VALUE_OPTIONAL, 'Path to cache directory')
30
31 4
            ->addOption('chronos_url', 'u', InputOption::VALUE_OPTIONAL, 'The chronos url (inclusive port)', '')
32 4
            ->addOption('chronos_http_username', 'un', InputOption::VALUE_OPTIONAL, 'The chronos username (HTTP credentials)', '')
33 4
            ->addOption('chronos_http_password', 'p', InputOption::VALUE_OPTIONAL, 'The chronos password (HTTP credentials)', '')
34 4
            ->addOption('repository_dir', 'r', InputOption::VALUE_OPTIONAL, 'Root path to your job files', '')
35
36 4
            ->addOption('marathon_url', 'mu', InputOption::VALUE_OPTIONAL, 'The marathon url (inclusive port)', '')
37 4
            ->addOption('marathon_http_username', 'mun', InputOption::VALUE_OPTIONAL, 'The marathon username (HTTP credentials)', '')
38 4
            ->addOption('marathon_http_password', 'mp', InputOption::VALUE_OPTIONAL, 'The marathon password (HTTP credentials)', '')
39 4
            ->addOption('repository_dir_marathon', 'mr', InputOption::VALUE_OPTIONAL, 'Root path to the app files', '')
40
        ;
41 4
    }
42
43
    /**
44
     * @param InputInterface $oInput
45
     * @param OutputInterface $oOutput
46
     * @return int
47
     */
48 4
    protected function execute(InputInterface $oInput, OutputInterface $oOutput)
49
    {
50 4
        $this->oInput = $oInput;
51 4
        $this->oOutput = $oOutput;
52
53 4
        return $this->process();
54
    }
55
56
    /**
57
     * @return int
58
     */
59 4
    protected function process()
60
    {
61 4
        $_aParams = $this->getInputValues();
62
63 4
        if ($this->hasValidateUserInput($_aParams))
64
        {
65 3
            $this->saveParameters($_aParams);
66 3
            return 0;
67
        }
68
69 1
        return 1;
70
    }
71
72
    /**
73
     * @return array<string,array<string,string|boolean>>
74
     */
75 4
    private function getInputValues()
76
    {
77 4
        $_aResult = [];
78
79 4
        $_aResult['cache_dir'] = [
80 4
            'value' => $this->getInputValue('cache_dir', '[GLOBAL] Please enter a cache directory'),
81
            'required' => true
82
        ];
83
84 4
        $_aResult['chronos_url'] = [
85 4
            'value' => $this->getInputValue('chronos_url', '[CHRONOS] Please enter the chronos url (inclusive port)'),
86
            'required' => false
87
        ];
88
89 4
        $_aResult['chronos_http_username'] = [
90 4
            'value' => $this->getInputValue('chronos_http_username', '[CHRONOS] Please enter the username to access your chronos instance'),
91
            'required' => false
92
        ];
93
94 4
        $_aResult['chronos_http_password'] = [
95 4
            'value' => $this->getInputValue('chronos_http_password', '[CHRONOS] Please enter the password to access your chronos instance', true),
96
            'required' => false
97
        ];
98
99 4
        $_aResult['repository_dir'] = [
100 4
            'value' => $this->getInputValue('repository_dir', '[CHRONOS] Please enter absolute path to your local chronos jobs configurations'),
101
            'required' => false
102
        ];
103
104 4
        $_aResult['marathon_url'] = [
105 4
            'value' => $this->getInputValue('marathon_url', '[MARATHON] Please enter the marathon url (inclusive port)'),
106
            'required' => false
107
        ];
108
109 4
        $_aResult['marathon_http_username'] = [
110 4
            'value' => $this->getInputValue('marathon_http_username', '[MARATHON] Please enter the username to access marathon instance'),
111
            'required' => false
112
        ];
113
114 4
        $_aResult['marathon_http_password'] = [
115 4
            'value' => $this->getInputValue('marathon_http_password', '[MARATHON] Please enter the password to access marathon instance', true),
116
            'required' => false
117
        ];
118
119 4
        $_aResult['repository_dir_marathon'] = [
120 4
            'value' => $this->getInputValue('repository_dir_marathon', '[MARATHON] Please enter absolute path to your local marathon tasks configurations'),
121
            'required' => false
122
        ];
123
124 4
        return $_aResult;
125
    }
126
127
    /**
128
     * @param string $sValueKey
129
     * @param string $sQuestion
130
     * @param boolean $bHiddenAnswer
131
     * @return string
132
     */
133 4
    private function getInputValue($sValueKey, $sQuestion, $bHiddenAnswer = false)
134
    {
135 4
        $_sValue = $this->oInput->getOption($sValueKey);
136 4
        if (empty($_sValue))
137
        {
138 3
            $_sValue = $this->printQuestion(
139
                $sQuestion,
140 3
                $this->getParameterValue($sValueKey),
141
                $bHiddenAnswer
142
            );
143
        }
144
145 4
        return $_sValue;
146
    }
147
148
    /**
149
     * @param array $aUserInput
150
     */
151 3
    private function saveParameters(array $aUserInput)
152
    {
153
        // We implemented an additional level of information
154
        // into the user input array: Is this field required or not?
155
        // To be backwards compatible we only store the value of
156
        // the question in the dump file.
157
        // With this loop we get rid of the "required" information
158
        // from getInputValues().
159 3
        $aToStore = [];
160 3
        foreach ($aUserInput as $key => $value)
161
        {
162 3
            $aToStore[$key] = ('null' === $value['value']) ? null : $value['value'];
163
        }
164
165
        $_aConfigToSave = [
166 3
            $this->getProfileName() => [
167 3
                'parameters' => $aToStore
168
            ]
169
        ];
170
171 3
        $_sPath = $this->getHomeDir() . DIRECTORY_SEPARATOR . $this->getParameterFileName();
172
173
        // load exiting config to merge
174 3
        $_aConfig = $this->loadConfigFile(['profiles' => []]);
175
176
        $_aFinalConfig = [
177 3
            'profiles' => array_merge($_aConfig['profiles'], $_aConfigToSave)
178
        ];
179
180
181
        // dump final config
182 3
        $_oDumper = new Dumper();
183 3
        $_sYaml = $_oDumper->dump($_aFinalConfig, 4);
184
185 3
        $_oFileSystem = new Filesystem();
186 3
        $_oFileSystem->dumpFile(
187
            $_sPath,
188
            $_sYaml
189
        );
190 3
    }
191
192
    /**
193
     * @param array $aUserInput
194
     * @return bool
195
     */
196 4
    private function hasValidateUserInput(array $aUserInput)
197
    {
198 4
        foreach ($aUserInput as $_sKey => $_sValue)
199
        {
200 4
            if ($_sValue['required'] == true && empty($_sValue['value']))
201
            {
202 1
                $this->oOutput->writeln(sprintf('<error>Please add a valid value for parameter "%s"</error>', $_sKey));
203 4
                return false;
204
            }
205
        }
206
207 3
        return true;
208
    }
209
210
    /**
211
     * @param string $sKey
212
     * @param mixed $mDefaultValue
213
     * @return mixed
214
     */
215 3
    private function getParameterValue($sKey, $mDefaultValue = null)
216
    {
217 3
        $_aParameters = $this->getParameters();
218
219 3
        if (isset($_aParameters['parameters']) && isset($_aParameters['parameters'][$sKey]))
220
        {
221
            return $_aParameters['parameters'][$sKey];
222
        }
223
224 3
        return $mDefaultValue;
225
    }
226
227
    /**
228
     * @return array
229
     */
230 3
    private function getParameters()
231
    {
232 3
        $_sProfile = $this->getProfileName();
233 3
        $_aParameters = $this->loadConfigFile();
234
235 3
        return (isset($_aParameters['profiles']) && isset($_aParameters['profiles'][$_sProfile]))
236
            ? $_aParameters['profiles'][$_sProfile]
237 3
            : ['profiles' => []];
238
    }
239
240
    /**
241
     * @param mixed $mDefaultValue
242
     * @return mixed
243
     */
244 4
    private function loadConfigFile($mDefaultValue = [])
245
    {
246 4
        $_sParameterFile = $this->getHomeDir() . DIRECTORY_SEPARATOR . $this->getParameterFileName();
247
248 4
        if (!file_exists($_sParameterFile)) {
249 4
            return $mDefaultValue;
250
        }
251
252
        $_oParser = new Parser();
253
254
        $_aParameters = $_oParser->parse(
255
            file_get_contents($_sParameterFile)
256
        );
257
258
        return $_aParameters;
259
    }
260
261
262
    /**
263
     * @param string $sQuestion
264
     * @param null|mixed $mDefaultValue
265
     * @param boolean $bHiddenAnswer
266
     * @return mixed
267
     */
268 3
    private function printQuestion($sQuestion, $mDefaultValue = null, $bHiddenAnswer = false)
269
    {
270 3
        $_oHelper = $this->getHelper('question');
271
272
        // If we have a hidden answer and the default value is not empty
273
        // the we will set it as empty, because we don`t want to show
274
        // the default value on the terminal.
275
        // We know that the user has to enter the password again
276
        // if he / she want to reconfigure something. But this
277
        // is an acceptable tradeoff.
278 3
        if ($bHiddenAnswer === true && !empty($mDefaultValue))
279
        {
280
            $mDefaultValue = null;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $mDefaultValue. This often makes code more readable.
Loading history...
281
        }
282
283 3
        $_sFormat = (!empty($mDefaultValue)) ? '<comment>%s (default: %s):</comment>' : '<comment>%s:</comment>';
284 3
        $_oQuestion = new Question(sprintf($_sFormat, $sQuestion, $mDefaultValue), $mDefaultValue);
285
286
        // Sensitive information (like passwords) should not be
287
        // visible during the configuration wizard
288 3
        if ($bHiddenAnswer === true)
289
        {
290 3
            $_oQuestion->setHidden(true);
291 3
            $_oQuestion->setHiddenFallback(false);
292
        }
293
294 3
        return $_oHelper->ask($this->oInput, $this->oOutput, $_oQuestion);
295
    }
296
}