Passed
Push — master ( 5b8585...da319f )
by Petr
07:39
created

TWrite::writePaddedLn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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