Passed
Push — dev ( 520705...3e16a6 )
by Janko
21:10 queued 05:06
created

setTemplateVariablesForAlliance()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 52
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 4.0016

Importance

Changes 0
Metric Value
cc 4
eloc 37
nc 2
nop 2
dl 0
loc 52
ccs 41
cts 43
cp 0.9535
crap 4.0016
rs 9.328
c 0
b 0
f 0

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\Module\Alliance\Lib\AllianceActionManagerInterface;
14
use Stu\Module\Alliance\Lib\AllianceListItem;
15
use Stu\Module\Alliance\Lib\AllianceMemberWrapper;
16
use Stu\Module\Alliance\Lib\AllianceUiFactoryInterface;
17
use Stu\Module\Control\GameControllerInterface;
18
use Stu\Orm\Entity\AllianceInterface;
19
use Stu\Orm\Entity\UserInterface;
20
use Stu\Orm\Repository\AllianceRelationRepositoryInterface;
21
use Stu\Orm\Repository\AllianceRepositoryInterface;
22
23
final class AllianceProvider implements ViewComponentProviderInterface
24
{
25 1
    public function __construct(private AllianceRelationRepositoryInterface $allianceRelationRepository, private AllianceActionManagerInterface $allianceActionManager, private AllianceRepositoryInterface $allianceRepository, private AllianceUserApplicationCheckerInterface $allianceUserApplicationChecker, private AllianceDescriptionRendererInterface $allianceDescriptionRenderer, private AllianceUiFactoryInterface $allianceUiFactory) {}
26
27 1
    #[Override]
28
    public function setTemplateVariables(GameControllerInterface $game): void
29
    {
30 1
        $user = $game->getUser();
31
32 1
        $alliance = null;
33 1
        if (request::has('id')) {
34
            $alliance = $this->allianceRepository->find(request::indInt('id'));
35
        }
36
37 1
        if ($alliance === null) {
38 1
            $alliance = $user->getAlliance();
39
        }
40
41 1
        $game->setTemplateVar('ALLIANCE', $alliance);
42
43 1
        if ($alliance === null || request::has('showlist')) {
44
            $this->setTemplateVariablesForAllianceList($game);
45
        } else {
46 1
            $this->setTemplateVariablesForAlliance($alliance, $game);
47
        }
48
49 1
        $game->addExecuteJS("initTranslations();", GameEnum::JS_EXECUTION_AFTER_RENDER);
50
    }
51
52 1
    private function setTemplateVariablesForAlliance(AllianceInterface $alliance, GameControllerInterface $game): void
53
    {
54 1
        $user = $game->getUser();
55 1
        $allianceId = $alliance->getId();
56
57 1
        $result = $this->allianceRelationRepository->getActiveByAlliance($allianceId);
58 1
        $userIsFounder = $alliance->getFounder()->getUser() === $user;
59 1
        $isInAlliance = $alliance === $game->getUser()->getAlliance();
60
61
62 1
        $game->appendNavigationPart(sprintf(
63 1
            '%s?id=%d',
64 1
            ModuleEnum::ALLIANCE->getPhpPage(),
65 1
            $alliance->getId()
66 1
        ), _('Allianz anzeigen'));
67
68 1
        $relations = [];
69 1
        foreach ($result as $key => $relation) {
70
            $relations[$key] = $this->allianceUiFactory->createAllianceRelationWrapper($alliance, $relation);
71
        }
72
73 1
        $game->setTemplateVar('SHOW_ALLIANCE', $alliance);
74
75 1
        $game->setTemplateVar(
76 1
            'ALLIANCE_RELATIONS',
77 1
            $relations !== []
78
                ? $relations
79 1
                : null
80 1
        );
81 1
        $game->setTemplateVar(
82 1
            'DESCRIPTION',
83 1
            $this->allianceDescriptionRenderer->render($alliance)
84 1
        );
85 1
        $game->setTemplateVar('IS_IN_ALLIANCE', $isInAlliance);
86 1
        $game->setTemplateVar('CAN_LEAVE_ALLIANCE', $isInAlliance && !$userIsFounder);
87 1
        $game->setTemplateVar(
88 1
            'CAN_EDIT',
89 1
            $this->allianceActionManager->mayEdit($alliance, $user)
90 1
        );
91 1
        $game->setTemplateVar(
92 1
            'CAN_MANAGE_FOREIGN_RELATIONS',
93 1
            $this->allianceActionManager->mayManageForeignRelations($alliance, $user)
94 1
        );
95 1
        $game->setTemplateVar(
96 1
            'CAN_SIGNUP',
97 1
            $this->allianceUserApplicationChecker->mayApply($user, $alliance)
98 1
        );
99
100 1
        $game->setTemplateVar(
101 1
            'MEMBERS',
102 1
            $alliance->getMembers()->map(
103 1
                fn(UserInterface $user): AllianceMemberWrapper => $this->allianceUiFactory->createAllianceMemberWrapper($user, $alliance)
104 1
            )
105 1
        );
106
    }
107
108
    private function setTemplateVariablesForAllianceList(GameControllerInterface $game): void
109
    {
110
        $game->appendNavigationPart(sprintf(
111
            '%s?showlist=1',
112
            ModuleEnum::ALLIANCE->getPhpPage()
113
        ), _('Allianzliste'));
114
115
        $game->setTemplateVar('SHOW_ALLIANCE_LIST', true);
116
        $game->setTemplateVar(
117
            'ALLIANCE_LIST_OPEN',
118
            array_map(
119
                fn(AllianceInterface $alliance): AllianceListItem => $this->allianceUiFactory->createAllianceListItem($alliance),
120
                $this->allianceRepository->findByApplicationState(true)
121
            )
122
        );
123
        $game->setTemplateVar(
124
            'ALLIANCE_LIST_CLOSED',
125
            array_map(
126
                fn(AllianceInterface $alliance): AllianceListItem => $this->allianceUiFactory->createAllianceListItem($alliance),
127
                $this->allianceRepository->findByApplicationState(false)
128
            )
129
        );
130
    }
131
}
132