Passed
Pull Request — master (#1695)
by Nico
40:23 queued 15:53
created

EpsBarProvider::setTemplateVariables()   C

Complexity

Conditions 11
Paths 240

Size

Total Lines 57
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 40
nc 240
nop 2
dl 0
loc 57
ccs 0
cts 40
cp 0
crap 132
rs 5.9833
c 1
b 0
f 0

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