Passed
Push — dev ( 2d398e...250fad )
by Janko
12:46
created

MaindeskProvider::setTemplateVariables()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 98
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 73
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 62
nc 8
nop 1
dl 0
loc 98
ccs 73
cts 74
cp 0.9865
crap 4
rs 8.829
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 Stu\Component\Colony\ColonyTypeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Colony\ColonyTypeEnum 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...
9
use Stu\Component\Communication\Kn\KnFactoryInterface;
10
use Stu\Component\Communication\Kn\KnItemInterface;
11
use Stu\Component\Crew\CrewCountRetrieverInterface;
12
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...
13
use Stu\Component\Player\ColonyLimitCalculatorInterface;
14
use Stu\Component\Player\CrewLimitCalculatorInterface;
15
use Stu\Component\Player\Relation\PlayerRelationDeterminatorInterface;
16
use Stu\Module\Control\GameControllerInterface;
17
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
18
use Stu\Module\Spacecraft\Lib\EmergencyWrapper;
19
use Stu\Orm\Entity\KnPostInterface;
20
use Stu\Orm\Repository\AllianceBoardTopicRepositoryInterface;
21
use Stu\Orm\Repository\ColonyShipQueueRepositoryInterface;
22
use Stu\Orm\Repository\HistoryRepositoryInterface;
23
use Stu\Orm\Repository\KnPostRepositoryInterface;
24
use Stu\Orm\Repository\ShipyardShipQueueRepositoryInterface;
25
use Stu\Orm\Repository\SpacecraftEmergencyRepositoryInterface;
26
use Stu\Orm\Repository\UserProfileVisitorRepositoryInterface;
27
use Stu\Orm\Repository\UserRepositoryInterface;
28
29
final class MaindeskProvider implements ViewComponentProviderInterface
30
{
31 1
    public function __construct(private HistoryRepositoryInterface $historyRepository, private AllianceBoardTopicRepositoryInterface $allianceBoardTopicRepository, private UserProfileVisitorRepositoryInterface $userProfileVisitorRepository, private KnPostRepositoryInterface $knPostRepository, private ColonyShipQueueRepositoryInterface $colonyShipQueueRepository, private ShipyardShipQueueRepositoryInterface $shipyardShipQueueRepository, private UserRepositoryInterface $userRepository, private SpacecraftEmergencyRepositoryInterface $spacecraftEmergencyRepository, private KnFactoryInterface $knFactory, private ColonyLimitCalculatorInterface $colonyLimitCalculator, private PlayerRelationDeterminatorInterface $playerRelationDeterminator, private CrewLimitCalculatorInterface $crewLimitCalculator, private CrewCountRetrieverInterface $crewCountRetriever) {}
32
33 1
    #[Override]
34
    public function setTemplateVariables(GameControllerInterface $game): void
35
    {
36 1
        $user = $game->getUser();
37 1
        $userId = $user->getId();
38
39 1
        $game->setTemplateVar(
40 1
            'DISPLAY_FIRST_COLONY_DIALOGUE',
41 1
            $user->getState() === UserEnum::USER_STATE_UNCOLONIZED
42 1
        );
43
44 1
        $game->setTemplateVar(
45 1
            'DISPLAY_COLONIZATION_SHIP_DIALOGUE',
46 1
            $user->getState() === UserEnum::USER_STATE_COLONIZATION_SHIP
47 1
        );
48
49 1
        $newAmount = $this->knPostRepository->getAmountSince($user->getKnMark());
50
51 1
        $game->setTemplateVar(
52 1
            'NEW_KN_POSTING_COUNT',
53 1
            $newAmount
54 1
        );
55 1
        $newKnPostings = $this->knPostRepository->getNewerThenMark($user->getKnMark());
56 1
        if ($newKnPostings !== []) {
57 1
            $game->setTemplateVar('MARKED_KN_ID', $newKnPostings[0]->getId());
58
        }
59 1
        $game->setTemplateVar(
60 1
            'NEW_KN_POSTINGS',
61 1
            array_map(
62 1
                function (KnPostInterface $knPost) use ($user, $newAmount): KnItemInterface {
63 1
                    $newAmount--;
64 1
                    $knItem = $this->knFactory->createKnItem(
65 1
                        $knPost,
66 1
                        $user
67 1
                    );
68 1
                    $knItem->setMark(((int)floor($newAmount / GameEnum::KN_PER_SITE)) * 6);
69 1
                    return $knItem;
70 1
                },
71 1
                $newKnPostings
72 1
            )
73 1
        );
74 1
        $game->setTemplateVar(
75 1
            'RECENT_PROFILE_VISITORS',
76 1
            $this->userProfileVisitorRepository->getRecent($userId)
77 1
        );
78 1
        $game->setTemplateVar(
79 1
            'RANDOM_ONLINE_USER',
80 1
            $this->userRepository->getOrderedByLastaction(35, $userId, time() - GameEnum::USER_ONLINE_PERIOD)
81 1
        );
82 1
        $game->setTemplateVar(
83 1
            'SHIP_BUILD_PROGRESS',
84 1
            [...$this->colonyShipQueueRepository->getByUserAndMode($userId, 1), ...$this->shipyardShipQueueRepository->getByUser($userId)]
85 1
        );
86 1
        $game->setTemplateVar(
87 1
            'SHIP_RETROFIT_PROGRESS',
88 1
            [...$this->colonyShipQueueRepository->getByUserAndMode($userId, 2)]
89 1
        );
90
91 1
        $alliance = $user->getAlliance();
92 1
        if ($alliance !== null) {
93 1
            $game->setTemplateVar('ALLIANCE', true);
94
95 1
            $game->setTemplateVar(
96 1
                'RECENT_ALLIANCE_BOARD_TOPICS',
97 1
                $this->allianceBoardTopicRepository->getRecentByAlliance($alliance->getId())
98 1
            );
99
        }
100
101 1
        if ($user->isShowPirateHistoryEntrys()) {
102
            $game->setTemplateVar('RECENT_HISTORY', $this->historyRepository->getRecent());
103
        } else {
104 1
            $game->setTemplateVar('RECENT_HISTORY', $this->historyRepository->getRecentWithoutPirate());
105
        }
106
107
        //emergencies
108 1
        $this->setPotentialEmergencies($game);
109
110
        //planet
111 1
        $game->setTemplateVar('PLANET_LIMIT', $this->colonyLimitCalculator->getColonyLimitWithType($user, ColonyTypeEnum::COLONY_TYPE_PLANET));
112 1
        $game->setTemplateVar('PLANET_COUNT', $this->colonyLimitCalculator->getColonyCountWithType($user, ColonyTypeEnum::COLONY_TYPE_PLANET));
113
114
        //moon
115 1
        $game->setTemplateVar('MOON_LIMIT', $this->colonyLimitCalculator->getColonyLimitWithType($user, ColonyTypeEnum::COLONY_TYPE_MOON));
116 1
        $game->setTemplateVar('MOON_COUNT', $this->colonyLimitCalculator->getColonyCountWithType($user, ColonyTypeEnum::COLONY_TYPE_MOON));
117
118
        //asteroid
119 1
        $game->setTemplateVar('ASTEROID_LIMIT', $this->colonyLimitCalculator->getColonyLimitWithType($user, ColonyTypeEnum::COLONY_TYPE_ASTEROID));
120 1
        $game->setTemplateVar('ASTEROID_COUNT', $this->colonyLimitCalculator->getColonyCountWithType($user, ColonyTypeEnum::COLONY_TYPE_ASTEROID));
121
122 1
        $game->setTemplateVar(
123 1
            'CREW_LIMIT',
124 1
            $this->crewLimitCalculator->getGlobalCrewLimit($user)
125 1
        );
126
127
        // crew count
128 1
        $game->setTemplateVar(
129 1
            'CREW_COUNT_SHIPS',
130 1
            $this->crewCountRetriever->getAssignedToShipsCount($user)
131 1
        );
132
    }
133
134 1
    private function setPotentialEmergencies(GameControllerInterface $game): void
135
    {
136 1
        $emergencies = $this->spacecraftEmergencyRepository->getActive();
137
138 1
        if ($emergencies === []) {
139 1
            return;
140
        }
141
142
        $emergencyWrappers = [];
143
144
        foreach ($emergencies as $emergency) {
145
            $emergencyWrappers[] = new EmergencyWrapper($this->playerRelationDeterminator, $emergency, $game->getUser());
146
        }
147
148
        $game->setTemplateVar('EMERGENCYWRAPPERS', $emergencyWrappers);
149
    }
150
}
151