Passed
Push — master ( f50aa1...d726cb )
by Nico
57:47 queued 28:23
created

TemplateHelper::getPlanetFieldTitle()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 30
nc 10
nop 1
dl 0
loc 50
ccs 0
cts 34
cp 0
crap 90
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Template;
6
7
use Stu\Module\Colony\Lib\PlanetFieldTypeRetrieverInterface;
8
use Stu\Orm\Entity\ColonyInterface;
9
use Stu\Orm\Entity\PlanetFieldInterface;
10
use Stu\Orm\Repository\ColonyTerraformingRepositoryInterface;
11
12
final class TemplateHelper implements TemplateHelperInterface
13
{
14 4
    public function __construct(
15
        private PlanetFieldTypeRetrieverInterface $planetFieldTypeRetriever,
16
        private ColonyTerraformingRepositoryInterface $colonyTerraformingRepository
17
    ) {
18 4
    }
19
20
    public function formatProductionValue(int $value): string
21
    {
22
        if ($value > 0) {
23
            return sprintf('<span class="positive">+%d</span>', $value);
24
        } elseif ($value < 0) {
25
            return sprintf('<span class="negative">%d</span>', $value);
26
        }
27
        return (string) $value;
28
    }
29
30
    public function addPlusCharacter(string $value): string
31
    {
32
        if ($value <= 0) {
33
            return $value;
34
        }
35
        return sprintf('+%d', $value);
36
    }
37
38
    public function jsquote(string $str): string
39
    {
40
        return str_replace(
41
            [
42
                "\\",
43
                "'",
44
            ],
45
            [
46
                "\\\\",
47
                "\\'",
48
            ],
49
            $str
50
        );
51
    }
52
53
    public function formatSeconds(string $time): string
54
    {
55
        $time = (int) $time;
56
        $h = floor($time / 3600);
57
        $time -= $h * 3600;
58
        $m = floor($time / 60);
59
        $time -= $m * 60;
60
61
        $ret = '';
62
        if ($h > 0) {
63
            $ret .= $h . 'h';
64
        }
65
        if ($m > 0) {
66
            $ret .= ' ' . $m . 'm';
67
        }
68
        if ($time > 0) {
69
            $ret .= ' ' . $time . 's';
70
        }
71
        return $ret;
72
    }
73
74
    public function getNumberWithThousandSeperator(int $number): string
75
    {
76
        return number_format((float) $number, 0, '', '.');
77
    }
78
79
    public function getPlanetFieldTypeDescription(
80
        int $fieldTypeId
81
    ): string {
82
        return $this->planetFieldTypeRetriever->getDescription($fieldTypeId);
83
    }
84
85
    public function getPlanetFieldTitle(
86
        PlanetFieldInterface $planetField
87
    ): string {
88
        $fieldTypeName = self::getPlanetFieldTypeDescription($planetField->getFieldType());
0 ignored issues
show
Bug Best Practice introduced by
The method Stu\Module\Template\Temp...tFieldTypeDescription() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        /** @scrutinizer ignore-call */ 
89
        $fieldTypeName = self::getPlanetFieldTypeDescription($planetField->getFieldType());
Loading history...
89
90
        $building = $planetField->getBuilding();
91
92
        if ($building === null) {
93
            $terraFormingState = null;
94
            $host = $planetField->getHost();
95
            if ($host instanceof ColonyInterface) {
96
                $terraFormingState = $this->colonyTerraformingRepository->getByColonyAndField(
97
                    $host->getId(),
98
                    $planetField->getId()
99
                );
100
            }
101
            if ($terraFormingState !== null) {
102
                return sprintf(
103
                    '%s läuft bis %s',
104
                    $terraFormingState->getTerraforming()->getDescription(),
105
                    date('d.m.Y H:i', $terraFormingState->getFinishDate())
106
                );
107
            }
108
            return $fieldTypeName;
109
        }
110
111
        if ($planetField->isUnderConstruction()) {
112
            return sprintf(
113
                'In Bau: %s auf %s - Fertigstellung: %s',
114
                $building->getName(),
115
                $fieldTypeName,
116
                date('d.m.Y H:i', $planetField->getBuildtime())
117
            );
118
        }
119
        if (!$planetField->isActivateable()) {
120
            return $building->getName() . " auf " . $fieldTypeName;
121
        }
122
123
        if ($planetField->isActive()) {
124
            if ($planetField->isDamaged()) {
125
                return $building->getName() . " (aktiviert, beschädigt) auf " . $fieldTypeName;
126
            }
127
            return $building->getName() . " (aktiviert) auf " . $fieldTypeName;
128
        }
129
130
        if ($planetField->hasHighDamage()) {
131
            return $building->getName() . " (stark beschädigt) auf " . $fieldTypeName;
132
        }
133
134
        return $building->getName() . " (deaktiviert) auf " . $fieldTypeName;
135
    }
136
}
137