setTemplateVariablesForAlliance()   C
last analyzed

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