Passed
Push — master ( bf5028...3a1f66 )
by Radu
03:00
created

Line::prefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace WebServCo\Framework\Cli\Progress;
3
4
final class Line
5
{
6
    protected $padding;
7
8
    protected $outPad;
9
    protected $outMessage;
10
11
    protected $showResult;
12
13
    public function __construct()
14
    {
15
        $this->showResult = false;
16
        $this->padding = 0;
17
    }
18
19
    public function setShowResult($showResult)
20
    {
21
        $this->showResult = (bool) $showResult;
22
    }
23
24
    public function prefix($message = '')
25
    {
26
        $this->outMessage = $message;
27
28
        $this->outPad = (0 < $this->padding) ? str_repeat(' ', (int) $this->padding) : null;
29
        return $this->outPad.$this->outMessage;
30
    }
31
32
    public function suffix($result = true)
33
    {
34
        $totalLen = strlen($this->outPad.$this->outMessage);
35
        $output = null;
36
37
        //overwrite current line
38
        $output .= "\033[" . $totalLen . 'D';
39
        $output .= str_repeat(' ', $this->padding);
40
        $output .= $this->outMessage;
41
42
        $padLen = 74 - $totalLen;
43
        if (0 < $padLen) {
44
            $output .= str_repeat(' ', $padLen);
45
        }
46
        if ($this->showResult) {
47
            $output .= '[';
48
            $output .= $result ? "\e[32mOK" : "\e[31mKO";
49
            $output .= "\e[0m" . ']';
50
        }
51
52
        $output .= "\r";
53
54
        return $output;
55
    }
56
57
    public function finish()
58
    {
59
        return "\033[" . 0 . 'D' . str_repeat(' ', 74) . "\r";
60
    }
61
}
62