Passed
Push — dev ( 79c88c...be1bbd )
by Nico
05:18
created

setTemplateVariablesForAlliance()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 116
Code Lines 79

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 81
CRAP Score 13.2243

Importance

Changes 0
Metric Value
cc 13
eloc 79
nc 12
nop 2
dl 0
loc 116
ccs 81
cts 91
cp 0.8901
crap 13.2243
rs 5.7515
c 0
b 0
f 0

How to fix   Long Method    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
declare(strict_types=1);
4
5
namespace Stu\Module\Game\Lib\View\Provider;
6
7
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...
8
use request;
9
use Stu\Component\Alliance\AllianceDescriptionRendererInterface;
10
use Stu\Component\Alliance\AllianceUserApplicationCheckerInterface;
11
use Stu\Component\Game\ModuleEnum;
12
use Stu\Component\Alliance\AllianceSettingsEnum;
13
use Stu\Component\Game\JavascriptExecutionTypeEnum;
14
use Stu\Module\Alliance\Lib\AllianceActionManagerInterface;
15
use Stu\Module\Alliance\Lib\AllianceJobManagerInterface;
16
use Stu\Module\Alliance\Lib\AllianceListItem;
17
use Stu\Module\Alliance\Lib\AllianceMemberWrapper;
18
use Stu\Module\Alliance\Lib\AllianceUiFactoryInterface;
19
use Stu\Module\Control\GameControllerInterface;
20
use Stu\Orm\Entity\Alliance;
21
use Stu\Orm\Entity\AllianceSettings;
22
use Stu\Orm\Entity\User;
23
use Stu\Orm\Repository\AllianceRelationRepositoryInterface;
24
use Stu\Orm\Repository\AllianceRepositoryInterface;
25
26
final class AllianceProvider implements ViewComponentProviderInterface
27
{
28 1
    public function __construct(private AllianceRelationRepositoryInterface $allianceRelationRepository, private AllianceActionManagerInterface $allianceActionManager, private AllianceRepositoryInterface $allianceRepository, private AllianceUserApplicationCheckerInterface $allianceUserApplicationChecker, private AllianceDescriptionRendererInterface $allianceDescriptionRenderer, private AllianceUiFactoryInterface $allianceUiFactory, private AllianceJobManagerInterface $allianceJobManager) {}
29
30 1
    #[Override]
31
    public function setTemplateVariables(GameControllerInterface $game): void
32
    {
33 1
        $user = $game->getUser();
34
35 1
        $alliance = null;
36 1
        if (request::has('id')) {
37
            $alliance = $this->allianceRepository->find(request::indInt('id'));
38
        }
39
40 1
        if ($alliance === null) {
41 1
            $alliance = $user->getAlliance();
42
        }
43
44 1
        $game->setTemplateVar('ALLIANCE', $alliance);
45
46 1
        if ($alliance === null || request::has('showlist')) {
47
            $this->setTemplateVariablesForAllianceList($game);
48
        } else {
49 1
            $this->setTemplateVariablesForAlliance($alliance, $game);
50
        }
51
52 1
        $game->addExecuteJS("initTranslations();", JavascriptExecutionTypeEnum::AFTER_RENDER);
53
    }
54
55 1
    private function setTemplateVariablesForAlliance(Alliance $alliance, GameControllerInterface $game): void
56
    {
57 1
        $user = $game->getUser();
58 1
        $allianceId = $alliance->getId();
59
60 1
        $result = $this->allianceRelationRepository->getActiveByAlliance($allianceId);
61 1
        $userIsFounder = $this->allianceJobManager->hasUserFounderPermission($user, $alliance);
62 1
        $isInAlliance = $alliance->getId() === $game->getUser()->getAlliance()?->getId();
63 1
        $settings = $alliance->getSettings();
64
65 1
        $game->appendNavigationPart(sprintf(
66 1
            '%s?id=%d',
67 1
            ModuleEnum::ALLIANCE->getPhpPage(),
68 1
            $alliance->getId()
69 1
        ), _('Allianz anzeigen'));
70
71 1
        $relations = [];
72 1
        foreach ($result as $key => $relation) {
73
            $relations[$key] = $this->allianceUiFactory->createAllianceRelationWrapper($alliance, $relation);
74
        }
75
76 1
        $game->setTemplateVar('SHOW_ALLIANCE', $alliance);
77
78 1
        $game->setTemplateVar(
79 1
            'ALLIANCE_RELATIONS',
80 1
            $relations !== []
81
                ? $relations
82 1
                : null
83 1
        );
84 1
        $game->setTemplateVar(
85 1
            'DESCRIPTION',
86 1
            $this->allianceDescriptionRenderer->render($alliance)
87 1
        );
88 1
        $game->setTemplateVar('IS_IN_ALLIANCE', $isInAlliance);
89 1
        $game->setTemplateVar('CAN_LEAVE_ALLIANCE', $isInAlliance && !$userIsFounder);
90 1
        $game->setTemplateVar(
91 1
            'CAN_EDIT',
92 1
            $this->allianceActionManager->mayEdit($alliance, $user)
93 1
        );
94 1
        $game->setTemplateVar(
95 1
            'CAN_MANAGE_FOREIGN_RELATIONS',
96 1
            $this->allianceActionManager->mayManageForeignRelations($alliance, $user)
97 1
        );
98 1
        $game->setTemplateVar(
99 1
            'CAN_SIGNUP',
100 1
            $this->allianceUserApplicationChecker->mayApply($user, $alliance)
101 1
        );
102
103 1
        $game->setTemplateVar(
104 1
            'MEMBERS',
105 1
            $alliance->getMembers()->map(
106 1
                fn(User $user): AllianceMemberWrapper => $this->allianceUiFactory->createAllianceMemberWrapper($user, $alliance)
107 1
            )
108 1
        );
109
110 1
        $founderJobs = [];
111 1
        $successorJobs = [];
112 1
        $diplomaticJobs = [];
113 1
        $otherJobs = [];
114
115 1
        foreach ($alliance->getJobs() as $job) {
116 1
            if (count($job->getUsers()) === 0 || $job->getSort() === null) {
117 1
                continue;
118
            }
119
120 1
            if ($job->hasFounderPermission()) {
121 1
                $founderJobs[] = $job;
122
            } elseif ($job->hasSuccessorPermission()) {
123
                $successorJobs[] = $job;
124
            } elseif ($job->hasDiplomaticPermission()) {
125
                $diplomaticJobs[] = $job;
126
            } else {
127
                $otherJobs[] = $job;
128
            }
129
        }
130
131 1
        usort($successorJobs, fn($a, $b) => $a->getSort() <=> $b->getSort());
132 1
        usort($diplomaticJobs, fn($a, $b) => $a->getSort() <=> $b->getSort());
133 1
        usort($otherJobs, fn($a, $b) => $a->getSort() <=> $b->getSort());
134
135 1
        $leadershipJobs = array_merge($founderJobs, $successorJobs, $diplomaticJobs, $otherJobs);
136
137 1
        $game->setTemplateVar('ALLIANCE_LEADERSHIP_JOBS', $leadershipJobs);
138
139 1
        $founderDescription = $settings->filter(
140 1
            function (AllianceSettings $setting): bool {
141
                return $setting->getSetting() === AllianceSettingsEnum::ALLIANCE_FOUNDER_DESCRIPTION;
142 1
            }
143 1
        )->first();
144
145 1
        $successorDescription = $settings->filter(
146 1
            function (AllianceSettings $setting): bool {
147
                return $setting->getSetting() === AllianceSettingsEnum::ALLIANCE_SUCCESSOR_DESCRIPTION;
148 1
            }
149 1
        )->first();
150
151 1
        $diplomatDescription = $settings->filter(
152 1
            function (AllianceSettings $setting): bool {
153
                return $setting->getSetting() === AllianceSettingsEnum::ALLIANCE_DIPLOMATIC_DESCRIPTION;
154 1
            }
155 1
        )->first();
156
157
158 1
        $game->setTemplateVar(
159 1
            'FOUNDER_DESCRIPTION',
160 1
            $founderDescription !== false ? $founderDescription->getValue() : 'Präsident'
161 1
        );
162
163 1
        $game->setTemplateVar(
164 1
            'SUCCESSOR_DESCRIPTION',
165 1
            $successorDescription !== false ? $successorDescription->getValue() : 'Vize-Präsident'
166 1
        );
167
168 1
        $game->setTemplateVar(
169 1
            'DIPLOMATIC_DESCRIPTION',
170 1
            $diplomatDescription !== false ? $diplomatDescription->getValue() : 'Außenminister'
171 1
        );
172
    }
173
174
    private function setTemplateVariablesForAllianceList(GameControllerInterface $game): void
175
    {
176
        $game->appendNavigationPart(sprintf(
177
            '%s?showlist=1',
178
            ModuleEnum::ALLIANCE->getPhpPage()
179
        ), _('Allianzliste'));
180
181
        $game->setTemplateVar('SHOW_ALLIANCE_LIST', true);
182
        $game->setTemplateVar(
183
            'ALLIANCE_LIST_OPEN',
184
            array_map(
185
                fn(Alliance $alliance): AllianceListItem => $this->allianceUiFactory->createAllianceListItem($alliance),
186
                $this->allianceRepository->findByApplicationState(true)
187
            )
188
        );
189
        $game->setTemplateVar(
190
            'ALLIANCE_LIST_CLOSED',
191
            array_map(
192
                fn(Alliance $alliance): AllianceListItem => $this->allianceUiFactory->createAllianceListItem($alliance),
193
                $this->allianceRepository->findByApplicationState(false)
194
            )
195
        );
196
    }
197
}
198