Passed
Push — master ( f544cb...b3a3d9 )
by Nico
36:43 queued 09:10
created

TalHelper   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Test Coverage

Coverage 43.23%

Importance

Changes 0
Metric Value
eloc 123
c 0
b 0
f 0
dl 0
loc 226
ccs 67
cts 155
cp 0.4323
rs 10
wmc 30

12 Methods

Rating   Name   Duplication   Size   Complexity  
B getPlanetFieldTitle() 0 50 9
A register() 0 67 1
A getBBCodeParser() 0 3 1
A formatProductionValue() 0 8 3
A getDic() 0 7 2
A addPlusCharacter() 0 6 2
A getPlanetFieldTypeDescription() 0 4 1
A getNumberWithThousandSeperator() 0 3 1
A jsquote() 0 12 1
A formatSeconds() 0 19 4
A getContactListModeDescription() 0 11 4
A setDic() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Tal;
6
7
use JBBCode\Parser;
8
use PhpTal\Php\TalesInternal;
9
use PhpTal\TalesRegistry;
10
use Psr\Container\ContainerInterface;
11
use Stu\Module\Colony\Lib\PlanetFieldTypeRetrieverInterface;
12
use Stu\Module\Control\StuTime;
13
use Stu\Module\Message\Lib\ContactListModeEnum;
14
use Stu\Module\Tal\Exception\DiContainerNotSetException;
15
use Stu\Orm\Entity\ColonyInterface;
16
use Stu\Orm\Entity\PlanetFieldInterface;
17
use Stu\Orm\Repository\ColonyTerraformingRepositoryInterface;
18
19
final class TalHelper
20
{
21
    private static ?ContainerInterface $dic = null;
22
23 4
    private static function setDic(ContainerInterface $dic): void
24
    {
25 4
        self::$dic = $dic;
26
    }
27
28
    /**
29
     * @throws DiContainerNotSetException
30
     */
31
    private static function getDic(): ContainerInterface
32
    {
33
        if (self::$dic === null) {
34
            throw new DiContainerNotSetException();
35
        }
36
37
        return self::$dic;
38
    }
39
40
    public static function formatProductionValue(int $value): string
41
    {
42
        if ($value > 0) {
43
            return sprintf('<span class="positive">+%d</span>', $value);
44
        } elseif ($value < 0) {
45
            return sprintf('<span class="negative">%d</span>', $value);
46
        }
47
        return (string) $value;
48
    }
49
50
    public static function addPlusCharacter(string $value): string
51
    {
52
        if ($value <= 0) {
53
            return $value;
54
        }
55
        return sprintf('+%d', $value);
56
    }
57
58
    public static function getContactListModeDescription(int $mode): string
59
    {
60
        switch ($mode) {
61
            case ContactListModeEnum::CONTACT_FRIEND:
62
                return _('Freund');
63
            case ContactListModeEnum::CONTACT_ENEMY:
64
                return _('Feind');
65
            case ContactListModeEnum::CONTACT_NEUTRAL:
66
                return _('Neutral');
67
        }
68
        return '';
69
    }
70
71
    public static function getBBCodeParser(): Parser
72
    {
73
        return self::getDic()->get(Parser::class);
74
    }
75
76
    public static function jsquote(string $str): string
77
    {
78
        return str_replace(
79
            [
80
                "\\",
81
                "'",
82
            ],
83
            [
84
                "\\\\",
85
                "\\'",
86
            ],
87
            $str
88
        );
89
    }
90
91
    public static function formatSeconds(string $time): string
92
    {
93
        $time = (int) $time;
94
        $h = floor($time / 3600);
95
        $time -= $h * 3600;
96
        $m = floor($time / 60);
97
        $time -= $m * 60;
98
99
        $ret = '';
100
        if ($h > 0) {
101
            $ret .= $h . 'h';
102
        }
103
        if ($m > 0) {
104
            $ret .= ' ' . $m . 'm';
105
        }
106
        if ($time > 0) {
107
            $ret .= ' ' . $time . 's';
108
        }
109
        return $ret;
110
    }
111
112
    public static function getNumberWithThousandSeperator(int $number): string
113
    {
114
        return number_format((float) $number, 0, '', '.');
115
    }
116
117
    public static function getPlanetFieldTypeDescription(
118
        int $fieldTypeId
119
    ): string {
120
        return self::getDic()->get(PlanetFieldTypeRetrieverInterface::class)->getDescription($fieldTypeId);
121
    }
122
123
    public static function getPlanetFieldTitle(
124
        PlanetFieldInterface $planetField
125
    ): string {
126
        $fieldTypeName = self::getPlanetFieldTypeDescription($planetField->getFieldType());
127
128
        $building = $planetField->getBuilding();
129
130
        if ($building === null) {
131
            $terraFormingState = null;
132
            $host = $planetField->getHost();
133
            if ($host instanceof ColonyInterface) {
134
                $terraFormingState = self::getDic()->get(ColonyTerraformingRepositoryInterface::class)->getByColonyAndField(
135
                    $host->getId(),
136
                    $planetField->getId()
137
                );
138
            }
139
            if ($terraFormingState !== null) {
140
                return sprintf(
141
                    '%s läuft bis %s',
142
                    $terraFormingState->getTerraforming()->getDescription(),
143
                    date('d.m.Y H:i', $terraFormingState->getFinishDate())
144
                );
145
            }
146
            return $fieldTypeName;
147
        }
148
149
        if ($planetField->isUnderConstruction()) {
150
            return sprintf(
151
                'In Bau: %s auf %s - Fertigstellung: %s',
152
                $building->getName(),
153
                $fieldTypeName,
154
                date('d.m.Y H:i', $planetField->getBuildtime())
155
            );
156
        }
157
        if (!$planetField->isActivateable()) {
158
            return $building->getName() . " auf " . $fieldTypeName;
159
        }
160
161
        if ($planetField->isActive()) {
162
            if ($planetField->isDamaged()) {
163
                return $building->getName() . " (aktiviert, beschädigt) auf " . $fieldTypeName;
164
            }
165
            return $building->getName() . " (aktiviert) auf " . $fieldTypeName;
166
        }
167
168
        if ($planetField->hasHighDamage()) {
169
            return $building->getName() . " (stark beschädigt) auf " . $fieldTypeName;
170
        }
171
172
        return $building->getName() . " (deaktiviert) auf " . $fieldTypeName;
173
    }
174
175
    /**
176
     * Registers global available tal methods
177
     */
178 4
    public static function register(ContainerInterface $dic): void
179
    {
180 4
        self::setDic($dic);
181
182 4
        TalesRegistry::registerPrefix(
183 4
            'clmodeDescription',
184 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getContactListModeDescription((int) ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
185 4
        );
186 4
        TalesRegistry::registerPrefix(
187 4
            'addPlusCharacter',
188 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::addPlusCharacter((int)' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
189 4
        );
190 4
        TalesRegistry::registerPrefix(
191 4
            'isPositive',
192 4
            fn ($src, $nothrow): string => '(int) ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ' > 0'
193 4
        );
194 4
        TalesRegistry::registerPrefix(
195 4
            'isNegative',
196 4
            fn ($src, $nothrow): string => '(int) ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ' < 0'
197 4
        );
198 4
        TalesRegistry::registerPrefix(
199 4
            'numberWithThousandSeperator',
200 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getNumberWithThousandSeperator(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
201 4
        );
202 4
        TalesRegistry::registerPrefix(
203 4
            'bbcode',
204 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getBBCodeParser()->parse(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')->getAsHtml()'
205 4
        );
206 4
        TalesRegistry::registerPrefix(
207 4
            'bbcode2txt',
208 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getBBCodeParser()->parse(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')->getAsText()'
209 4
        );
210 4
        TalesRegistry::registerPrefix(
211 4
            'jsquote',
212 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::jsquote(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
213 4
        );
214 4
        TalesRegistry::registerPrefix(
215 4
            'datetime',
216 4
            fn ($src, $nothrow): string => 'date(\'d.m.\', ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ') . (date("Y", ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')+' . StuTime::STU_YEARS_IN_FUTURE_OFFSET . ') . " " . date("H:i", ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
217 4
        );
218 4
        TalesRegistry::registerPrefix(
219 4
            'date',
220 4
            fn ($src, $nothrow): string => 'date(\'d.m.\', ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ') . (date("Y", ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')+' . StuTime::STU_YEARS_IN_FUTURE_OFFSET . ')'
221 4
        );
222 4
        TalesRegistry::registerPrefix(
223 4
            'nl2br',
224 4
            fn ($src, $nothrow): string => 'nl2br(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
225 4
        );
226 4
        TalesRegistry::registerPrefix(
227 4
            'nl2brBbCode',
228 4
            fn ($src, $nothrow): string => 'nl2br(\Stu\Module\Tal\TalHelper::getBBCodeParser()->parse(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')->getAsHtml())'
229 4
        );
230 4
        TalesRegistry::registerPrefix(
231 4
            'formatSeconds',
232 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::formatSeconds(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
233 4
        );
234 4
        TalesRegistry::registerPrefix(
235 4
            'planetFieldTypeDescription',
236 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getPlanetFieldTypeDescription(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
237 4
        );
238 4
        TalesRegistry::registerPrefix(
239 4
            'planetFieldTitle',
240 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getPlanetFieldTitle(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
241 4
        );
242 4
        TalesRegistry::registerPrefix(
243 4
            'formatProductionValue',
244 4
            fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::formatProductionValue(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')'
245 4
        );
246
    }
247
}
248