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

Decorator   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 65
c 9
b 0
f 0
dl 0
loc 140
rs 10
wmc 23

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A greenText() 0 8 2
A flashSuccess() 0 18 2
A simple() 0 7 2
A redText() 0 8 2
A flashError() 0 18 2
A alert() 0 14 2
A yellowText() 0 8 2
A getWidth() 0 3 1
A isInteractive() 0 7 2
A newLine() 0 8 3
A hr() 0 9 2
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