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

ShowOrbitManagement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
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