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