EpsBarProvider::getEpsStatusBar()   C
last analyzed

Complexity

Conditions 11
Paths 240

Size

Total Lines 49
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 15.8785

Importance

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