Passed
Pull Request — master (#1825)
by Nico
59:43 queued 25:38
created

ShowOrbitManagement   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 83
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A handle() 0 59 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\View\ShowOrbitManagement;
6
7
use Stu\Module\Colony\Lib\ColonyLoaderInterface;
8
use Stu\Module\Colony\View\ShowColony\ShowColony;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\Control\ViewControllerInterface;
11
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
12
use Stu\Orm\Repository\ShipRepositoryInterface;
13
14
final class ShowOrbitManagement implements ViewControllerInterface
15
{
16
    public const VIEW_IDENTIFIER = 'SHOW_SHIP_MANAGEMENT';
17
18
    private ColonyLoaderInterface $colonyLoader;
19
20
    private ShowOrbitManagementRequestInterface $showOrbitManagementRequest;
21
22
    private ShipRepositoryInterface $shipRepository;
23
24
    private ShipWrapperFactoryInterface $shipWrapperFactory;
25
26
    public function __construct(
27
        ColonyLoaderInterface $colonyLoader,
28
        ShowOrbitManagementRequestInterface $showOrbitManagementRequest,
29
        ShipRepositoryInterface $shipRepository,
30
        ShipWrapperFactoryInterface $shipWrapperFactory
31
    ) {
32
        $this->colonyLoader = $colonyLoader;
33
        $this->showOrbitManagementRequest = $showOrbitManagementRequest;
34
        $this->shipRepository = $shipRepository;
35
        $this->shipWrapperFactory = $shipWrapperFactory;
36
    }
37
38
    public function handle(GameControllerInterface $game): void
39
    {
40
        $userId = $game->getUser()->getId();
41
42
        $colony = $this->colonyLoader->loadWithOwnerValidation(
43
            $this->showOrbitManagementRequest->getColonyId(),
44
            $userId,
45
            false
46
        );
47
48
        $shipList = $this->shipRepository->getByLocation($colony->getStarsystemMap());
49
50
        $groupedList = [];
51
52
        foreach ($shipList as $ship) {
53
            $fleet = $ship->getFleet();
54
            $fleetId = $fleet === null ? 0 : $fleet->getId();
55
56
            $fleet = $groupedList[$fleetId] ?? null;
57
            if ($fleet === null) {
58
                $groupedList[$fleetId] = [];
59
            }
60
61
            $groupedList[$fleetId][] = $ship;
62
        }
63
64
        $list = [];
65
66
        foreach ($groupedList as $fleetId => $shipList) {
67
            $fleetWrapper = $this->shipWrapperFactory->wrapShipsAsFleet($shipList, $fleetId === 0);
68
            $key = sprintf('%d.%d', $fleetWrapper->get()->getSort(), $fleetWrapper->get()->getUser()->getId());
69
            $list[$key] = $fleetWrapper;
70
        }
71
72
        $game->appendNavigationPart(
73
            'colony.php',
74
            _('Kolonien')
75
        );
76
        $game->appendNavigationPart(
77
            sprintf(
78
                '?%s=1&id=%s',
79
                ShowColony::VIEW_IDENTIFIER,
80
                $colony->getId()
81
            ),
82
            $colony->getName()
83
        );
84
        $game->appendNavigationPart(
85
            sprintf(
86
                '?%s=1&id=%d',
87
                static::VIEW_IDENTIFIER,
88
                $colony->getId()
89
            ),
90
            _('Orbitalmanagement')
91
        );
92
        $game->setPagetitle(sprintf('%s Orbit', $colony->getName()));
93
        $game->setViewTemplate('html/colony/menu/orbitalmanagement.twig');
94
95
        $game->setTemplateVar('MANAGER_ID', $colony->getId());
96
        $game->setTemplateVar('FLEET_WRAPPERS', $list);
97
    }
98
}
99