Test Failed
Push — dev ( 83327d...238307 )
by Nico
51:35 queued 19:29
created

EpsBarProvider::getEpsStatusBar()   C

Complexity

Conditions 11
Paths 240

Size

Total Lines 49
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 35
nc 240
nop 3
dl 0
loc 49
ccs 0
cts 35
cp 0
crap 132
rs 5.9833
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
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...
6
use Stu\Lib\Colony\PlanetFieldHostInterface;
7
use Stu\Module\Control\GameControllerInterface;
8
use Stu\Module\Template\StatusBarColorEnum;
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...
9
use Stu\Orm\Entity\ColonyInterface;
10
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
11
12
final class EpsBarProvider implements GuiComponentProviderInterface
13
{
14
    public function __construct(private PlanetFieldRepositoryInterface $planetFieldRepository) {}
15
16
    #[Override]
17
    public function setTemplateVariables(
18
        PlanetFieldHostInterface $host,
19
        GameControllerInterface $game
20
    ): void {
21
        $energyProduction = $this->planetFieldRepository->getEnergyProductionByHost($host);
22
23
        $game->setTemplateVar('EPS_STATUS_BAR', $this->getEpsStatusBar($host, $energyProduction));
24
        $game->setTemplateVar('EPS_PRODUCTION', $energyProduction);
25
        $game->setTemplateVar('EPS_BAR_TITLE_STRING', $this->getEpsBarTitleString($host, $energyProduction));
26
    }
27
28
    public static function getEpsStatusBar(PlanetFieldHostInterface $host, int $energyProduction, int $width = 360): string
29
    {
30
        $currentEps = $host instanceof ColonyInterface ? $host->getEps() : 0;
31
32
        $bars = [];
33
        $epsBar = '';
34
        if ($energyProduction < 0) {
35
            $prod = abs($energyProduction);
36
            if ($currentEps - $prod < 0) {
37
                $bars[StatusBarColorEnum::STATUSBAR_RED] = $currentEps;
38
                $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
39
            } else {
40
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps - $prod;
41
                $bars[StatusBarColorEnum::STATUSBAR_RED] = $prod;
42
                $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
43
            }
44
        }
45
        if ($energyProduction > 0) {
46
            if ($currentEps + $energyProduction > $host->getMaxEps()) {
47
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
48
                if ($currentEps < $host->getMaxEps()) {
49
                    $bars[StatusBarColorEnum::STATUSBAR_GREEN] = $host->getMaxEps() - $currentEps;
50
                }
51
            } else {
52
                $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
53
                $bars[StatusBarColorEnum::STATUSBAR_GREEN] = $energyProduction;
54
                $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps - $energyProduction;
55
            }
56
        }
57
        if ($energyProduction == 0) {
58
            $bars[StatusBarColorEnum::STATUSBAR_YELLOW] = $currentEps;
59
            $bars[StatusBarColorEnum::STATUSBAR_GREY] = $host->getMaxEps() - $currentEps;
60
        }
61
        foreach ($bars as $color => $value) {
62
            if ($host->getMaxEps() < $value) {
63
                $value = $host->getMaxEps();
64
            }
65
            if ($value <= 0) {
66
                continue;
67
            }
68
            $epsBar .= sprintf(
69
                '<img src="/assets/bars/balken.png" style="background-color: #%s;height: 12px; width: %dpx;" title="%s" />',
70
                $color,
71
                round($width / 100 * (100 / $host->getMaxEps() * $value)),
72
                'Energieproduktion'
73
            );
74
        }
75
76
        return $epsBar;
77
    }
78
79
    private function getEpsBarTitleString(PlanetFieldHostInterface $host, int $energyProduction): string
80
    {
81
        if ($host instanceof ColonyInterface) {
82
            $forecast = $host->getEps() + $energyProduction;
83
            if ($host->getEps() + $energyProduction < 0) {
84
                $forecast = 0;
85
            }
86
            if ($host->getEps() + $energyProduction > $host->getMaxEps()) {
87
                $forecast = $host->getMaxEps();
88
            }
89
90
            $eps = $host->getEps();
91
        } else {
92
            $eps = 0;
93
            $forecast = $energyProduction;
94
        }
95
96
        if ($energyProduction > 0) {
97
            $energyProduction = sprintf('+%d', $energyProduction);
98
        }
99
100
        return sprintf(
101
            _('Energie: %d/%d (%s/Runde = %d)'),
102
            $eps,
103
            $host->getMaxEps(),
104
            $energyProduction,
105
            $forecast
106
        );
107
    }
108
}
109