Passed
Push — master ( c7771a...f82578 )
by Radu
02:19
created

ProgressBar::prefixProgress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace WebServCo\Framework;
3
4
//https://gist.github.com/mayconbordin/2860547
5
final class ProgressBar
6
{
7
    protected $type;
8
9
    protected $width;
10
    protected $padding;
11
    protected $total;
12
    protected $item;
13
14
    protected $outBar;
15
    protected $outPad;
16
    protected $outMessage;
17
18
    public function __construct($width = 20)
19
    {
20
        $this->type = 'single_line';
21
        $this->width = $width;
22
        $this->padding = 25;
23
        $this->total = 100;
24
        $this->item = 1;
25
    }
26
27
    public function start($total = 100)
28
    {
29
        $this->total = $total;
30
    }
31
32
    public function advanceTo($item)
33
    {
34
        $this->item = $item;
35
    }
36
37
    public function setType($type)
38
    {
39
        if (in_array($type, ['single_line', 'multi_line'])) {
40
            $this->type = $type;
41
        }
42
    }
43
44
    public function prefix($message = '')
45
    {
46
        switch ($this->type) {
47
            case 'single_line':
48
            case 'multi_line':
49
                return $this->prefixProgress($message);
50
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
51
        }
52
    }
53
54
    public function suffix($result = true)
55
    {
56
        switch ($this->type) {
57
            case 'single_line':
58
                return $this->suffixSingle($result);
59
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
60
            case 'multi_line':
61
                return $this->suffixMulti($result);
62
                break;
63
        }
64
    }
65
66
    protected function prefixProgress($message)
67
    {
68
        $percent = round($this->item * 100 / $this->total);
69
        $bar = (int) round($this->width * $percent / 100);
70
        $this->outBar = sprintf(
71
            "%s%% [%s>%s] %s",
72
            $percent,
73
            str_repeat('=', $bar),
74
            str_repeat(' ', (int) round($this->width-$bar)),
75
            $this->item . '/' . $this->total
76
        );
77
        $this->outMessage = $message;
78
79
        $padLen = ($this->width + $this->padding) - strlen($this->outBar);
80
        $this->outPad = (0 < $padLen) ? str_repeat(' ', (int) $padLen) : null;
81
        return $this->outBar.$this->outPad.$this->outMessage;
82
    }
83
84
    protected function suffixSingle($result, $overwrite = false)
85
    {
86
        $totalLen = strlen($this->outBar.$this->outPad.$this->outMessage);
87
        $output = null;
88
89
        if ($overwrite) {
90
            //overwrite current line
91
            $output .= "\033[" . $totalLen . 'D';
92
            $output .= str_repeat(' ', $this->width + $this->padding);
93
            $output .= $this->outMessage;
94
        }
95
96
        $padLen = 74 - $totalLen;
97
        if (0 < $padLen) {
98
            $output .= str_repeat(' ', $padLen);
99
        }
100
        $output .= '[';
101
        $output .= $result ? "\e[32mOK" : "\e[31mKO";
102
        $output .= "\e[0m" . ']';
103
        $output .= "\r";
104
105
        return $output;
106
    }
107
108
    protected function suffixMulti($result, $overwrite = true)
109
    {
110
        $output = $this->suffixSingle($result, $overwrite);
111
        $output .= PHP_EOL;
112
113
        return $output;
114
    }
115
116
    public function finish()
117
    {
118
        return "\033[" . 0 . 'D' . str_repeat(' ', 74) . "\r";
119
    }
120
}
121