Passed
Push — master ( b9da30...84e51b )
by kozo
01:33
created

Decorator::getWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
namespace Watchmaker\lib;
4
5
class Decorator
6
{
7
    private $color = null;
8
    private $collector = null;
9
10
    public function __construct(StringCollector $collector)
11
    {
12
        $this->color = new Color();
13
        $this->collector = $collector;
14
    }
15
16
    public function simple($text)
17
    {
18
        if ($this->isInteractive() === false) {
19
            return ;
20
        }
21
22
        $this->collector->text($text);
23
    }
24
25
    public function greenText($text)
26
    {
27
        if ($this->isInteractive() === false) {
28
            return ;
29
        }
30
31
        $d = $this->color->apply('color_82', $text);
32
        $this->collector->text($d);
33
    }
34
35
    public function yellowText($text)
36
    {
37
        if ($this->isInteractive() === false) {
38
            return ;
39
        }
40
41
        $d = $this->color->apply('color_226', $text);
42
        $this->collector->text($d);
43
    }
44
45
    public function redText($text)
46
    {
47
        if ($this->isInteractive() === false) {
48
            return ;
49
        }
50
51
        $d = $this->color->apply('color_196', $text);
52
        $this->collector->text($d);
53
    }
54
55
    public function alert($text)
56
    {
57
        if ($this->isInteractive() === false) {
58
            return ;
59
        }
60
61
        $length = mb_strlen($text) + 12;
62
63
        $outText = '';
64
        $outText .= $this->color->apply('color_130', str_repeat('*', $length)) . "\n";
65
        $outText .= $this->color->apply('color_130', '*     ' . $text . '     *') . "\n";
66
        $outText .= $this->color->apply('color_130', str_repeat('*', $length)) . "\n";
67
68
        $this->collector->text($outText);
69
    }
70
71
    public function flashSuccess($message = ' All Installed')
72
    {
73
        if ($this->isInteractive() === false) {
74
            return ;
75
        }
76
77
        $width = $this->getWidth();
78
        $length = mb_strlen($message);
79
80
        $text = '';
81
        $b = $this->color->apply('color_22', str_repeat('-', $width));
82
        $text .= $this->color->apply('bg_color_22', $b) . "\n";
83
        $b = $this->color->apply('color_22', str_repeat('-', $width - $length));
84
        $text .= $this->color->apply('bg_color_22', $message . $b) . "\n";
85
        $b = $this->color->apply('color_22', str_repeat('-', $width));
86
        $text .= $this->color->apply('bg_color_22', $b) . "\n";
87
88
        $this->collector->text($text);
89
    }
90
91
    public function flashError($message = ' Please update crontab.')
92
    {
93
        if ($this->isInteractive() === false) {
94
            return ;
95
        }
96
97
        $width = $this->getWidth();
98
        $length = mb_strlen($message);
99
100
        $text = '';
101
        $b = $this->color->apply('color_1', str_repeat('-', $width));
102
        $text .= $this->color->apply('bg_color_1', $b) . "\n";
103
        $b = $this->color->apply('color_1', str_repeat('-', $width - $length));
104
        $text .= $this->color->apply('bg_color_1', $message . $b) . "\n";
105
        $b = $this->color->apply('color_1', str_repeat('-', $width));
106
        $text .= $this->color->apply('bg_color_1', $b) . "\n";
107
108
        $this->collector->text($text);
109
    }
110
111
    public function hr()
112
    {
113
        if ($this->isInteractive() === false) {
114
            return ;
115
        }
116
117
        $width = $this->getWidth();
118
        $d = str_repeat('-', $width) . "\n";
119
        $this->collector->text($d);
120
    }
121
122
    public function newLine($line = 1)
123
    {
124
        if ($this->isInteractive() === false) {
125
            return ;
126
        }
127
128
        for ($i = 0; $i < $line; $i++) {
129
            $this->collector->text("\n", false);
130
        }
131
    }
132
133
    private function getWidth()
134
    {
135
        return intval(trim(`tput cols` ?? 80));
136
    }
137
138
    private function isInteractive()
139
    {
140
        if (posix_isatty(STDOUT) === false) {
141
            return false;
142
        }
143
144
        return true;
145
    }
146
}
147