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

ShowColony::handle()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 36
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 59
ccs 0
cts 40
cp 0
crap 90
rs 8.0555

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\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