Passed
Push — master ( f544cb...b3a3d9 )
by Nico
36:43 queued 09:10
created

ShowColony::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 18
ccs 0
cts 9
cp 0
crap 2
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\View\ShowColony;
6
7
use request;
8
use Stu\Component\Building\BuildingEnum;
9
use Stu\Component\Colony\ColonyFunctionManagerInterface;
10
use Stu\Component\Colony\ColonyMenuEnum;
11
use Stu\Component\Colony\OrbitShipListRetrieverInterface;
12
use Stu\Module\Colony\Lib\ColonyGuiHelperInterface;
13
use Stu\Module\Colony\Lib\ColonyLoaderInterface;
14
use Stu\Module\Control\GameControllerInterface;
15
use Stu\Module\Control\ViewControllerInterface;
16
use Stu\Module\Database\View\Category\Tal\DatabaseCategoryTalFactoryInterface;
17
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
18
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
19
20
final class ShowColony implements ViewControllerInterface
21
{
22
    public const VIEW_IDENTIFIER = 'SHOW_COLONY';
23
24
    private ColonyLoaderInterface $colonyLoader;
25
26
    private ColonyGuiHelperInterface $colonyGuiHelper;
27
28
    private ShowColonyRequestInterface $showColonyRequest;
29
30
    private DatabaseCategoryTalFactoryInterface $databaseCategoryTalFactory;
31
32
    private TorpedoTypeRepositoryInterface $torpedoTypeRepository;
33
34
    private ShipWrapperFactoryInterface $shipWrapperFactory;
35
36
    private OrbitShipListRetrieverInterface $orbitShipListRetriever;
37
38
    private ColonyFunctionManagerInterface $colonyFunctionManager;
39
40
    public function __construct(
41
        ColonyLoaderInterface $colonyLoader,
42
        ColonyGuiHelperInterface $colonyGuiHelper,
43
        ShowColonyRequestInterface $showColonyRequest,
44
        TorpedoTypeRepositoryInterface $torpedoTypeRepository,
45
        DatabaseCategoryTalFactoryInterface $databaseCategoryTalFactory,
46
        OrbitShipListRetrieverInterface $orbitShipListRetriever,
47
        ColonyFunctionManagerInterface $colonyFunctionManager,
48
        ShipWrapperFactoryInterface $shipWrapperFactory
49
    ) {
50
        $this->colonyLoader = $colonyLoader;
51
        $this->colonyGuiHelper = $colonyGuiHelper;
52
        $this->showColonyRequest = $showColonyRequest;
53
        $this->databaseCategoryTalFactory = $databaseCategoryTalFactory;
54
        $this->torpedoTypeRepository = $torpedoTypeRepository;
55
        $this->shipWrapperFactory = $shipWrapperFactory;
56
        $this->orbitShipListRetriever = $orbitShipListRetriever;
57
        $this->colonyFunctionManager = $colonyFunctionManager;
58
    }
59
60
    public function handle(GameControllerInterface $game): void
61
    {
62
        $user = $game->getUser();
63
        $userId = $user->getId();
64
65
        $colony = $this->colonyLoader->byIdAndUser(
66
            $this->showColonyRequest->getColonyId(),
67
            $userId,
68
            false
69
        );
70
71
        $menu = ColonyMenuEnum::getFor($game->getViewContext()['COLONY_MENU'] ?? null);
72
        $this->colonyGuiHelper->registerComponents($colony, $game);
73
        $game->setTemplateVar('CURRENT_MENU', $menu);
74
75
        $firstOrbitShip = null;
76
77
        $shipList = $this->orbitShipListRetriever->retrieve($colony);
78
        if ($shipList !== []) {
79
            // if selected, return the current target
80
            $target = request::indInt('target');
81
82
            if ($target !== 0) {
83
                foreach ($shipList as $fleet) {
84
                    foreach ($fleet['ships'] as $idx => $ship) {
85
                        if ($idx == $target) {
86
                            $firstOrbitShip = $ship;
87
                        }
88
                    }
89
                }
90
            }
91
            if ($firstOrbitShip === null) {
92
                $firstOrbitShip = current(current($shipList)['ships']);
93
            }
94
        }
95
96
        $game->appendNavigationPart(
97
            'colony.php',
98
            _('Kolonien')
99
        );
100
        $game->appendNavigationPart(
101
            sprintf('?%s=1&id=%d', static::VIEW_IDENTIFIER, $colony->getId()),
102
            $colony->getName()
103
        );
104
        $game->setTemplateFile('html/colony/colony.twig');
105
        $game->setPagetitle(sprintf(_('Kolonie: %s'), $colony->getName()));
106
107
108
        $game->setTemplateVar('SELECTED_COLONY_MENU_TEMPLATE', $menu->getTemplate());
109
110
        $starsystem = $this->databaseCategoryTalFactory->createDatabaseCategoryEntryTal($colony->getSystem()->getDatabaseEntry(), $user);
0 ignored issues
show
Bug introduced by
It seems like $colony->getSystem()->getDatabaseEntry() can also be of type null; however, parameter $databaseEntry of Stu\Module\Database\View...abaseCategoryEntryTal() does only seem to accept Stu\Orm\Entity\DatabaseEntryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        $starsystem = $this->databaseCategoryTalFactory->createDatabaseCategoryEntryTal(/** @scrutinizer ignore-type */ $colony->getSystem()->getDatabaseEntry(), $user);
Loading history...
111
        $game->setTemplateVar('STARSYSTEM_ENTRY_TAL', $starsystem);
112
113
        $game->setTemplateVar('FIRST_ORBIT_SHIP', $firstOrbitShip ? $this->shipWrapperFactory->wrapShip($firstOrbitShip) : null);
114
115
        $particlePhalanx = $this->colonyFunctionManager->hasFunction($colony, BuildingEnum::BUILDING_FUNCTION_PARTICLE_PHALANX);
116
        $game->setTemplateVar(
117
            'BUILDABLE_TORPEDO_TYPES',
118
            $particlePhalanx ? $this->torpedoTypeRepository->getForUser($userId) : null
119
        );
120
    }
121
}
122