Passed
Pull Request — master (#1833)
by Nico
34:40
created

TwigHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 7
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Twig;
6
7
use JBBCode\Parser;
8
use Noodlehaus\ConfigInterface;
9
use Stu\Component\Building\NameAbbreviations;
10
use Stu\Component\Colony\ColonyMenuEnum;
11
use Stu\Component\Game\ModuleViewEnum;
12
use Stu\Component\Ship\Crew\ShipCrewCalculatorInterface;
13
use Stu\Lib\Colony\PlanetFieldHostInterface;
14
use Stu\Lib\ModuleScreen\GradientColorInterface;
15
use Stu\Module\Colony\Lib\ColonyEpsProductionPreviewWrapper;
16
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
17
use Stu\Module\Colony\Lib\ColonyProductionPreviewWrapper;
18
use Stu\Module\Control\StuTime;
19
use Stu\Module\Ship\Lib\Battle\FightLibInterface;
20
use Stu\Module\Ship\Lib\ShipNfsItem;
21
use Stu\Module\Tal\TalHelper;
22
use Stu\Orm\Entity\AnomalyInterface;
23
use Stu\Orm\Entity\BuildingInterface;
24
use Stu\Orm\Entity\ShipInterface;
25
use Twig\Environment;
26
use Twig\TwigFilter;
27
use Twig\TwigFunction;
28
29
class TwigHelper
30
{
31
    private Environment $environment;
32
33
    private Parser $parser;
34
35
    private ConfigInterface $config;
36
37
    private FightLibInterface $fightLib;
38
39
    private ColonyLibFactoryInterface $colonyLibFactory;
40
41
    private ShipCrewCalculatorInterface $shipCrewCalculator;
42
43
    private GradientColorInterface $gradientColor;
44
45 4
    public function __construct(
46
        Environment $environment,
47
        Parser $parser,
48
        ConfigInterface $config,
49
        FightLibInterface $fightLib,
50
        ColonyLibFactoryInterface $colonyLibFactory,
51
        ShipCrewCalculatorInterface $shipCrewCalculator,
52
        GradientColorInterface $gradientColor
53
    ) {
54 4
        $this->environment = $environment;
55 4
        $this->parser = $parser;
56 4
        $this->config = $config;
57 4
        $this->fightLib = $fightLib;
58 4
        $this->colonyLibFactory = $colonyLibFactory;
59 4
        $this->shipCrewCalculator = $shipCrewCalculator;
60 4
        $this->gradientColor = $gradientColor;
61
    }
62
63 4
    public function registerGlobalVariables(): void
64
    {
65 4
        $this->environment->addGlobal(
66 4
            'ASSET_PATHS',
67 4
            [
68 4
                'alliance' => $this->config->get('game.alliance_avatar_path'),
69 4
                'user' => $this->config->get('game.user_avatar_path'),
70 4
                'faction' => 'assets/rassen/',
71 4
            ]
72 4
        );
73
    }
74
75
    /**
76
     * Registers global available twig methods and filters
77
     */
78 4
    public function registerFiltersAndFunctions(): void
79
    {
80 4
        $this->registerFilters();
81 4
        $this->registerFunctions();
82
    }
83
84 4
    private function registerFilters(): void
85
    {
86 4
        $bbcode2txtFilter = new TwigFilter('bbcode2txt', function ($string): string {
87
            return $this->parser->parse($string)->getAsText();
88 4
        });
89 4
        $this->environment->addFilter($bbcode2txtFilter);
90
91 4
        $bbcodeFilter = new TwigFilter('bbcode', function ($string): string {
92
            return $this->parser->parse($string)->getAsHTML();
93 4
        }, ['is_safe' => ['html']]);
94 4
        $this->environment->addFilter($bbcodeFilter);
95
96 4
        $jsquoteFilter = new TwigFilter('jsquote', function ($string): string {
97
            return TalHelper::jsquote($string);
98 4
        });
99 4
        $this->environment->addFilter($jsquoteFilter);
100
101 4
        $addPlusCharacterFilter = new TwigFilter('addPlusCharacter', function ($value): string {
102
            if (is_int($value)) {
103
                return TalHelper::addPlusCharacter((string) $value);
104
            }
105
            return TalHelper::addPlusCharacter($value);
106 4
        });
107 4
        $this->environment->addFilter($addPlusCharacterFilter);
108
109 4
        $formatSecondsFilter = new TwigFilter('formatSeconds', function ($value): string {
110
            if (is_int($value)) {
111
                return TalHelper::formatSeconds((string) $value);
112
            }
113
            return TalHelper::formatSeconds($value);
114 4
        });
115 4
        $this->environment->addFilter($formatSecondsFilter);
116
117 4
        $planetFieldTitleFilter = new TwigFilter('planetFieldTitle', function ($planetField): string {
118
            return TalHelper::getPlanetFieldTitle($planetField);
119 4
        });
120 4
        $this->environment->addFilter($planetFieldTitleFilter);
121
122 4
        $planetFieldTypeDescriptionFilter = new TwigFilter('planetFieldTypeDescription', function ($id): string {
123
            return TalHelper::getPlanetFieldTypeDescription($id);
124 4
        });
125 4
        $this->environment->addFilter($planetFieldTypeDescriptionFilter);
126
127 4
        $formatProductionValueFilter = new TwigFilter('formatProductionValue', function ($value): string {
128
            return TalHelper::formatProductionValue($value);
129 4
        });
130 4
        $this->environment->addFilter($formatProductionValueFilter);
131
132 4
        $isPositiveFilter = new TwigFilter('isPositive', function (int $value): bool {
133
            return $value > 0;
134 4
        });
135 4
        $this->environment->addFilter($isPositiveFilter);
136
137 4
        $stuDateTimeFilter = new TwigFilter('stuDateTime', function ($value): string {
138
            return sprintf(
139
                '%s%s %s',
140
                date('d.m.', $value),
141
                (int)date("Y", $value) + StuTime::STU_YEARS_IN_FUTURE_OFFSET,
142
                date("H:i", $value)
143
            );
144 4
        });
145 4
        $this->environment->addFilter($stuDateTimeFilter);
146
147 4
        $stuDateFilter = new TwigFilter('stuDate', function ($value): string {
148
            return sprintf(
149
                '%s%s',
150
                date('d.m.', $value),
151
                (int)date("Y", $value) + StuTime::STU_YEARS_IN_FUTURE_OFFSET
152
            );
153 4
        });
154 4
        $this->environment->addFilter($stuDateFilter);
155
156 4
        $nl2brFilter = new TwigFilter('nl2br', function (string $value): string {
157
            return nl2br($value);
158 4
        });
159 4
        $this->environment->addFilter($nl2brFilter);
160
161 4
        $htmlSafeFilter = new TwigFilter('htmlSafe', function (string $text): string {
162
            return htmlspecialchars($text);
163 4
        });
164 4
        $this->environment->addFilter($htmlSafeFilter);
165
166 4
        $adventDoorFilter = new TwigFilter('adventDoor', function (AnomalyInterface $anomaly): int {
167
            return (int)((120 - $anomaly->getRemainingTicks()) / 5) + 1;
168 4
        });
169 4
        $this->environment->addFilter($adventDoorFilter);
170
171 4
        $shortNameFilter = new TwigFilter('shortName', function (string $name): string {
172
            return array_reduce(
173
                array_keys(NameAbbreviations::ABBREVIATIONS),
174
                fn (string $value, string $from): string => str_replace($from, NameAbbreviations::ABBREVIATIONS[$from], $value),
175
                $name
176
            );
177 4
        });
178 4
        $this->environment->addFilter($shortNameFilter);
179
180 4
        $getMaxCrewCountByShipFilter = new TwigFilter('getMaxCrewCountByShip', function (ShipInterface $ship): int {
181
            return $this->shipCrewCalculator->getMaxCrewCountByShip($ship);
182 4
        });
183 4
        $this->environment->addFilter($getMaxCrewCountByShipFilter);
184
185 4
        $numberWithThousandSeperatorFilter = new TwigFilter('numberWithThousandSeperator', function ($value): string {
186
            return TalHelper::getNumberWithThousandSeperator($value);
187 4
        });
188 4
        $this->environment->addFilter($numberWithThousandSeperatorFilter);
189
    }
190
191 4
    private function registerFunctions(): void
192
    {
193 4
        $canAttackTargetFunction = new TwigFunction('canAttackTarget', function (ShipInterface $ship, ShipInterface|ShipNfsItem $target): bool {
194
            return $this->fightLib->canAttackTarget($ship, $target);
195 4
        });
196 4
        $this->environment->addFunction($canAttackTargetFunction);
197
198 4
        $getEpsProductionPreviewFunction = new TwigFunction('getEpsProductionPreview', function (PlanetFieldHostInterface $host, BuildingInterface $building): ColonyEpsProductionPreviewWrapper {
199
            return $this->colonyLibFactory->createEpsProductionPreviewWrapper($host, $building);
200 4
        });
201 4
        $this->environment->addFunction($getEpsProductionPreviewFunction);
202
203 4
        $getCommodityProductionPreviewFunction = new TwigFunction('getCommodityProductionPreview', function (PlanetFieldHostInterface $host, BuildingInterface $building): ColonyProductionPreviewWrapper {
204
            return $this->colonyLibFactory->createColonyProductionPreviewWrapper($building, $host);
205 4
        });
206 4
        $this->environment->addFunction($getCommodityProductionPreviewFunction);
207
208 4
        $getColonyMenuClassFunction = new TwigFunction('getColonyMenuClass', function (ColonyMenuEnum $currentMenu, int $value): string {
209
            return ColonyMenuEnum::getMenuClass($currentMenu, $value);
210 4
        });
211 4
        $this->environment->addFunction($getColonyMenuClassFunction);
212
213 4
        $getViewFunction = new TwigFunction('getView', function (string $value): ModuleViewEnum {
214
            return ModuleViewEnum::from($value);
215 4
        });
216 4
        $this->environment->addFunction($getViewFunction);
217
218 4
        $getUniqIdFunction = new TwigFunction('getUniqId', function (): string {
219
            return uniqid();
220 4
        });
221 4
        $this->environment->addFunction($getUniqIdFunction);
222
223 4
        $gradientColorFunction = new TwigFunction('gradientColor', function (int $value, int $lowest, int $highest): string {
224
            return $this->gradientColor->calculateGradientColor($value, $lowest, $highest);
225 4
        });
226 4
        $this->environment->addFunction($gradientColorFunction);
227
    }
228
}
229