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
02:54
created

ConfigureCommand::hasValidateUserInput()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
crap 4
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 3
            $aToStore[$key] = $value['value'];
162 3
        }
163
164 3
        $_oDumper = new Dumper();
165 3
        $_sYaml = $_oDumper->dump(array('parameters' => $aToStore), 2);
166
167 3
        $_oFileSystem = new Filesystem();
168 3
        $_oFileSystem->dumpFile($this->getHomeDir() . '/parameters.yml', $_sYaml);
169 3
    }
170
171
    /**
172
     * @param array $aUserInput
173
     * @return bool
174
     */
175 4
    private function hasValidateUserInput(array $aUserInput)
176
    {
177 4
        foreach ($aUserInput as $_sKey => $_sValue)
178
        {
179 4
            if ($_sValue['required'] == true && empty($_sValue['value']))
180 4
            {
181 1
                $this->oOutput->writeln(sprintf('<error>Please add a valid value for parameter "%s"</error>', $_sKey));
182 1
                return false;
183
            }
184 3
        }
185
186 3
        return true;
187
    }
188
189
    /**
190
     * @param string $sKey
191
     * @param mixed $mDefaultValue
192
     * @return mixed
193
     */
194 3
    private function getParameterValue($sKey, $mDefaultValue = null)
195
    {
196 3
        $_oParser = new Parser();
197 3
        $_sParameterFile = $this->getHomeDir() . '/parameters.yml';
198
199 3
        if (file_exists($_sParameterFile))
200 3
        {
201
            $_aParameters = $_oParser->parse(
202
                file_get_contents($_sParameterFile)
203
            );
204
205
            if (isset($_aParameters['parameters']) && isset($_aParameters['parameters'][$sKey]))
206
            {
207
                return $_aParameters['parameters'][$sKey];
208
            }
209
        }
210
211 3
        return $mDefaultValue;
212
    }
213
214
    /**
215
     * @param string $sQuestion
216
     * @param null|mixed $mDefaultValue
217
     * @param boolean $bHiddenAnswer
218
     * @return mixed
219
     */
220 3
    private function printQuestion($sQuestion, $mDefaultValue = null, $bHiddenAnswer = false)
221
    {
222 3
        $_oHelper = $this->getHelper('question');
223
224
        // If we have a hidden answer and the default value is not empty
225
        // the we will set it as empty, because we don`t want to show
226
        // the default value on the terminal.
227
        // We know that the user has to enter the password again
228
        // if he / she want to reconfigure something. But this
229
        // is an acceptable tradeoff.
230 3
        if ($bHiddenAnswer === true && !empty($mDefaultValue)) {
231
            $mDefaultValue = null;
232
        }
233
234 3
        $_sFormat = (!empty($mDefaultValue)) ? '<comment>%s (default: %s):</comment>' : '<comment>%s:</comment>';
235 3
        $_oQuestion = new Question(sprintf($_sFormat, $sQuestion, $mDefaultValue), $mDefaultValue);
236
237
        // Sensitive information (like passwords) should not be
238
        // visible during the configuration wizard
239 3
        if ($bHiddenAnswer === true) {
240 3
            $_oQuestion->setHidden(true);
241 3
            $_oQuestion->setHiddenFallback(false);
242 3
        }
243
244 3
        return $_oHelper->ask($this->oInput, $this->oOutput, $_oQuestion);
245
    }
246
}