Passed
Pull Request — dev (#2303)
by Janko
05:56
created

StatusBar::render()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 18
ccs 12
cts 13
cp 0.9231
crap 5.0113
rs 9.5555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Template;
6
7
class StatusBar implements StatusBarInterface
8
{
9
    private StatusBarColorEnum $color = StatusBarColorEnum::EMPTY;
10
11
    private string $label = '';
12
13
    private int $maxValue = 100;
14
15
    private int $value = 0;
16
17
    private float $sizeModifier = 1;
18
19 23
    #[\Override]
20
    public function setColor(StatusBarColorEnum $color): StatusBarInterface
21
    {
22 23
        $this->color = $color;
23 23
        return $this;
24
    }
25
26 23
    #[\Override]
27
    public function setLabel(string $label): StatusBarInterface
28
    {
29 23
        $this->label = $label;
30 23
        return $this;
31
    }
32
33 23
    #[\Override]
34
    public function setMaxValue(int $maxValue): StatusBarInterface
35
    {
36 23
        $this->maxValue = $maxValue;
37 23
        return $this;
38
    }
39
40 23
    #[\Override]
41
    public function setValue(int $value): StatusBarInterface
42
    {
43 23
        $this->value = $value;
44 23
        return $this;
45
    }
46
47 3
    #[\Override]
48
    public function setSizeModifier(float $modifier): StatusBarInterface
49
    {
50 3
        $this->sizeModifier = $modifier;
51 3
        return $this;
52
    }
53
54
    public function __toString(): string
55
    {
56
        return $this->render();
57
    }
58
59 23
    #[\Override]
60
    public function render(): string
61
    {
62 23
        if ($this->maxValue === 0 && $this->value === 0) {
63 1
            return $this->getStatusBar(StatusBarColorEnum::GREY, 50);
64
        }
65
66 23
        $pro = $this->maxValue === 0
67
            ? 100
68 23
            : max(0, @round((100 / $this->maxValue) * min($this->value, $this->maxValue)));
69 23
        $bar = $this->getStatusBar(
70 23
            $this->color,
71 23
            ceil($pro / 2)
72 23
        );
73 23
        if ($pro < 100) {
74 21
            $bar .= $this->getStatusBar(StatusBarColorEnum::GREY, floor((100 - $pro) / 2));
75
        }
76 23
        return $bar;
77
    }
78
79 23
    private function getStatusBar(StatusBarColorEnum $color, float $amount): string
80
    {
81 23
        return sprintf(
82 23
            '<img
83
                alt="%s"
84
                src="/assets/bars/balken.png"
85
                style="background-color: #%s;height: 12px; width:%dpx;"
86
                title="%s: %d/%d"
87 23
             />',
88 23
            $this->label,
89 23
            $color->value,
90 23
            (int) (round($amount) * $this->sizeModifier),
91 23
            $this->label,
92 23
            $this->value,
93 23
            $this->maxValue
94 23
        );
95
    }
96
}
97