Passed
Push — master ( a4c5f9...448818 )
by Keoghan
18:30
created

ConsoleWriter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 6.67%

Importance

Changes 0
Metric Value
wmc 13
eloc 30
dl 0
loc 150
ccs 2
cts 30
cp 0.0667
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A warn() 0 9 2
A comment() 0 3 1
A alert() 0 9 1
A question() 0 3 1
A __construct() 0 3 1
A error() 0 3 1
A parseVerbosity() 0 9 3
A line() 0 5 2
A info() 0 3 1
1
<?php
2
3
namespace App\Support\Console;
4
5
use Illuminate\Console\OutputStyle;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
8
use Symfony\Component\Console\Input\StringInput;
9
use Symfony\Component\Console\Output\ConsoleOutput;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class ConsoleWriter
13
{
14
    /** @var \Illuminate\Console\OutputStyle */
15
    protected $output;
16
17
    /**
18
     * The default verbosity of output commands.
19
     *
20
     * @var int
21
     */
22
    protected $verbosity = OutputInterface::VERBOSITY_NORMAL;
23
24
    /* The mapping between human readable verbosity levels and Symfony's OutputInterface.
25
     *
26
     * @var array
27
     */
28
    protected $verbosityMap = [
29
        'v'      => OutputInterface::VERBOSITY_VERBOSE,
30
        'vv'     => OutputInterface::VERBOSITY_VERY_VERBOSE,
31
        'vvv'    => OutputInterface::VERBOSITY_DEBUG,
32
        'quiet'  => OutputInterface::VERBOSITY_QUIET,
33
        'normal' => OutputInterface::VERBOSITY_NORMAL,
34
    ];
35
36 213
    public function __construct()
37
    {
38 213
        $this->output = new OutputStyle(new StringInput(''), new ConsoleOutput());
39
    }
40
41
    /**
42
     * Write a string as information output.
43
     *
44
     * @param string          $string
45
     * @param null|int|string $verbosity
46
     *
47
     * @return void
48
     */
49
    public function info($string, $verbosity = null)
50
    {
51
        $this->line($string, 'info', $verbosity);
52
    }
53
54
    /**
55
     * Write a string as standard output.
56
     *
57
     * @param string          $string
58
     * @param string          $style
59
     * @param null|int|string $verbosity
60
     *
61
     * @return void
62
     */
63
    public function line($string, $style = null, $verbosity = null)
64
    {
65
        $styled = $style ? "<$style>$string</$style>" : $string;
66
67
        $this->output->writeln($styled, $this->parseVerbosity($verbosity));
68
    }
69
70
    /**
71
     * Get the verbosity level in terms of Symfony's OutputInterface level.
72
     *
73
     * @param string|int|null $level
74
     *
75
     * @return int
76
     */
77
    protected function parseVerbosity($level = null)
78
    {
79
        if (isset($this->verbosityMap[$level])) {
80
            $level = $this->verbosityMap[$level];
81
        } elseif (!is_int($level)) {
82
            $level = $this->verbosity;
83
        }
84
85
        return $level;
86
    }
87
88
    /**
89
     * Write a string as comment output.
90
     *
91
     * @param string          $string
92
     * @param null|int|string $verbosity
93
     *
94
     * @return void
95
     */
96
    public function comment($string, $verbosity = null)
97
    {
98
        $this->line($string, 'comment', $verbosity);
99
    }
100
101
    /**
102
     * Write a string as question output.
103
     *
104
     * @param string          $string
105
     * @param null|int|string $verbosity
106
     *
107
     * @return void
108
     */
109
    public function question($string, $verbosity = null)
110
    {
111
        $this->line($string, 'question', $verbosity);
112
    }
113
114
    /**
115
     * Write a string as error output.
116
     *
117
     * @param string          $string
118
     * @param null|int|string $verbosity
119
     *
120
     * @return void
121
     */
122
    public function error($string, $verbosity = null)
123
    {
124
        $this->line($string, 'error', $verbosity);
125
    }
126
127
    /**
128
     * Write a string as warning output.
129
     *
130
     * @param string          $string
131
     * @param null|int|string $verbosity
132
     *
133
     * @return void
134
     */
135
    public function warn($string, $verbosity = null)
136
    {
137
        if (!$this->output->getFormatter()->hasStyle('warning')) {
138
            $style = new OutputFormatterStyle('yellow');
139
140
            $this->output->getFormatter()->setStyle('warning', $style);
141
        }
142
143
        $this->line($string, 'warning', $verbosity);
144
    }
145
146
    /**
147
     * Write a string in an alert box.
148
     *
149
     * @param string $string
150
     *
151
     * @return void
152
     */
153
    public function alert($string)
154
    {
155
        $length = Str::length(strip_tags($string)) + 12;
156
157
        $this->comment(str_repeat('*', $length));
158
        $this->comment('*     '.$string.'     *');
159
        $this->comment(str_repeat('*', $length));
160
161
        $this->output->newLine();
162
    }
163
}
164