Passed
Push — master ( 784be2...c4713a )
by Nico
58:15 queued 29:26
created

ShowOrbitManagement   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 50
c 1
b 1
f 0
dl 0
loc 86
ccs 0
cts 53
cp 0
rs 10
wmc 6

2 Methods

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