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

ShowManagement::handle()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 27
c 0
b 0
f 0
nc 11
nop 1
dl 0
loc 49
ccs 0
cts 29
cp 0
crap 132
rs 7.3166

How to fix   Complexity   

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\ShowManagement;
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\Lib\Colony\PlanetFieldHostProviderInterface;
13
use Stu\Module\Colony\Lib\ColonyGuiHelperInterface;
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\Entity\ColonyInterface;
19
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
20
21
final class ShowManagement implements ViewControllerInterface
22
{
23
    public const VIEW_IDENTIFIER = 'SHOW_MANAGEMENT';
24
25
    private PlanetFieldHostProviderInterface $planetFieldHostProvider;
26
27
    private ColonyGuiHelperInterface $colonyGuiHelper;
28
29
    private TorpedoTypeRepositoryInterface $torpedoTypeRepository;
30
31
    private DatabaseCategoryTalFactoryInterface $databaseCategoryTalFactory;
32
33
    private OrbitShipListRetrieverInterface $orbitShipListRetriever;
34
35
    private ShipWrapperFactoryInterface $shipWrapperFactory;
36
37
    private ColonyFunctionManagerInterface $colonyFunctionManager;
38
39
    public function __construct(
40
        PlanetFieldHostProviderInterface $planetFieldHostProvider,
41
        ColonyGuiHelperInterface $colonyGuiHelper,
42
        TorpedoTypeRepositoryInterface $torpedoTypeRepository,
43
        DatabaseCategoryTalFactoryInterface $databaseCategoryTalFactory,
44
        OrbitShipListRetrieverInterface $orbitShipListRetriever,
45
        ColonyFunctionManagerInterface $colonyFunctionManager,
46
        ShipWrapperFactoryInterface $shipWrapperFactory
47
    ) {
48
        $this->planetFieldHostProvider = $planetFieldHostProvider;
49
        $this->colonyGuiHelper = $colonyGuiHelper;
50
        $this->torpedoTypeRepository = $torpedoTypeRepository;
51
        $this->databaseCategoryTalFactory = $databaseCategoryTalFactory;
52
        $this->orbitShipListRetriever = $orbitShipListRetriever;
53
        $this->shipWrapperFactory = $shipWrapperFactory;
54
        $this->colonyFunctionManager = $colonyFunctionManager;
55
    }
56
57
    public function handle(GameControllerInterface $game): void
58
    {
59
        $userId = $game->getUser()->getId();
60
61
        $host = $this->planetFieldHostProvider->loadHostViaRequestParameters($game->getUser());
62
63
        $this->colonyGuiHelper->registerComponents($host, $game);
64
        $game->setTemplateVar('CURRENT_MENU', ColonyMenuEnum::MENU_INFO);
65
        $game->showMacro(ColonyMenuEnum::MENU_INFO->getTemplate());
66
67
        if (!$host instanceof ColonyInterface) {
68
            return;
69
        }
70
71
        $systemDatabaseEntry = $host->getSystem()->getDatabaseEntry();
72
        if ($systemDatabaseEntry !== null) {
73
            $starsystem = $this->databaseCategoryTalFactory->createDatabaseCategoryEntryTal($systemDatabaseEntry, $game->getUser());
74
            $game->setTemplateVar('STARSYSTEM_ENTRY_TAL', $starsystem);
75
        }
76
77
        $firstOrbitShip = null;
78
79
        $shipList = $this->orbitShipListRetriever->retrieve($host);
80
        if ($shipList !== []) {
81
            // if selected, return the current target
82
            $target = request::postInt('target');
83
84
            if ($target !== 0) {
85
                foreach ($shipList as $fleet) {
86
                    foreach ($fleet['ships'] as $idx => $ship) {
87
                        if ($idx == $target) {
88
                            $firstOrbitShip = $ship;
89
                        }
90
                    }
91
                }
92
            }
93
            if ($firstOrbitShip === null) {
94
                $firstOrbitShip = current(current($shipList)['ships']);
95
            }
96
        }
97
98
99
        $game->setTemplateVar(
100
            'FIRST_ORBIT_SHIP',
101
            $firstOrbitShip ? $this->shipWrapperFactory->wrapShip($firstOrbitShip) : null
102
        );
103
104
        $particlePhalanx = $this->colonyFunctionManager->hasFunction($host, BuildingEnum::BUILDING_FUNCTION_PARTICLE_PHALANX);
105
        $game->setTemplateVar('BUILDABLE_TORPEDO_TYPES', $particlePhalanx ? $this->torpedoTypeRepository->getForUser($userId) : null);
106
    }
107
}
108