Completed
Push — master ( d6a82f...065486 )
by Zach
13s
created

Command::askPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Console;
4
5
use Symfony\Component\Console\Question\ChoiceQuestion;
6
use Symfony\Component\Console\Question\ConfirmationQuestion;
7
use Symfony\Component\Console\Question\Question;
8
use Yarak\Console\Input\Input;
9
use Yarak\Console\Output\SymfonyOutput;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Command\Command as SymfonyCommand;
13
14
abstract class Command extends SymfonyCommand
15
{
16
    /**
17
     * Symfony console output.
18
     *
19
     * @var SymfonyOutput
20
     */
21
    protected $output;
22
23
    /**
24
     * Symfony input implementation.
25
     *
26
     * @var InputInterface
27
     */
28
    protected $input;
29
30
    /**
31
     * The command description.
32
     *
33
     * @var string
34
     */
35
    protected $description;
36
37
    /**
38
     * Command signature.
39
     *
40
     * @var null|string
41
     */
42
    protected $signature = null;
43
44
    /**
45
     * Configure the command if signature is set.
46
     */
47
    protected function configure()
48
    {
49
        if (!is_null($this->signature)) {
50
            $parser = new SignatureParser($this);
51
52
            $parser->parse($this->signature);
53
54
            $this->setDescription($this->description);
55
        }
56
    }
57
58
    /**
59
     * Execute the command.
60
     *
61
     * @param InputInterface  $input
62
     * @param OutputInterface $output
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66
        $this->output = new SymfonyOutput($output);
67
68
        $this->input = $input;
69
70
        if (method_exists($this, 'handle')) {
71
            $this->handle();
72
        }
73
    }
74
75
    /**
76
     * Return the output implementation.
77
     *
78
     * @return SymfonyOutput
79
     */
80
    protected function getOutput()
81
    {
82
        return $this->output;
83
    }
84
85
    /**
86
     * Get root symfony output implementation.
87
     *
88
     * @return OutputInterface
89
     */
90
    protected function getOutputInterface()
91
    {
92
        return $this->getOutput()->getOutput();
93
    }
94
95
    /**
96
     * Get the value of a command argument.
97
     *
98
     * @param string $key
99
     *
100
     * @return string|array
101
     */
102
    protected function argument($key = null)
103
    {
104
        if (is_null($key)) {
105
            return $this->input->getArguments();
106
        }
107
108
        return $this->input->getArgument($key);
109
    }
110
111
    /**
112
     * Determine if the given argument is present.
113
     *
114
     * @param string|int $name
115
     *
116
     * @return bool
117
     */
118
    protected function hasArgument($name)
119
    {
120
        return $this->input->hasArgument($name);
121
    }
122
123
    /**
124
     * Get the value of a command option.
125
     *
126
     * @param string $key
127
     *
128
     * @return string|array
129
     */
130
    protected function option($key = null)
131
    {
132
        if (is_null($key)) {
133
            return $this->input->getOptions();
134
        }
135
136
        return $this->input->getOption($key);
137
    }
138
139
    /**
140
     * Determine if the given option is present.
141
     *
142
     * @param string $name
143
     *
144
     * @return bool
145
     */
146
    protected function hasOption($name)
147
    {
148
        return $this->input->hasOption($name);
149
    }
150
151
    /**
152
     * Add input to the command.
153
     *
154
     * @param Input $input
155
     */
156
    public function addInput(Input $input)
157
    {
158
        $reflection = new \ReflectionClass($input);
159
160
        $method = 'add'.$reflection->getShortName();
161
162
        if (method_exists($this, $method)) {
163
            $this->$method(...array_values($input->getAttributes()));
164
        }
165
    }
166
167
    /**
168
     * Ask for a confirmation.
169
     *
170
     * @param string $text
171
     *
172
     * @return mixed
173
     */
174
    public function confirm($text)
175
    {
176
        $helper = $this->getHelper('question');
177
        $question = new ConfirmationQuestion($text, false);
178
179
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
180
    }
181
182
    /**
183
     * Ask a question.
184
     *
185
     * @param string     $question
186
     * @param mixed|null $default
187
     *
188
     * @return mixed
189
     */
190 View Code Duplication
    public function ask($question, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
191
    {
192
        $helper = $this->getHelper('question');
193
        $question = new Question($question, $default);
194
195
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
196
    }
197
198
    /**
199
     * Ask a password.
200
     *
201
     * @param string $question
202
     *
203
     * @return mixed
204
     */
205
    public function askPassword($question)
206
    {
207
        $helper = $this->getHelper('question');
208
209
        $question = new Question($question);
210
        $question->setHidden(true);
211
        $question->setHiddenFallback(false);
212
213
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
214
    }
215
216
    /**
217
     * Ask a question where the answer is available from a list of predefined choices.
218
     *
219
     * @param string     $question
220
     * @param array      $choices
221
     * @param mixed|null $default
222
     *
223
     * @return mixed
224
     */
225 View Code Duplication
    public function choose($question, array $choices, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
226
    {
227
        $helper = $this->getHelper('question');
228
        $question = new ChoiceQuestion($question, $choices, $default);
229
        $question->setErrorMessage('%s is not a valid answer.');
230
231
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
232
    }
233
234
    /**
235
     * Ask a question where some auto-completion help is provided.
236
     *
237
     * @param string     $question
238
     * @param array      $autoCompletion
239
     * @param mixed|null $default
240
     *
241
     * @return mixed
242
     */
243 View Code Duplication
    public function anticipate($question, array $autoCompletion, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
244
    {
245
        $helper = $this->getHelper('question');
246
        $question = new Question($question, $default);
247
        $question->setAutocompleterValues($autoCompletion);
248
249
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
250
    }
251
252
    /**
253
     * Ask a question where the answer is available from a list of predefined choices and more choices can be selected.
254
     *
255
     * @param string     $question
256
     * @param array      $choices
257
     * @param mixed|null $default
258
     *
259
     * @return mixed
260
     */
261 View Code Duplication
    public function choice($question, array $choices, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
262
    {
263
        $helper = $this->getHelper('question');
264
        $question = new ChoiceQuestion($question, $choices, $default);
265
        $question->setMultiselect(true);
266
267
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
268
    }
269
}
270