Passed
Pull Request — master (#1884)
by Janko
50:38 queued 25:12
created

StatusBar::setSizeModifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Template;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class StatusBar implements StatusBarInterface
10
{
11
    private string $color = '';
12
13
    private string $label = '';
14
15
    private int $maxValue = 100;
16
17
    private int $value = 0;
18
19
    private float $sizeModifier = 1;
20
21
    #[Override]
22
    public function setColor(string $color): StatusBarInterface
23
    {
24
        $this->color = $color;
25
        return $this;
26
    }
27
28
    #[Override]
29
    public function setLabel(string $label): StatusBarInterface
30
    {
31
        $this->label = $label;
32
        return $this;
33
    }
34
35
    #[Override]
36
    public function setMaxValue(int $maxValue): StatusBarInterface
37
    {
38
        $this->maxValue = $maxValue;
39
        return $this;
40
    }
41
42
    #[Override]
43
    public function setValue(int $value): StatusBarInterface
44
    {
45
        $this->value = $value;
46
        return $this;
47
    }
48
49
    #[Override]
50
    public function setSizeModifier(float $modifier): StatusBarInterface
51
    {
52
        $this->sizeModifier = $modifier;
53
        return $this;
54
    }
55
56
    #[Override]
57
    public function __toString(): string
58
    {
59
        return $this->render();
60
    }
61
62
    #[Override]
63
    public function render(): string
64
    {
65
        $pro = $this->maxValue === 0 ? 100 : max(0, @round((100 / $this->maxValue) * min($this->value, $this->maxValue)));
66
        $bar = $this->getStatusBar(
67
            $this->color,
68
            ceil($pro / 2)
69
        );
70
        if ($pro < 100) {
71
            $bar .= $this->getStatusBar(StatusBarColorEnum::STATUSBAR_GREY, floor((100 - $pro) / 2));
0 ignored issues
show
Bug introduced by
The type Stu\Module\Template\StatusBarColorEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
        }
73
        return $bar;
74
    }
75
76
    private function getStatusBar(string $color, float $amount): string
77
    {
78
        return sprintf(
79
            '<img
80
                alt="%s"
81
                src="/assets/bars/balken.png"
82
                style="background-color: #%s;height: 12px; width:%dpx;"
83
                title="%s: %d/%d"
84
             />',
85
            $this->label,
86
            $color,
87
            (int) (round($amount) * $this->sizeModifier),
88
            $this->label,
89
            $this->value,
90
            $this->maxValue
91
        );
92
    }
93
}
94