Passed
Push — master ( 7bee4d...457c92 )
by Sebastian
01:54
created

DefaultIO::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Console\IO;
11
12
use CaptainHook\App\Console\IO;
13
use CaptainHook\App\Console\IOUtil;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Helper\HelperSet;
16
use Symfony\Component\Console\Output\ConsoleOutputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
use Symfony\Component\Console\Question\Question;
20
21
/**
22
 * Class DefaultIO
23
 *
24
 * @package CaptainHook
25
 * @author  Sebastian Feldmann <[email protected]>
26
 * @link    https://github.com/captainhookphp/captainhook
27
 * @since   Class available since Release 0.9.0
28
 */
29
class DefaultIO extends Base
30
{
31
    /**
32
     * @var \Symfony\Component\Console\Input\InputInterface
33
     */
34
    protected $input;
35
36
    /**
37
     * @var \Symfony\Component\Console\Output\OutputInterface
38
     */
39
    protected $output;
40
41
    /**
42
     * @var \Symfony\Component\Console\Helper\HelperSet
43
     */
44
    protected $helperSet;
45
46
    /**
47
     * @var array
48
     */
49
    private $verbosityMap;
50
51
    /**
52
     * Constructor
53
     *
54
     * @param \Symfony\Component\Console\Input\InputInterface   $input
55
     * @param \Symfony\Component\Console\Output\OutputInterface $output
56
     * @param \Symfony\Component\Console\Helper\HelperSet       $helperSet
57
     */
58 15
    public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet = null)
59
    {
60 15
        $this->input        = $input;
61 15
        $this->output       = $output;
62 15
        $this->helperSet    = $helperSet;
63 15
        $this->verbosityMap = [
64
            IO::QUIET        => OutputInterface::VERBOSITY_QUIET,
65
            IO::NORMAL       => OutputInterface::VERBOSITY_NORMAL,
66
            IO::VERBOSE      => OutputInterface::VERBOSITY_VERBOSE,
67
            IO::VERY_VERBOSE => OutputInterface::VERBOSITY_VERY_VERBOSE,
68
            IO::DEBUG        => OutputInterface::VERBOSITY_DEBUG
69
        ];
70 15
    }
71
72
    /**
73
     * Return the original cli arguments
74
     *
75
     * @return array
76
     */
77 2
    public function getArguments(): array
78
    {
79 2
        return $this->input->getArguments();
80
    }
81
82
    /**
83
     * Return the original cli argument or a given default
84
     *
85
     * @param  string $name
86
     * @param  string $default
87
     * @return string
88
     */
89 1
    public function getArgument(string $name, string $default = '') : string
90
    {
91 1
        return (string)($this->getArguments()[$name] ?? $default);
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 1
    public function isInteractive()
98
    {
99 1
        return $this->input->isInteractive();
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 1
    public function isVerbose()
106
    {
107 1
        return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113 1
    public function isVeryVerbose()
114
    {
115 1
        return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121 1
    public function isDebug()
122
    {
123 1
        return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG;
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129 1
    public function write($messages, $newline = true, $verbosity = self::NORMAL)
130
    {
131 1
        $this->doWrite($messages, $newline, false, $verbosity);
132 1
    }
133
134
    /**
135
     * {@inheritDoc}
136
     */
137 3
    public function writeError($messages, $newline = true, $verbosity = self::NORMAL)
138
    {
139 3
        $this->doWrite($messages, $newline, true, $verbosity);
140 3
    }
141
142
    /**
143
     * Write to the appropriate user output
144
     *
145
     * @param array|string $messages
146
     * @param bool         $newline
147
     * @param bool         $stderr
148
     * @param int          $verbosity
149
     */
150 4
    private function doWrite($messages, $newline, $stderr, $verbosity)
151
    {
152 4
        $sfVerbosity = $this->verbosityMap[$verbosity];
153 4
        if ($sfVerbosity > $this->output->getVerbosity()) {
154 1
            return;
155
        }
156
157 3
        $this->getOutputToWriteTo($stderr)->write($messages, $newline, $sfVerbosity);
158 3
    }
159
160
    /**
161
     * {@inheritDoc}
162
     */
163 1
    public function ask($question, $default = null)
164
    {
165
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
166 1
        $helper   = $this->helperSet->get('question');
167 1
        $question = new Question($question, $default);
168
169 1
        return $helper->ask($this->input, $this->getOutputToWriteTo(), $question);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $helper->ask($thi...ToWriteTo(), $question) also could return the type boolean|string[] which is incompatible with the return type mandated by CaptainHook\App\Console\IO::ask() of string.
Loading history...
170
    }
171
172
    /**
173
     * {@inheritDoc}
174
     */
175 1
    public function askConfirmation($question, $default = true)
176
    {
177
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
178 1
        $helper   = $this->helperSet->get('question');
179 1
        $question = new ConfirmationQuestion($question, $default);
180
181 1
        return IOUtil::answerToBool($helper->ask($this->input, $this->getOutputToWriteTo(), $question));
182
    }
183
184
    /**
185
     * {@inheritDoc}
186
     */
187 1
    public function askAndValidate($question, $validator, $attempts = null, $default = null)
188
    {
189
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
190 1
        $helper   = $this->helperSet->get('question');
191 1
        $question = new Question($question, $default);
192 1
        $question->setValidator($validator);
193 1
        $question->setMaxAttempts($attempts);
194
195 1
        return $helper->ask($this->input, $this->getOutputToWriteTo(), $question);
196
    }
197
198
    /**
199
     * Return the output to write to
200
     *
201
     * @param  bool $stdErr
202
     * @return \Symfony\Component\Console\Output\OutputInterface
203
     */
204 6
    private function getOutputToWriteTo($stdErr = false)
205
    {
206 6
        if ($stdErr && $this->output instanceof ConsoleOutputInterface) {
207 1
            return $this->output->getErrorOutput();
208
        }
209
210 5
        return $this->output;
211
    }
212
}
213