Passed
Pull Request — master (#1976)
by Janko
29:19 queued 17:22
created

TwigHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 98.84%

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 140
ccs 85
cts 86
cp 0.9884
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerFiltersAndFunctions() 0 4 1
A registerGlobalVariables() 0 8 1
A registerFilters() 0 66 3
A registerFunctions() 0 35 1
A __construct() 0 13 1
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;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Building\NameAbbreviations 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...
10
use Stu\Component\Colony\ColonyMenuEnum;
11
use Stu\Component\Game\ModuleViewEnum;
12
use Stu\Component\Spacecraft\Crew\SpacecraftCrewCalculatorInterface;
13
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
14
use Stu\Component\Spacecraft\System\SpacecraftSystemWrapper;
15
use Stu\Component\Spacecraft\System\SpacecraftSystemWrapperFactoryInterface;
16
use Stu\Lib\Colony\PlanetFieldHostInterface;
17
use Stu\Lib\ModuleScreen\GradientColorInterface;
18
use Stu\Module\Colony\Lib\ColonyEpsProductionPreviewWrapper;
19
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
20
use Stu\Module\Colony\Lib\ColonyProductionPreviewWrapper;
21
use Stu\Module\Control\StuRandom;
22
use Stu\Module\Control\StuTime;
23
use Stu\Module\Spacecraft\Lib\Battle\FightLibInterface;
24
use Stu\Module\Spacecraft\Lib\ShipNfsItem;
25
use Stu\Module\Template\TemplateHelperInterface;
26
use Stu\Orm\Entity\AnomalyInterface;
27
use Stu\Orm\Entity\BuildingInterface;
28
use Stu\Orm\Entity\PlanetFieldInterface;
29
use Stu\Orm\Entity\SpacecraftInterface;
30
use Twig\Environment;
31
use Twig\TwigFilter;
32
use Twig\TwigFunction;
33
34
class TwigHelper
35
{
36 2
    public function __construct(
37
        private Environment $environment,
38
        private Parser $parser,
39
        private ConfigInterface $config,
40
        private FightLibInterface $fightLib,
41
        private ColonyLibFactoryInterface $colonyLibFactory,
42
        private SpacecraftCrewCalculatorInterface $shipCrewCalculator,
43
        private SpacecraftSystemWrapperFactoryInterface $spacecraftSystemWrapperFactory,
44
        private GradientColorInterface $gradientColor,
45
        private TemplateHelperInterface $templateHelper,
46
        private StuTime $stuTime,
47
        private StuRandom $stuRandom
48 2
    ) {}
49
50 2
    public function registerGlobalVariables(): void
51
    {
52 2
        $this->environment->addGlobal(
53 2
            'ASSET_PATHS',
54 2
            [
55 2
                'alliance' => $this->config->get('game.alliance_avatar_path'),
56 2
                'user' => $this->config->get('game.user_avatar_path'),
57 2
                'faction' => 'assets/rassen/',
58 2
            ]
59 2
        );
60
    }
61
62
    /**
63
     * Registers global available twig methods and filters
64
     */
65 2
    public function registerFiltersAndFunctions(): void
66
    {
67 2
        $this->registerFilters();
68 2
        $this->registerFunctions();
69
    }
70
71 99
    private function registerFilters(): void
72
    {
73 87
        $bbcode2txtFilter = new TwigFilter('bbcode2txt', fn($string): string => $this->parser->parse($string)->getAsText());
74 2
        $this->environment->addFilter($bbcode2txtFilter);
75
76 99
        $bbcodeFilter = new TwigFilter('bbcode', fn($string): string => $this->parser->parse($string)->getAsHTML(), ['is_safe' => ['html']]);
77 2
        $this->environment->addFilter($bbcodeFilter);
78
79 2
        $jsquoteFilter = new TwigFilter('jsquote', fn($string): string => $this->templateHelper->jsquote($string));
80 2
        $this->environment->addFilter($jsquoteFilter);
81
82 2
        $addPlusCharacterFilter = new TwigFilter('addPlusCharacter', function ($value): string {
83 7
            if (is_int($value)) {
84 7
                return $this->templateHelper->addPlusCharacter((string) $value);
85
            }
86 1
            return $this->templateHelper->addPlusCharacter($value);
87 2
        });
88 2
        $this->environment->addFilter($addPlusCharacterFilter);
89
90 2
        $formatSecondsFilter = new TwigFilter('formatSeconds', function ($value): string {
91 8
            if (is_int($value)) {
92 8
                return $this->templateHelper->formatSeconds((string) $value);
93
            }
94
            return $this->templateHelper->formatSeconds($value);
95 2
        });
96 2
        $this->environment->addFilter($formatSecondsFilter);
97
98 6
        $planetFieldTitleFilter = new TwigFilter('planetFieldTitle', fn($planetField): string => $this->templateHelper->getPlanetFieldTitle($planetField));
99 2
        $this->environment->addFilter($planetFieldTitleFilter);
100
101 3
        $planetFieldTypeDescriptionFilter = new TwigFilter('planetFieldTypeDescription', fn($id): string => $this->templateHelper->getPlanetFieldTypeDescription($id));
102 2
        $this->environment->addFilter($planetFieldTypeDescriptionFilter);
103
104 4
        $formatProductionValueFilter = new TwigFilter('formatProductionValue', fn($value): string => $this->templateHelper->formatProductionValue($value));
105 2
        $this->environment->addFilter($formatProductionValueFilter);
106
107 2
        $isPositiveFilter = new TwigFilter('isPositive', fn(int $value): bool => $value > 0);
108 2
        $this->environment->addFilter($isPositiveFilter);
109
110 12
        $stuDateTimeFilter = new TwigFilter('stuDateTime', fn($value): string => $this->stuTime->transformToStuDateTime($value));
111 2
        $this->environment->addFilter($stuDateTimeFilter);
112
113 8
        $stuDateFilter = new TwigFilter('stuDate', fn($value): string => $this->stuTime->transformToStuDate($value));
114 2
        $this->environment->addFilter($stuDateFilter);
115
116 10
        $nl2brFilter = new TwigFilter('nl2br', fn(string $value): string => nl2br($value));
117 2
        $this->environment->addFilter($nl2brFilter);
118
119 15
        $htmlSafeFilter = new TwigFilter('htmlSafe', fn(string $text): string => htmlspecialchars($text));
120 2
        $this->environment->addFilter($htmlSafeFilter);
121
122 2
        $adventDoorFilter = new TwigFilter('adventDoor', fn(AnomalyInterface $anomaly): int => (int)((120 - $anomaly->getRemainingTicks()) / 5) + 1);
123 2
        $this->environment->addFilter($adventDoorFilter);
124
125 3
        $shortNameFilter = new TwigFilter('shortName', fn(string $name): string => array_reduce(
126 3
            array_keys(NameAbbreviations::ABBREVIATIONS),
127 3
            fn(string $value, string $from): string => str_replace($from, NameAbbreviations::ABBREVIATIONS[$from], $value),
128 3
            $name
129 3
        ));
130 2
        $this->environment->addFilter($shortNameFilter);
131
132 3
        $getMaxCrewCountByShipFilter = new TwigFilter('getMaxCrewCountByShip', fn(SpacecraftInterface $spacecraft): int => $this->shipCrewCalculator->getMaxCrewCountByShip($spacecraft));
133 2
        $this->environment->addFilter($getMaxCrewCountByShipFilter);
134
135 4
        $numberWithThousandSeperatorFilter = new TwigFilter('numberWithThousandSeperator', fn($value): string => $this->templateHelper->getNumberWithThousandSeperator($value));
136 2
        $this->environment->addFilter($numberWithThousandSeperatorFilter);
137
    }
138
139 10
    private function registerFunctions(): void
140
    {
141 3
        $canAttackTargetFunction = new TwigFunction('canAttackTarget', fn(SpacecraftInterface $spacecraft, SpacecraftInterface|ShipNfsItem $target): bool => $this->fightLib->canAttackTarget($spacecraft, $target));
142 2
        $this->environment->addFunction($canAttackTargetFunction);
143
144 3
        $getEpsProductionPreviewFunction = new TwigFunction('getEpsProductionPreview', fn(PlanetFieldHostInterface $host, BuildingInterface $building): ColonyEpsProductionPreviewWrapper => $this->colonyLibFactory->createEpsProductionPreviewWrapper($host, $building));
145 2
        $this->environment->addFunction($getEpsProductionPreviewFunction);
146
147 3
        $getCommodityProductionPreviewFunction = new TwigFunction('getCommodityProductionPreview', fn(PlanetFieldHostInterface $host, BuildingInterface $building): ColonyProductionPreviewWrapper => $this->colonyLibFactory->createColonyProductionPreviewWrapper($building, $host));
148 2
        $this->environment->addFunction($getCommodityProductionPreviewFunction);
149
150 7
        $getColonyMenuClassFunction = new TwigFunction('getColonyMenuClass', fn(ColonyMenuEnum $currentMenu, int $value): string => ColonyMenuEnum::getMenuClass($currentMenu, $value));
151 2
        $this->environment->addFunction($getColonyMenuClassFunction);
152
153 5
        $getViewFunction = new TwigFunction('getView', fn(string $value): ModuleViewEnum => ModuleViewEnum::from($value));
154 2
        $this->environment->addFunction($getViewFunction);
155
156 10
        $getUniqIdFunction = new TwigFunction('getUniqId', fn(): string => $this->stuRandom->uniqid());
157 2
        $this->environment->addFunction($getUniqIdFunction);
158
159 2
        $gradientColorFunction = new TwigFunction('gradientColor', fn(int $value, int $lowest, int $highest): string => $this->gradientColor->calculateGradientColor($value, $lowest, $highest));
160 2
        $this->environment->addFunction($gradientColorFunction);
161
162 5
        $gradientColorFunction = new TwigFunction('stuDate', fn(string $format): string => $this->stuTime->date($format));
163 2
        $this->environment->addFunction($gradientColorFunction);
164
165 6
        $dayNightPrefixFunction = new TwigFunction('getDayNightPrefix', fn(PlanetFieldInterface $field): string => $field->getDayNightPrefix($this->stuTime->time()));
166 2
        $this->environment->addFunction($dayNightPrefixFunction);
167
168 2
        $hasSpacecraftSystemByNameFunction = new TwigFunction(
169 2
            'getSpacecraftSystemWrapper',
170 2
            fn(SpacecraftInterface $spacecraft, string $name): ?SpacecraftSystemWrapper
171 2
            => $this->spacecraftSystemWrapperFactory->create($spacecraft, SpacecraftSystemTypeEnum::getByName($name))
172 2
        );
173 2
        $this->environment->addFunction($hasSpacecraftSystemByNameFunction);
174
    }
175
}
176