Completed
Pull Request — master (#116)
by Théo
02:23
created

Printer::printv()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
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 $with;
36
37
    /**
38
     * @param int $verbosity
39
     * @param bool $supportColors
40
     * @param int|null $with
41
     */
42
    public function __construct($verbosity, $supportColors, $with = null)
43
    {
44
        if (null === $with) {
45
            $terminal = new Terminal();
46
            $with = $terminal->getWidth();
47
        }
48
49
        $this->verbosity = $verbosity;
50
        $this->supportColors = $supportColors;
51
        $this->with = $with;
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(str_repeat('=', strlen($title)), $verbosity, $style);
76
        $this->printvln('', $verbosity, $style);
77
    }
78
79
    /**
80
     * @param Requirement $requirement
81
     *
82
     * @return null|string
83
     */
84
    public function getRequirementErrorMessage(Requirement $requirement)
85
    {
86
        if ($requirement->isFulfilled()) {
87
            return null;
88
        }
89
90
        $errorMessage = wordwrap($requirement->getTestMessage(), $this->with - 3, PHP_EOL.'   ').PHP_EOL;
91
92
        return $errorMessage;
93
    }
94
95
    /**
96
     * @param string $title
97
     * @param string $message
98
     * @param int   $verbosity
99
     * @param string|null $style
100
     */
101
    public function block($title, $message, $verbosity, $style = null)
102
    {
103
        $message = str_pad(
104
            ' ['.$title.'] '.trim($message).' ', $this->with,
105
            ' ',
106
            STR_PAD_RIGHT
107
        );
108
109
        $this->printvln('', $verbosity);
110
        $this->printvln(str_repeat(' ', $this->with), $verbosity, $style);
111
        $this->printvln($message, $verbosity, $style);
112
        $this->printv(str_repeat(' ', $this->with), $verbosity, $style);
113
        $this->printvln('', $verbosity);
114
    }
115
116
    /**
117
     * @param string $message
118
     * @param int   $verbosity
119
     * @param string|null $style
120
     */
121
    public function printvln($message, $verbosity, $style = null)
122
    {
123
        $this->printv($message, $verbosity, $style);
124
        $this->printv(PHP_EOL, $verbosity, $style);
125
    }
126
127
    /**
128
     * @param string $message
129
     * @param int   $verbosity
130
     * @param string|null $style
131
     */
132
    public function printv($message, $verbosity, $style = null)
133
    {
134
        if ($verbosity > $this->verbosity) {
135
            return;
136
        }
137
138
        $message = sprintf(
139
            '%s%s%s',
140
            $this->supportColors && isset($this->styles[$style]) ? $this->styles[$style] : '',
141
            $message,
142
            $this->supportColors ? $this->styles['reset'] : ''
143
        );
144
145
        echo $message;
146
    }
147
148
}
149