TWrite   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 55
c 1
b 0
f 0
dl 0
loc 157
ccs 88
cts 88
cp 1
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A sendFailExplain() 0 4 1
A write() 0 10 3
A sendSkipped() 0 3 1
A terminateWithError() 0 4 1
A resetWorking() 0 5 2
A writeHeadlineLn() 0 4 1
A sendSuccessMessage() 0 7 1
A writeLn() 0 3 1
A sendOk() 0 3 1
A sendCustom() 0 3 1
A sendFail() 0 3 1
A writeFooter() 0 6 1
A sendWarning() 0 3 1
A sendWorking() 0 10 2
A sendErrorMessage() 0 7 1
A removeLastOutput() 0 7 2
A writePadded() 0 3 1
A writePaddedLn() 0 3 1
A writeHeader() 0 5 1
1
<?php
2
3
namespace kalanis\kw_clipr\Output;
4
5
6
use kalanis\kw_clipr\CliprException;
7
use kalanis\kw_clipr\Interfaces;
8
9
10
/**
11
 * Trait TWrite
12
 * @package kalanis\kw_clipr\Output
13
 * @property bool $quiet
14
 */
15
trait TWrite
16
{
17
    /** @var string[] */
18
    protected array $progressIndicator = ['|', '/', '-', '\\'];
19
    protected bool $workingSent = false;
20
    protected int $lastOutputLength = 0;
21
22 1
    public function sendOk(): void
23
    {
24 1
        $this->write(' .... [ <green>OK</green> ]');
25 1
    }
26
27 1
    public function sendSkipped(): void
28
    {
29 1
        $this->write(' .... [ <yellow>SKIPPED</yellow> ]');
30 1
    }
31
32 1
    public function sendWarning(): void
33
    {
34 1
        $this->write(' .... [ <yellow>WARNING</yellow> ]');
35 1
    }
36
37 2
    public function sendFail(): void
38
    {
39 2
        $this->write(' .... [ <red>FAIL</red> ]');
40 2
    }
41
42 1
    public function sendCustom(string $message): void
43
    {
44 1
        $this->write(" .... [ $message ]");
45 1
    }
46
47 1
    public function sendFailExplain(string $message): void
48
    {
49 1
        $this->sendFail();
50 1
        $this->write(' ' . $message);
51 1
    }
52
53 4
    public function sendErrorMessage(string $message): void
54
    {
55 4
        $this->writeLn();
56 4
        $this->writeLn(' <redbg>  ' . str_repeat(' ', mb_strlen($message)) . '  </redbg>');
57 4
        $this->writeLn(' <redbg>  ' . $message . '  </redbg>');
58 4
        $this->writeLn(' <redbg>  ' . str_repeat(' ', mb_strlen($message)) . '  </redbg>');
59 4
        $this->writeLn();
60 4
    }
61
62 1
    public function sendSuccessMessage(string $message): void
63
    {
64 1
        $this->writeLn();
65 1
        $this->writeLn(' <greenbg>  ' . str_repeat(' ', mb_strlen($message)) . '  </greenbg>');
66 1
        $this->writeLn(' <greenbg>  ' . $message . '  </greenbg>');
67 1
        $this->writeLn(' <greenbg>  ' . str_repeat(' ', mb_strlen($message)) . '  </greenbg>');
68 1
        $this->writeLn();
69 1
    }
70
71
    /**
72
     * @param string $message
73
     * @param int $exitCode
74
     * @throws CliprException
75
     */
76 1
    public function terminateWithError(string $message, int $exitCode = Interfaces\IStatuses::STATUS_ERROR): void
77
    {
78 1
        $this->sendErrorMessage($message);
79 1
        throw new CliprException($message, $exitCode);
80
    }
81
82 1
    public function writeHeadlineLn(string $output, string $colour = 'green'): void
83
    {
84 1
        $this->writeLn("<$colour>$output</$colour>");
85 1
        $this->writeLn("<$colour>" . str_repeat('-', strlen($output)) . "</$colour>");
86 1
    }
87
88 1
    public function sendWorking(): void
89
    {
90 1
        $this->workingSent = true;
91 1
        $op = next($this->progressIndicator);
92 1
        if (false === $op) {
93 1
            reset($this->progressIndicator);
94 1
            $op = current($this->progressIndicator);
95
        }
96 1
        $this->write($this->getTranslator()->getStepsBack());
97 1
        $this->write(strval($op));
98 1
    }
99
100 1
    public function resetWorking(): void
101
    {
102 1
        if ($this->workingSent) {
103 1
            $this->write($this->getTranslator()->getStepsBack());
104 1
            $this->workingSent = false;
105
        }
106 1
    }
107
108 27
    public function write(string $output = ''): void
109
    {
110 27
        if (isset($this->quiet) && (true === $this->quiet)) {
111 14
            return;
112
        }
113
114 13
        $output = $this->getTranslator()->translate($output);
115
116 13
        $this->lastOutputLength = strlen($output);
117 13
        echo $output;
118 13
    }
119
120 18
    public function writeLn(string $output = ''): void
121
    {
122 18
        $this->write($output . $this->getTranslator()->getEol());
123 18
    }
124
125 1
    public function writePadded(string $output = '', int $paddingLength = 0): void
126
    {
127 1
        $this->write(str_pad($output, $paddingLength));
128 1
    }
129
130 1
    public function writePaddedLn(string $output = '', int $paddingLength = 0): void
131
    {
132 1
        $this->writeLn(str_pad($output, $paddingLength));
133 1
    }
134
135
    /**
136
     * @codeCoverageIgnore because it contains timestamp and most of it is already covered
137
     */
138 1
    public function writeHeader(): void
139
    {
140 1
        $this->write('<green>' . str_repeat('*', 20) . '</green>');
141 1
        $this->write('<green> ' . get_called_class() . ' - Start Time ' . date('Y-m-d H:i:s') . ' </green>');
142 1
        $this->writeLn('<green>' . str_repeat('*', 20) . '</green>');
143 1
    }
144
145
    /**
146
     * @codeCoverageIgnore because it contains timestamp and most of it is already covered
147
     */
148 1
    public function writeFooter(): void
149
    {
150 1
        $this->write('<green>' . str_repeat('*', 40) . '</green>');
151 1
        $this->write('<green> End Time ' . date('Y-m-d H:i:s') . ' </green>');
152 1
        $this->writeLn('<green>' . str_repeat('*', 40) . '</green>');
153 1
        $this->writeLn();
154 1
    }
155
156 1
    public function removeLastOutput(): void
157
    {
158 1
        if (0 < $this->lastOutputLength) {
159 1
            $this->write(
160 1
                $this->getTranslator()->getStepsBack($this->lastOutputLength)
161 1
                . str_repeat(' ', $this->lastOutputLength)
162 1
                . $this->getTranslator()->getStepsBack($this->lastOutputLength)
163
            );
164
        }
165 1
    }
166
167
    /**
168
     * Get translator for preset platform
169
     * @return AOutput
170
     */
171
    abstract protected function getTranslator(): AOutput;
172
}
173