Completed
Push — master ( 96549e...2e805c )
by Tibor
01:19
created

IndentedOutput::addIndentationToMessages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace Bex\Behat\ScreenshotExtension\Output;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
8
/**
9
 * Class IndentedOutput
10
 *
11
 * @package Bex\Behat\ScreenshotExtension\Output
12
 *
13
 * @author Geza Buza <[email protected]>
14
 */
15
class IndentedOutput implements OutputInterface
16
{
17
    const INDENT_WIDTH = 2;
18
19
    const DEFAULT_INDENT_LEVEL = 3;
20
    const DEFAULT_INDENT_CHARACTER = ' ';
21
22
    /**
23
     * @var string String being appended to the message
24
     */
25
    private $indentation;
26
27
    /**
28
     * @var OutputInterface
29
     */
30
    private $decoratedOutput;
31
32
    /**
33
     * IndentedOutput constructor.
34
     *
35
     * @param OutputInterface $decoratedOutput
36
     * @param int $indentLevel
37
     * @param string $indentCharacter
38
     */
39
    public function __construct(
40
        OutputInterface $decoratedOutput,
41
        $indentLevel = self::DEFAULT_INDENT_LEVEL,
42
        $indentCharacter = self::DEFAULT_INDENT_CHARACTER
43
    ) {
44
        $this->indentation = str_repeat($indentCharacter, $indentLevel * self::INDENT_WIDTH);
45
        $this->decoratedOutput = $decoratedOutput;
46
    }
47
48
    /**
49
     * Writes a message to the output.
50
     *
51
     * @param string|array $messages The message as an array of lines or a single string
52
     * @param bool $newline Whether to add a newline
53
     * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
54
     */
55
    public function write($messages, $newline = false, $options = 0)
56
    {
57
        $messages = $this->addIndentationToMessages($messages);
58
59
        $this->decoratedOutput->write($messages, $newline, $options);
60
    }
61
62
    /**
63
     * Writes a message to the output and adds a newline at the end.
64
     *
65
     * @param string|array $messages The message as an array of lines of a single string
66
     * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
67
     */
68
    public function writeln($messages, $options = 0)
69
    {
70
        $messages = $this->addIndentationToMessages($messages);
71
72
        $this->decoratedOutput->writeln($messages, $options);
73
    }
74
75
    /**
76
     * Sets the verbosity of the output.
77
     *
78
     * @param int $level The level of verbosity (one of the VERBOSITY constants)
79
     */
80
    public function setVerbosity($level)
81
    {
82
        $this->decoratedOutput->setVerbosity($level);
83
    }
84
85
    /**
86
     * Gets the current verbosity of the output.
87
     *
88
     * @return int The current level of verbosity (one of the VERBOSITY constants)
89
     */
90
    public function getVerbosity()
91
    {
92
        return $this->decoratedOutput->getVerbosity();
93
    }
94
95
    /**
96
     * Returns whether verbosity is quiet (-q).
97
     *
98
     * @return bool true if verbosity is set to VERBOSITY_QUIET, false otherwise
99
     */
100
    public function isQuiet()
101
    {
102
        return $this->decoratedOutput->isQuiet();
103
    }
104
105
    /**
106
     * Returns whether verbosity is verbose (-v).
107
     *
108
     * @return bool true if verbosity is set to VERBOSITY_VERBOSE, false otherwise
109
     */
110
    public function isVerbose()
111
    {
112
        return $this->decoratedOutput->isVerbose();
113
    }
114
115
    /**
116
     * Returns whether verbosity is very verbose (-vv).
117
     *
118
     * @return bool true if verbosity is set to VERBOSITY_VERY_VERBOSE, false otherwise
119
     */
120
    public function isVeryVerbose()
121
    {
122
        return $this->decoratedOutput->isVeryVerbose();
123
    }
124
125
    /**
126
     * Returns whether verbosity is debug (-vvv).
127
     *
128
     * @return bool true if verbosity is set to VERBOSITY_DEBUG, false otherwise
129
     */
130
    public function isDebug()
131
    {
132
        return $this->decoratedOutput->isDebug();
133
    }
134
135
    /**
136
     * Sets the decorated flag.
137
     *
138
     * @param bool $decorated Whether to decorate the messages
139
     */
140
    public function setDecorated($decorated)
141
    {
142
        $this->decoratedOutput->setDecorated($decorated);
143
    }
144
145
    /**
146
     * Gets the decorated flag.
147
     *
148
     * @return bool true if the output will decorate messages, false otherwise
149
     */
150
    public function isDecorated()
151
    {
152
        return $this->decoratedOutput->isDecorated();
153
    }
154
155
    /**
156
     * Sets output formatter.
157
     *
158
     * @param \Symfony\Component\Console\Formatter\OutputFormatterInterface $formatter
159
     */
160
    public function setFormatter(\Symfony\Component\Console\Formatter\OutputFormatterInterface $formatter)
161
    {
162
        $this->decoratedOutput->setFormatter($formatter);
163
    }
164
165
    /**
166
     * Returns current output formatter instance.
167
     *
168
     * @return \Symfony\Component\Console\Formatter\OutputFormatterInterface
169
     */
170
    public function getFormatter()
171
    {
172
        return $this->decoratedOutput->getFormatter();
173
    }
174
175
    /**
176
     * Prepend message with padding
177
     * 
178
     * @param $messages
179
     *
180
     * @return array|string
181
     */
182
    private function addIndentationToMessages($messages)
183
    {
184
        if (is_array($messages)) {
185
            array_walk(
186
                $messages,
187
                function ($message) {
188
                    return $this->indentation . $message;
189
                }
190
            );
191
            return $messages;
192
        } else {
193
            $messages = $this->indentation . $messages;
194
            return $messages;
195
        }
196
    }
197
}