Passed
Push — master ( db1ea6...f2ea2d )
by Théo
03:06
created

Printer::printvln()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
namespace _HumbugBox5addf3ce683e7\KevinGH\RequirementChecker;
13
14
/**
15
 * The code in this file must be PHP 5.3+ compatible as is used to know if the application can be run.
16
 *
17
 * @private
18
 */
19
final class Printer
20
{
21
    private $styles = array('reset' => "\33[0m", 'red' => "\33[31m", 'green' => "\33[32m", 'yellow' => "\33[33m", 'title' => "\33[33m", 'error' => "\33[37;41m", 'success' => "\33[30;42m");
22
    private $verbosity;
23
    private $supportColors;
24
    private $width;
25
    /**
26
     * @param int      $verbosity
27
     * @param bool     $supportColors
28
     * @param null|int $width
29
     */
30
    public function __construct($verbosity, $supportColors, $width = null)
31
    {
32
        if (null === $width) {
33
            $terminal = new \_HumbugBox5addf3ce683e7\KevinGH\RequirementChecker\Terminal();
34
            $width = \min($terminal->getWidth(), 80);
35
        }
36
        $this->verbosity = $verbosity;
37
        $this->supportColors = $supportColors;
38
        $this->width = $width;
39
    }
40
    /**
41
     * @return int
42
     */
43
    public function getVerbosity()
44
    {
45
        return $this->verbosity;
46
    }
47
    /**
48
     * @param int $verbosity
49
     */
50
    public function setVerbosity($verbosity)
51
    {
52
        $this->verbosity = $verbosity;
53
    }
54
    /**
55
     * @param string      $title
56
     * @param int         $verbosity
57
     * @param null|string $style
58
     */
59
    public function title($title, $verbosity, $style = null)
60
    {
61
        if (null === $style) {
62
            $style = 'title';
63
        }
64
        $this->printvln('', $verbosity, $style);
65
        $this->printvln($title, $verbosity, $style);
66
        $this->printvln(\str_repeat('=', \min(\strlen($title), $this->width)), $verbosity, $style);
67
        $this->printvln('', $verbosity, $style);
68
    }
69
    /**
70
     * @param Requirement $requirement
71
     *
72
     * @return null|string
73
     */
74
    public function getRequirementErrorMessage(\_HumbugBox5addf3ce683e7\KevinGH\RequirementChecker\Requirement $requirement)
75
    {
76
        if ($requirement->isFulfilled()) {
77
            return null;
78
        }
79
        $errorMessage = \wordwrap($requirement->getTestMessage(), $this->width - 3, \PHP_EOL . '   ') . \PHP_EOL;
80
        return $errorMessage;
81
    }
82
    /**
83
     * @param string      $title
84
     * @param string      $message
85
     * @param int         $verbosity
86
     * @param null|string $style
87
     */
88
    public function block($title, $message, $verbosity, $style = null)
89
    {
90
        $prefix = ' [' . $title . '] ';
91
        $message = $prefix . \trim($message);
92
        $lines = array();
93
        $remainingMessage = $message;
94
        while (\strlen($remainingMessage) > 0) {
95
            $wrapped = \wordwrap($remainingMessage, $this->width - 3, '¬');
96
            $exploded = \explode('¬', $wrapped);
97
            $line = $exploded[0];
98
            $remainingMessage = \ltrim(\substr($remainingMessage, \strlen($line)));
99
            if (\strlen($remainingMessage) > 0) {
100
                $remainingMessage = \str_repeat(' ', \strlen($prefix)) . $remainingMessage;
101
            }
102
            $lines[] = \str_pad($line, $this->width, ' ', \STR_PAD_RIGHT);
103
        }
104
        $this->printvln('', $verbosity);
105
        $this->printvln(\str_repeat(' ', $this->width), $verbosity, $style);
106
        foreach ($lines as $line) {
107
            $this->printvln($line, $verbosity, $style);
108
        }
109
        $this->printv(\str_repeat(' ', $this->width), $verbosity, $style);
110
        $this->printvln('', $verbosity);
111
    }
112
    /**
113
     * @param string      $message
114
     * @param int         $verbosity
115
     * @param null|string $style
116
     */
117
    public function printvln($message, $verbosity, $style = null)
118
    {
119
        $this->printv($message, $verbosity, $style);
120
        $this->printv(\PHP_EOL, $verbosity, null);
121
    }
122
    /**
123
     * @param string      $message
124
     * @param int         $verbosity
125
     * @param null|string $style
126
     */
127
    public function printv($message, $verbosity, $style = null)
128
    {
129
        if ($verbosity > $this->verbosity) {
130
            return;
131
        }
132
        $message = \wordwrap($message, $this->width);
133
        $message = \sprintf('%s%s%s', $this->supportColors && isset($this->styles[$style]) ? $this->styles[$style] : '', $message, $this->supportColors ? $this->styles['reset'] : '');
134
        echo $message;
135
    }
136
}
137