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
Pull Request — master (#68)
by Bidesh
04:18
created

ConfigureCommand::printQuestion()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 28
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0729

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
ccs 12
cts 14
cp 0.8571
rs 8.439
cc 5
eloc 10
nc 8
nop 3
crap 5.0729
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 4
        {
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', 'Please enter a cache directory'),
81
            'required' => true
82 4
        ];
83
84 4
        $_aResult['chronos_url'] = [
85 4
            'value' => $this->getInputValue('chronos_url', 'Please enter the chronos url (inclusive port)'),
86
            'required' => false
87 4
        ];
88
89 4
        $_aResult['chronos_http_username'] = [
90 4
            'value' => $this->getInputValue('chronos_http_username', 'Please enter the username to access your chronos instance'),
91 1
            'required' => false
92 4
        ];
93
94 4
        $_aResult['chronos_http_password'] = [
95 4
            'value' => $this->getInputValue('chronos_http_password', 'Please enter the password to access your chronos instance', true),
96
            'required' => false
97 4
        ];
98
99 4
        $_aResult['repository_dir'] = [
100 4
            'value' => $this->getInputValue('repository_dir', 'Please enter your root path to your job files'),
101
            'required' => false
102 4
        ];
103
104 4
        $_aResult['marathon_url'] = [
105 4
            'value' => $this->getInputValue('marathon_url', 'Please enter the marathon url (inclusive port)'),
106
            'required' => false
107 4
        ];
108
109 4
        $_aResult['marathon_http_username'] = [
110 4
            'value' => $this->getInputValue('marathon_http_username', 'Please enter the username to access marathon instance'),
111
            'required' => false
112 4
        ];
113
114 4
        $_aResult['marathon_http_password'] = [
115 4
            'value' => $this->getInputValue('marathon_http_password', 'Please enter the password to access marathon instance', true),
116
            'required' => false
117 4
        ];
118
119 4
        $_aResult['repository_dir_marathon'] = [
120 4
            'value' => $this->getInputValue('repository_dir_marathon', 'Please enter the root path to your app files'),
121
            'required' => false
122 4
        ];
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 4
        {
138 3
            $_sValue = $this->printQuestion(
139 3
                $sQuestion,
140 3
                $this->getParameterValue($sValueKey),
141
                $bHiddenAnswer
142 3
            );
143 3
        }
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] = $value['value'];
163 3
        }
164
165 3
        $_oDumper = new Dumper();
166 3
        $_sYaml = $_oDumper->dump(array('parameters' => $aToStore), 2);
167
168 3
        $_oFileSystem = new Filesystem();
169 3
        $_oFileSystem->dumpFile($this->getHomeDir() . '/parameters.yml', $_sYaml);
170 3
    }
171
172
    /**
173
     * @param array $aUserInput
174
     * @return bool
175
     */
176 4
    private function hasValidateUserInput(array $aUserInput)
177
    {
178 4
        foreach ($aUserInput as $_sKey => $_sValue)
179
        {
180 4
            if ($_sValue['required'] == true && empty($_sValue['value']))
181 4
            {
182 1
                $this->oOutput->writeln(sprintf('<error>Please add a valid value for parameter "%s"</error>', $_sKey));
183 1
                return false;
184
            }
185 3
        }
186
187 3
        return true;
188
    }
189
190
    /**
191
     * @param string $sKey
192
     * @param mixed $mDefaultValue
193
     * @return mixed
194
     */
195 3
    private function getParameterValue($sKey, $mDefaultValue = null)
196
    {
197 3
        $_oParser = new Parser();
198 3
        $_sParameterFile = $this->getHomeDir() . '/parameters.yml';
199
200 3
        if (file_exists($_sParameterFile))
201 3
        {
202
            $_aParameters = $_oParser->parse(
203
                file_get_contents($_sParameterFile)
204
            );
205
206
            if (isset($_aParameters['parameters']) && isset($_aParameters['parameters'][$sKey]))
207
            {
208
                return $_aParameters['parameters'][$sKey];
209
            }
210
        }
211
212 3
        return $mDefaultValue;
213
    }
214
215
    /**
216
     * @param string $sQuestion
217
     * @param null|mixed $mDefaultValue
218
     * @param boolean $bHiddenAnswer
219
     * @return mixed
220
     */
221 3
    private function printQuestion($sQuestion, $mDefaultValue = null, $bHiddenAnswer = false)
222
    {
223 3
        $_oHelper = $this->getHelper('question');
224
225
        // If we have a hidden answer and the default value is not empty
226
        // the we will set it as empty, because we don`t want to show
227
        // the default value on the terminal.
228
        // We know that the user has to enter the password again
229
        // if he / she want to reconfigure something. But this
230
        // is an acceptable tradeoff.
231 3
        if ($bHiddenAnswer === true && !empty($mDefaultValue))
232 3
        {
233
            $mDefaultValue = null;
234
        }
235
236 3
        $_sFormat = (!empty($mDefaultValue)) ? '<comment>%s (default: %s):</comment>' : '<comment>%s:</comment>';
237 3
        $_oQuestion = new Question(sprintf($_sFormat, $sQuestion, $mDefaultValue), $mDefaultValue);
238
239
        // Sensitive information (like passwords) should not be
240
        // visible during the configuration wizard
241 3
        if ($bHiddenAnswer === true)
242 3
        {
243 3
            $_oQuestion->setHidden(true);
244 3
            $_oQuestion->setHiddenFallback(false);
245 3
        }
246
247 3
        return $_oHelper->ask($this->oInput, $this->oOutput, $_oQuestion);
248
    }
249
}