Completed
Pull Request — master (#31)
by
unknown
01:53
created

Command::choose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 8
loc 8
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 $text
171
     * @return mixed
172
     */
173
    public function confirm($text)
174
    {
175
        $helper = $this->getHelper('question');
176
        $question = new ConfirmationQuestion($text, false);
177
178
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
179
    }
180
181
    /**
182
     * Ask a question.
183
     *
184
     * @param $question
185
     * @param mixed|null $default
186
     * @return mixed
187
     */
188 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...
189
    {
190
        $helper = $this->getHelper('question');
191
        $question = new Question($question, $default);
192
193
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
194
    }
195
196
    /**
197
     * Ask a password
198
     *
199
     * @param $question
200
     * @return mixed
201
     */
202
    public function askPassword($question)
203
    {
204
        $helper = $this->getHelper('question');
205
206
        $question = new Question($question);
207
        $question->setHidden(true);
208
        $question->setHiddenFallback(false);
209
210
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
211
    }
212
213
    /**
214
     * Ask a question where the answer is available from a list of predefined choices.
215
     *
216
     * @param $question
217
     * @param array $choices
218
     * @param mixed|null $default
219
     *
220
     * @return mixed
221
     */
222 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...
223
    {
224
        $helper = $this->getHelper('question');
225
        $question = new ChoiceQuestion($question, $choices, $default);
226
        $question->setErrorMessage('%s is not a valid answer.');
227
228
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
229
    }
230
231
    /**
232
     * Ask a question where some auto-completion help is provided.
233
     *
234
     * @param $question
235
     * @param array $autoCompletion
236
     * @param mixed|null $default
237
     *
238
     * @return mixed
239
     */
240 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...
241
    {
242
        $helper = $this->getHelper('question');
243
        $question = new Question($question, $default);
244
        $question->setAutocompleterValues($autoCompletion);
245
246
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
247
    }
248
249
    /**
250
     * Ask a question where the answer is available from a list of predefined choices and more choices can be selected.
251
     *
252
     * @param $question
253
     * @param array $choices
254
     * @param mixed|null $default
255
     *
256
     * @return mixed
257
     */
258 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...
259
    {
260
        $helper = $this->getHelper('question');
261
        $question = new ChoiceQuestion($question, $choices, $default);
262
        $question->setMultiselect(true);
263
264
        return $helper->ask($this->input, $this->getOutputInterface(), $question);
265
    }
266
}
267