Passed
Pull Request — master (#1699)
by Nico
43:19 queued 18:45
created

MaindeskProvider::setTemplateVariables()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 105
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 64
nc 2
nop 1
dl 0
loc 105
ccs 0
cts 81
cp 0
crap 6
rs 8.7853
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 Stu\Component\Colony\ColonyTypeEnum;
8
use Stu\Component\Communication\Kn\KnFactoryInterface;
9
use Stu\Component\Communication\Kn\KnItemInterface;
10
use Stu\Component\Crew\CrewCountRetrieverInterface;
11
use Stu\Component\Game\GameEnum;
12
use Stu\Component\Player\ColonyLimitCalculatorInterface;
13
use Stu\Component\Player\CrewLimitCalculatorInterface;
14
use Stu\Component\Player\PlayerRelationDeterminatorInterface;
15
use Stu\Module\Control\GameControllerInterface;
16
use Stu\Module\PlayerSetting\Lib\UserEnum;
17
use Stu\Module\Ship\Lib\EmergencyWrapper;
18
use Stu\Orm\Entity\KnPostInterface;
19
use Stu\Orm\Repository\AllianceBoardTopicRepositoryInterface;
20
use Stu\Orm\Repository\ColonyShipQueueRepositoryInterface;
21
use Stu\Orm\Repository\HistoryRepositoryInterface;
22
use Stu\Orm\Repository\KnPostRepositoryInterface;
23
use Stu\Orm\Repository\ShipyardShipQueueRepositoryInterface;
24
use Stu\Orm\Repository\SpacecraftEmergencyRepositoryInterface;
25
use Stu\Orm\Repository\UserProfileVisitorRepositoryInterface;
26
use Stu\Orm\Repository\UserRepositoryInterface;
27
28
final class MaindeskProvider implements ViewComponentProviderInterface
29
{
30
    private HistoryRepositoryInterface $historyRepository;
31
32
    private AllianceBoardTopicRepositoryInterface $allianceBoardTopicRepository;
33
34
    private UserProfileVisitorRepositoryInterface $userProfileVisitorRepository;
35
36
    private KnPostRepositoryInterface $knPostRepository;
37
38
    private ColonyShipQueueRepositoryInterface $colonyShipQueueRepository;
39
40
    private ShipyardShipQueueRepositoryInterface $shipyardShipQueueRepository;
41
42
    private UserRepositoryInterface $userRepository;
43
44
    private SpacecraftEmergencyRepositoryInterface $spacecraftEmergencyRepository;
45
46
    private KnFactoryInterface $knFactory;
47
48
    private ColonyLimitCalculatorInterface $colonyLimitCalculator;
49
50
    private CrewCountRetrieverInterface $crewCountRetriever;
51
52
    private PlayerRelationDeterminatorInterface $playerRelationDeterminator;
53
54
    private CrewLimitCalculatorInterface $crewLimitCalculator;
55
56
    public function __construct(
57
        HistoryRepositoryInterface $historyRepository,
58
        AllianceBoardTopicRepositoryInterface $allianceBoardTopicRepository,
59
        UserProfileVisitorRepositoryInterface $userProfileVisitorRepository,
60
        KnPostRepositoryInterface $knPostRepository,
61
        ColonyShipQueueRepositoryInterface $colonyShipQueueRepository,
62
        ShipyardShipQueueRepositoryInterface $shipyardShipQueueRepository,
63
        UserRepositoryInterface $userRepository,
64
        SpacecraftEmergencyRepositoryInterface $spacecraftEmergencyRepository,
65
        KnFactoryInterface $knFactory,
66
        ColonyLimitCalculatorInterface $colonyLimitCalculator,
67
        PlayerRelationDeterminatorInterface $playerRelationDeterminator,
68
        CrewLimitCalculatorInterface $crewLimitCalculator,
69
        CrewCountRetrieverInterface $crewCountRetriever
70
    ) {
71
        $this->historyRepository = $historyRepository;
72
        $this->allianceBoardTopicRepository = $allianceBoardTopicRepository;
73
        $this->userProfileVisitorRepository = $userProfileVisitorRepository;
74
        $this->knPostRepository = $knPostRepository;
75
        $this->colonyShipQueueRepository = $colonyShipQueueRepository;
76
        $this->shipyardShipQueueRepository = $shipyardShipQueueRepository;
77
        $this->userRepository = $userRepository;
78
        $this->spacecraftEmergencyRepository = $spacecraftEmergencyRepository;
79
        $this->knFactory = $knFactory;
80
        $this->colonyLimitCalculator = $colonyLimitCalculator;
81
        $this->crewCountRetriever = $crewCountRetriever;
82
        $this->playerRelationDeterminator = $playerRelationDeterminator;
83
        $this->crewLimitCalculator = $crewLimitCalculator;
84
    }
85
86
    public function setTemplateVariables(GameControllerInterface $game): void
87
    {
88
        $user = $game->getUser();
89
        $userId = $user->getId();
90
91
        $game->setTemplateVar(
92
            'DISPLAY_FIRST_COLONY_DIALOGUE',
93
            $user->getState() === UserEnum::USER_STATE_UNCOLONIZED
94
        );
95
96
        $game->setTemplateVar(
97
            'DISPLAY_COLONIZATION_SHIP_DIALOGUE',
98
            $user->getState() === UserEnum::USER_STATE_COLONIZATION_SHIP
99
        );
100
101
        $game->setTemplateVar(
102
            'DISPLAY_TUTORIAL_1',
103
            $user->getState() === UserEnum::USER_STATE_TUTORIAL1
104
        );
105
106
        $game->setTemplateVar(
107
            'DISPLAY_TUTORIAL_2',
108
            $user->getState() === UserEnum::USER_STATE_TUTORIAL2
109
        );
110
111
        $game->setTemplateVar(
112
            'DISPLAY_TUTORIAL_3',
113
            $user->getState() === UserEnum::USER_STATE_TUTORIAL3
114
        );
115
116
        $game->setTemplateVar(
117
            'DISPLAY_TUTORIAL_4',
118
            $user->getState() === UserEnum::USER_STATE_TUTORIAL4
119
        );
120
121
        $newAmount = $this->knPostRepository->getAmountSince($user->getKNMark());
122
123
        $game->setTemplateVar(
124
            'NEW_KN_POSTING_COUNT',
125
            $newAmount
126
        );
127
        $game->setTemplateVar(
128
            'NEW_KN_POSTINGS',
129
            array_map(
130
                function (KnPostInterface $knPost) use ($user, $newAmount): KnItemInterface {
131
                    $newAmount--;
132
                    $knItem = $this->knFactory->createKnItem(
133
                        $knPost,
134
                        $user
135
                    );
136
                    $knItem->setMark(((int)floor($newAmount / GameEnum::KN_PER_SITE)) * 6);
137
                    return $knItem;
138
                },
139
                $this->knPostRepository->getNewerThenMark($user->getKNMark())
140
            )
141
        );
142
        $game->setTemplateVar(
143
            'RECENT_PROFILE_VISITORS',
144
            $this->userProfileVisitorRepository->getRecent($userId)
145
        );
146
        $game->setTemplateVar(
147
            'RANDOM_ONLINE_USER',
148
            $this->userRepository->getOrderedByLastaction(15, $userId, time() - GameEnum::USER_ONLINE_PERIOD)
149
        );
150
        $game->setTemplateVar(
151
            'SHIP_BUILD_PROGRESS',
152
            [...$this->colonyShipQueueRepository->getByUser($userId), ...$this->shipyardShipQueueRepository->getByUser($userId)]
153
        );
154
155
        $alliance = $user->getAlliance();
156
        if ($alliance !== null) {
157
            $game->setTemplateVar('ALLIANCE', true);
158
159
            $game->setTemplateVar(
160
                'RECENT_ALLIANCE_BOARD_TOPICS',
161
                $this->allianceBoardTopicRepository->getRecentByAlliance($alliance->getId())
162
            );
163
        }
164
165
        $game->setTemplateVar('RECENT_HISTORY', $this->historyRepository->getRecent());
166
167
        //emergencies
168
        $this->setPotentialEmergencies($game);
169
170
        //planet
171
        $game->setTemplateVar('PLANET_LIMIT', $this->colonyLimitCalculator->getColonyLimitWithType($user, ColonyTypeEnum::COLONY_TYPE_PLANET));
172
        $game->setTemplateVar('PLANET_COUNT', $this->colonyLimitCalculator->getColonyCountWithType($user, ColonyTypeEnum::COLONY_TYPE_PLANET));
173
174
        //moon
175
        $game->setTemplateVar('MOON_LIMIT', $this->colonyLimitCalculator->getColonyLimitWithType($user, ColonyTypeEnum::COLONY_TYPE_MOON));
176
        $game->setTemplateVar('MOON_COUNT', $this->colonyLimitCalculator->getColonyCountWithType($user, ColonyTypeEnum::COLONY_TYPE_MOON));
177
178
        //asteroid
179
        $game->setTemplateVar('ASTEROID_LIMIT', $this->colonyLimitCalculator->getColonyLimitWithType($user, ColonyTypeEnum::COLONY_TYPE_ASTEROID));
180
        $game->setTemplateVar('ASTEROID_COUNT', $this->colonyLimitCalculator->getColonyCountWithType($user, ColonyTypeEnum::COLONY_TYPE_ASTEROID));
181
182
        $game->setTemplateVar(
183
            'CREW_LIMIT',
184
            $this->crewLimitCalculator->getGlobalCrewLimit($user)
185
        );
186
187
        // crew count
188
        $game->setTemplateVar(
189
            'CREW_COUNT_SHIPS',
190
            $this->crewCountRetriever->getAssignedToShipsCount($user)
191
        );
192
    }
193
194
    private function setPotentialEmergencies(GameControllerInterface $game): void
195
    {
196
        $emergencies = $this->spacecraftEmergencyRepository->getActive();
197
198
        if (empty($emergencies)) {
199
            return;
200
        }
201
202
        $emergencyWrappers = [];
203
204
        foreach ($emergencies as $emergency) {
205
            $emergencyWrappers[] = new EmergencyWrapper($this->playerRelationDeterminator, $emergency, $game->getUser());
206
        }
207
208
        $game->setTemplateVar('EMERGENCYWRAPPERS', $emergencyWrappers);
209
    }
210
}
211