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 (#46)
by Andy
02:37
created

ConfigureCommand::printQuestion()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.1158

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
ccs 10
cts 12
cp 0.8333
rs 8.439
cc 5
eloc 10
nc 8
nop 3
crap 5.1158
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('chronos_url', 'u', InputOption::VALUE_OPTIONAL, 'The chronos url (inclusive port)')
30 4
            ->addOption('chronos_http_username', 'un', InputOption::VALUE_OPTIONAL, 'The chronos username (HTTP credentials)', '')
31 4
            ->addOption('chronos_http_password', 'p', InputOption::VALUE_OPTIONAL, 'The chronos password (HTTP credentials)', '')
32 4
            ->addOption('chronos_url', 'u', InputOption::VALUE_OPTIONAL, 'The chronos url (inclusive port)')
33 4
            ->addOption('cache_dir', 'd', InputOption::VALUE_OPTIONAL, 'Path to cache directory')
34 4
            ->addOption('repository_dir', 'r', InputOption::VALUE_OPTIONAL, 'Root path to your job files')
35
        ;
36 4
    }
37
38
    /**
39
     * @param InputInterface $oInput
40
     * @param OutputInterface $oOutput
41
     * @return int
42
     */
43 4
    protected function execute(InputInterface $oInput, OutputInterface $oOutput)
44
    {
45 4
        $this->oInput = $oInput;
46 4
        $this->oOutput = $oOutput;
47
48 4
        return $this->process();
49
    }
50
51
    /**
52
     * @return int
53
     */
54 4
    protected function process()
55 1
    {
56 4
        $_aParams = $this->getInputValues();
57
58 4
        if ($this->hasValidateUserInput($_aParams))
59 4
        {
60 3
            $this->saveParameters($_aParams);
61 3
            return 0;
62
        }
63
64 1
        return 1;
65
    }
66
67
    /**
68
     * @return array<string,array<string,string|boolean>>
69
     */
70 4
    private function getInputValues()
71
    {
72 4
        $_aResult = [];
73
74 4
        $_aResult['chronos_url'] = [
75 4
            'value' => $this->getInputValue('chronos_url', 'Please enter the chronos url (inclusive port)'),
76
            'required' => true
77 4
        ];
78 4
        $_aResult['chronos_http_username'] = [
79 4
            'value' => $this->getInputValue('chronos_http_username', 'Please enter the username to access your chronos instance'),
80
            'required' => false
81 4
        ];
82 4
        $_aResult['chronos_http_password'] = [
83 4
            'value' => $this->getInputValue('chronos_http_password', 'Please enter the password to access your chronos instance', true),
84
            'required' => false
85 4
        ];
86 4
        $_aResult['cache_dir'] = [
87 4
            'value' => $this->getInputValue('cache_dir', 'Please enter a cache directory'),
88
            'required' => true
89 4
        ];
90 4
        $_aResult['repository_dir'] = [
91 4
            'value' => $this->getInputValue('repository_dir', 'Please enter your root path to your job files'),
92
            'required' => true
93 4
        ];
94
95 4
        return $_aResult;
96
    }
97
98
    /**
99
     * @param string $sValueKey
100
     * @param string $sQuestion
101
     * @param boolean $bHiddenAnswer
102
     * @return string
103
     */
104 4
    private function getInputValue($sValueKey, $sQuestion, $bHiddenAnswer = false)
105
    {
106 4
        $_sValue = $this->oInput->getOption($sValueKey);
107 4
        if (empty($_sValue))
108 4
        {
109 3
            $_sValue = $this->printQuestion(
110 3
                $sQuestion,
111 3
                $this->getParameterValue($sValueKey),
112
                $bHiddenAnswer
113 3
            );
114 3
        }
115
116 4
        return $_sValue;
117
    }
118
119
    /**
120
     * @param array $aUserInput
121
     */
122 3
    private function saveParameters(array $aUserInput)
123
    {
124
        // We implemented an additional level of information
125
        // into the user input array: Is this field required or not?
126
        // To be backwards compatible we only store the value of
127
        // the question in the dump file.
128
        // With this loop we get rid of the "required" information
129
        // from getInputValues().
130 3
        $aToStore = [];
131 3
        foreach ($aUserInput as $key => $value) {
132 3
            $aToStore[$key] = $value['value'];
133 3
        }
134
135 3
        $_oDumper = new Dumper();
136 3
        $_sYaml = $_oDumper->dump(array('parameters' => $aToStore));
137
138 3
        $_oFileSystem = new Filesystem();
139 3
        $_oFileSystem->dumpFile($this->getHomeDir() . '/parameters.yml', $_sYaml);
140 3
    }
141
142
    /**
143
     * @param array $aUserInput
144
     * @return bool
145
     */
146 4
    private function hasValidateUserInput(array $aUserInput)
147
    {
148 4
        foreach ($aUserInput as $_sKey => $_sValue)
149
        {
150 4
            if ($_sValue['required'] == true && empty($_sValue['value']))
151 4
            {
152 1
                $this->oOutput->writeln(sprintf('<error>Please add a valid value for parameter "%s"</error>', $_sKey));
153 1
                return false;
154
            }
155 4
        }
156
157 3
        return true;
158
    }
159
160
    /**
161
     * @param string $sKey
162
     * @param mixed $mDefaultValue
163
     * @return mixed
164
     */
165 3
    private function getParameterValue($sKey, $mDefaultValue = null)
166
    {
167 3
        $_oParser = new Parser();
168 3
        $_sParameterFile = $this->getHomeDir() . '/parameters.yml';
169
170 3
        if (file_exists($_sParameterFile))
171 3
        {
172
            $_aParameters = $_oParser->parse(
173
                file_get_contents($_sParameterFile)
174
            );
175
176
            if (isset($_aParameters['parameters']) && isset($_aParameters['parameters'][$sKey]))
177
            {
178
                return $_aParameters['parameters'][$sKey];
179
            }
180
        }
181
182 3
        return $mDefaultValue;
183
    }
184
185
    /**
186
     * @param string $sQuestion
187
     * @param null|mixed $mDefaultValue
188
     * @param boolean $bHiddenAnswer
189
     * @return mixed
190
     */
191 3
    private function printQuestion($sQuestion, $mDefaultValue = null, $bHiddenAnswer = false)
192
    {
193 3
        $_oHelper = $this->getHelper('question');
194
195
        // If we have a hidden answer and the default value is not empty
196
        // the we will set it as empty, because we don`t want to show
197
        // the default value on the terminal.
198
        // We know that the user has to enter the password again
199
        // if he / she want to reconfigure something. But this
200
        // is an acceptable tradeoff.
201 3
        if ($bHiddenAnswer === true && !empty($mDefaultValue)) {
202
            $mDefaultValue = null;
203
        }
204
205 3
        $_sFormat = (!empty($mDefaultValue)) ? '<comment>%s (default: %s):</comment>' : '<comment>%s:</comment>';
206 3
        $_oQuestion = new Question(sprintf($_sFormat, $sQuestion, $mDefaultValue), $mDefaultValue);
207
208
        // Sensitive information (like passwords) should not be
209
        // visible during the configuration wizard
210 3
        if ($bHiddenAnswer === true) {
211 3
            $_oQuestion->setHidden(true);
212 3
            $_oQuestion->setHiddenFallback(false);
213 3
        }
214
215 3
        return $_oHelper->ask($this->oInput, $this->oOutput, $_oQuestion);
216
    }
217
}