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

ShowManagement   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 43
c 0
b 0
f 0
dl 0
loc 85
ccs 0
cts 37
cp 0
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
B handle() 0 49 11
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