Passed
Pull Request — master (#116)
by Théo
02:21
created

Printer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the box project.
5
 *
6
 * (c) Kevin Herrera <[email protected]>
7
 *     Théo Fidry <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace KevinGH\Box\RequirementChecker;
14
15
use Symfony\Component\Console\Terminal;
16
17
/**
18
 * The code in this file must be PHP 5.3+ compatible as is used to know if the application can be run.
19
 *
20
 * @private
21
 */
22
final class Printer
23
{
24
    private $styles = array(
25
        'reset' => "\033[0m",
26
        'red' => "\033[31m",
27
        'green' => "\033[32m",
28
        'yellow' => "\033[33m",
29
        'title' => "\033[33m",
30
        'error' => "\033[37;41m",
31
        'success' => "\033[30;42m",
32
    );
33
    private $verbosity;
34
    private $supportColors;
35
    private $width;
36
37
    /**
38
     * @param int $verbosity
39
     * @param bool $supportColors
40
     * @param int|null $width
41
     */
42
    public function __construct($verbosity, $supportColors, $width = null)
43
    {
44
        if (null === $width) {
45
            $terminal = new Terminal();
46
            $width = min($terminal->getWidth(), 80);
47
        }
48
49
        $this->verbosity = $verbosity;
50
        $this->supportColors = $supportColors;
51
        $this->width = $width;
52
    }
53
54
    /**
55
     * @return int
56
     */
57
    public function getVerbosity()
58
    {
59
        return $this->verbosity;
60
    }
61
62
    /**
63
     * @param string $title
64
     * @param int $verbosity
65
     * @param string|null $style
66
     */
67
    public function title($title, $verbosity, $style = null)
68
    {
69
        if (null === $style) {
70
            $style = 'title';
71
        }
72
73
        $this->printvln('', $verbosity, $style);
74
        $this->printvln($title, $verbosity, $style);
75
        $this->printvln(
76
            str_repeat(
77
                '=',
78
                min(strlen($title), $this->width)
79
            ),
80
            $verbosity,
81
            $style
82
        );
83
        $this->printvln('', $verbosity, $style);
84
    }
85
86
    /**
87
     * @param Requirement $requirement
88
     *
89
     * @return null|string
90
     */
91
    public function getRequirementErrorMessage(Requirement $requirement)
92
    {
93
        if ($requirement->isFulfilled()) {
94
            return null;
95
        }
96
97
        $errorMessage = wordwrap($requirement->getTestMessage(), $this->width - 3, PHP_EOL.'   ').PHP_EOL;
98
99
        return $errorMessage;
100
    }
101
102
    /**
103
     * @param string $title
104
     * @param string $message
105
     * @param int   $verbosity
106
     * @param string|null $style
107
     */
108
    public function block($title, $message, $verbosity, $style = null)
109
    {
110
        $prefix = ' [' . $title . '] ';
111
        $message = $prefix .trim($message);
112
113
        $lines = [];
114
115
        $remainingMessage = $message;
116
117
        while (strlen($remainingMessage) > 0) {
118
            $wrapped = wordwrap($remainingMessage, $this->width - 3, '¬');
119
            $exploded = explode('¬', $wrapped);
120
            $line = $exploded[0];
121
            $remainingMessage = ltrim(substr($remainingMessage, strlen($line)));
122
123
            if (strlen($remainingMessage) > 0) {
124
                $remainingMessage = str_repeat(' ', strlen($prefix)).$remainingMessage;
125
            }
126
127
            $lines[] = str_pad($line, $this->width, ' ', STR_PAD_RIGHT);
128
        }
129
130
        $this->printvln('', $verbosity);
131
        $this->printvln(str_repeat(' ', $this->width), $verbosity, $style);
132
        foreach ($lines as $line) {
133
            $this->printvln($line, $verbosity, $style);
134
        }
135
        $this->printv(str_repeat(' ', $this->width), $verbosity, $style);
136
        $this->printvln('', $verbosity);
137
    }
138
139
    /**
140
     * @param string $message
141
     * @param int   $verbosity
142
     * @param string|null $style
143
     */
144
    public function printvln($message, $verbosity, $style = null)
145
    {
146
        $this->printv($message, $verbosity, $style);
147
        $this->printv(PHP_EOL, $verbosity, null);
148
    }
149
150
    /**
151
     * @param string $message
152
     * @param int   $verbosity
153
     * @param string|null $style
154
     */
155
    public function printv($message, $verbosity, $style = null)
156
    {
157
        if ($verbosity > $this->verbosity) {
158
            return;
159
        }
160
161
        $message = wordwrap($message, $this->width);
162
163
        $message = sprintf(
164
            '%s%s%s',
165
            $this->supportColors && isset($this->styles[$style]) ? $this->styles[$style] : '',
166
            $message,
167
            $this->supportColors ? $this->styles['reset'] : ''
168
        );
169
170
        echo $message;
171
    }
172
173
}
174