Completed
Push — master ( b88171...5ac9a0 )
by Sebastian
02:24
created

DefaultIO::doWrite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 4
crap 2
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 SebastianFeldmann\CaptainHook\Console\IO;
11
12
use SebastianFeldmann\CaptainHook\Console\IOUtil;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Helper\HelperSet;
15
use Symfony\Component\Console\Output\ConsoleOutputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18
use Symfony\Component\Console\Question\Question;
19
20
/**
21
 * Class DefaultIO
22
 *
23
 * @package CaptainHook
24
 * @author  Sebastian Feldmann <[email protected]>
25
 * @link    https://github.com/sebastianfeldmann/captainhook
26
 * @since   Class available since Release 0.9.0
27
 */
28
class DefaultIO extends Base
29
{
30
    /**
31
     * @var \Symfony\Component\Console\Input\InputInterface
32
     */
33
    protected $input;
34
35
    /**
36
     * @var \Symfony\Component\Console\Output\OutputInterface
37
     */
38
    protected $output;
39
40
    /**
41
     * @var \Symfony\Component\Console\Helper\HelperSet
42
     */
43
    protected $helperSet;
44
45
    /**
46
     * @var array<int, int>
47
     */
48
    private $verbosityMap;
49
50
    /**
51
     * Constructor.
52
     *
53
     * @param \Symfony\Component\Console\Input\InputInterface   $input
54
     * @param \Symfony\Component\Console\Output\OutputInterface $output
55
     * @param \Symfony\Component\Console\Helper\HelperSet       $helperSet
56
     */
57 11
    public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet = null)
58
    {
59 11
        $this->input        = $input;
60 11
        $this->output       = $output;
61 11
        $this->helperSet    = $helperSet;
62 11
        $this->verbosityMap = [
63 11
            self::QUIET        => OutputInterface::VERBOSITY_QUIET,
64 11
            self::NORMAL       => OutputInterface::VERBOSITY_NORMAL,
65 11
            self::VERBOSE      => OutputInterface::VERBOSITY_VERBOSE,
66 11
            self::VERY_VERBOSE => OutputInterface::VERBOSITY_VERY_VERBOSE,
67 11
            self::DEBUG        => OutputInterface::VERBOSITY_DEBUG,
68
        ];
69 11
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 1
    public function isInteractive()
75
    {
76 1
        return $this->input->isInteractive();
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 1
    public function isVerbose()
83
    {
84 1
        return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 1
    public function isVeryVerbose()
91
    {
92 1
        return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98 1
    public function isDebug()
99
    {
100 1
        return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 1
    public function write($messages, $newline = true, $verbosity = self::NORMAL)
107
    {
108 1
        $this->doWrite($messages, $newline, false, $verbosity);
109 1
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114 2
    public function writeError($messages, $newline = true, $verbosity = self::NORMAL)
115
    {
116 2
        $this->doWrite($messages, $newline, true, $verbosity);
117 2
    }
118
119
    /**
120
     * Write to the appropriate user output.
121
     *
122
     * @param array|string $messages
123
     * @param bool         $newline
124
     * @param bool         $stderr
125
     * @param int          $verbosity
126
     */
127 3
    private function doWrite($messages, $newline, $stderr, $verbosity)
128
    {
129 3
        $sfVerbosity = $this->verbosityMap[$verbosity];
130 3
        if ($sfVerbosity > $this->output->getVerbosity()) {
131 1
            return;
132
        }
133
134 2
        $this->getOutputToWriteTo($stderr)->write($messages, $newline, $sfVerbosity);
135 2
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140 1
    public function ask($question, $default = null)
141
    {
142
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
143 1
        $helper   = $this->helperSet->get('question');
144 1
        $question = new Question($question, $default);
145
146 1
        return $helper->ask($this->input, $this->getOutputToWriteTo(), $question);
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152 1
    public function askConfirmation($question, $default = true)
153
    {
154
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
155 1
        $helper   = $this->helperSet->get('question');
156 1
        $question = new ConfirmationQuestion($question, $default);
157
158 1
        return IOUtil::answerToBool($helper->ask($this->input, $this->getOutputToWriteTo(), $question));
159
    }
160
161
    /**
162
     * {@inheritDoc}
163
     */
164 1
    public function askAndValidate($question, $validator, $attempts = null, $default = null)
165
    {
166
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
167 1
        $helper   = $this->helperSet->get('question');
168 1
        $question = new Question($question, $default);
169 1
        $question->setValidator($validator);
170 1
        $question->setMaxAttempts($attempts);
171
172 1
        return $helper->ask($this->input, $this->getOutputToWriteTo(), $question);
173
    }
174
175
    /**
176
     * Return the output to write to.
177
     *
178
     * @param  bool $stdErr
179
     * @return \Symfony\Component\Console\Output\OutputInterface
180
     */
181 5
    private function getOutputToWriteTo($stdErr = false)
182
    {
183 5
        if ($stdErr && $this->output instanceof ConsoleOutputInterface) {
184 1
            return $this->output->getErrorOutput();
185
        }
186
187 4
        return $this->output;
188
    }
189
}
190