Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

DefaultIO   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
eloc 47
c 0
b 0
f 0
dl 0
loc 206
ccs 48
cts 48
cp 1
rs 10

15 Methods

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