Passed
Push — dev ( d91619...3d1f41 )
by Nico
14:25
created

setTemplateVariablesForAlliance()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 88
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 67
CRAP Score 7.0163

Importance

Changes 0
Metric Value
cc 7
eloc 59
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 88
ccs 67
cts 72
cp 0.9306
crap 7.0163
rs 7.9612

How to fix   Long Method   

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\GameEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\GameEnum 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...
12
use Stu\Component\Game\ModuleEnum;
13
use Stu\Component\Alliance\AllianceSettingsEnum;
14
use Stu\Module\Alliance\Lib\AllianceActionManagerInterface;
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\AllianceInterface;
20
use Stu\Orm\Entity\AllianceSettingsInterface;
21
use Stu\Orm\Entity\UserInterface;
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) {}
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();", GameEnum::JS_EXECUTION_AFTER_RENDER);
52
    }
53
54 1
    private function setTemplateVariablesForAlliance(AllianceInterface $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 = $alliance->getFounder()->getUser() === $user;
61 1
        $isInAlliance = $alliance === $game->getUser()->getAlliance();
62 1
        $settings = $alliance->getSettings();
0 ignored issues
show
Unused Code introduced by
The assignment to $settings is dead and can be removed.
Loading history...
63
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(UserInterface $user): AllianceMemberWrapper => $this->allianceUiFactory->createAllianceMemberWrapper($user, $alliance)
107 1
            )
108 1
        );
109
110 1
        $founderDescription = $alliance->getSettings()->filter(
111 1
            function (AllianceSettingsInterface $setting) {
112
                return $setting->getSetting() === AllianceSettingsEnum::ALLIANCE_FOUNDER_DESCRIPTION;
113 1
            }
114 1
        )->first();
115
116 1
        $successorDescription = $alliance->getSettings()->filter(
117 1
            function (AllianceSettingsInterface $setting) {
118
                return $setting->getSetting() === AllianceSettingsEnum::ALLIANCE_SUCCESSOR_DESCRIPTION;
119 1
            }
120 1
        )->first();
121
122 1
        $diplomatDescription = $alliance->getSettings()->filter(
123 1
            function (AllianceSettingsInterface $setting) {
124
                return $setting->getSetting() === AllianceSettingsEnum::ALLIANCE_DIPLOMATIC_DESCRIPTION;
125 1
            }
126 1
        )->first();
127
128
129 1
        $game->setTemplateVar(
130 1
            'FOUNDER_DESCRIPTION',
131 1
            $founderDescription !== false ? $founderDescription->getValue() : 'Präsident'
132 1
        );
133
134 1
        $game->setTemplateVar(
135 1
            'SUCCESSOR_DESCRIPTION',
136 1
            $successorDescription !== false ? $successorDescription->getValue() : 'Vize-Präsident'
137 1
        );
138
139 1
        $game->setTemplateVar(
140 1
            'DIPLOMATIC_DESCRIPTION',
141 1
            $diplomatDescription !== false ? $diplomatDescription->getValue() : 'Außenminister'
142 1
        );
143
    }
144
145
    private function setTemplateVariablesForAllianceList(GameControllerInterface $game): void
146
    {
147
        $game->appendNavigationPart(sprintf(
148
            '%s?showlist=1',
149
            ModuleEnum::ALLIANCE->getPhpPage()
150
        ), _('Allianzliste'));
151
152
        $game->setTemplateVar('SHOW_ALLIANCE_LIST', true);
153
        $game->setTemplateVar(
154
            'ALLIANCE_LIST_OPEN',
155
            array_map(
156
                fn(AllianceInterface $alliance): AllianceListItem => $this->allianceUiFactory->createAllianceListItem($alliance),
157
                $this->allianceRepository->findByApplicationState(true)
158
            )
159
        );
160
        $game->setTemplateVar(
161
            'ALLIANCE_LIST_CLOSED',
162
            array_map(
163
                fn(AllianceInterface $alliance): AllianceListItem => $this->allianceUiFactory->createAllianceListItem($alliance),
164
                $this->allianceRepository->findByApplicationState(false)
165
            )
166
        );
167
    }
168
}
169