Passed
Push — dev ( a5a844...c2033c )
by Janko
07:10
created

ShowOrbitManagement   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 88
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B handle() 0 64 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
        /**
55
         * @var array<int, array<ShipInterface>>
56
         */
57
        $groupedList = [];
58
59
        foreach ($shipList as $ship) {
60
            $fleetId = $ship->getFleetId() === null ? 0 : $ship->getFleetId();
61
62
            $fleet = $groupedList[$fleetId] ?? null;
63
            if ($fleet === null) {
64
                $groupedList[$fleetId] = [];
65
            }
66
67
            $groupedList[$fleetId][] = $ship;
68
        }
69
70
        $list = [];
71
72
        foreach ($groupedList as $fleetId => $shipList) {
73
            $fleetWrapper = $this->shipWrapperFactory->wrapShipsAsFleet($shipList, $fleetId === 0);
74
            $key = sprintf('%d.%d', $fleetWrapper->get()->getSort(), $fleetWrapper->get()->getUser()->getId());
75
            $list[$key] = $fleetWrapper;
76
        }
77
78
        $game->appendNavigationPart(
79
            'colony.php',
80
            _('Kolonien')
81
        );
82
        $game->appendNavigationPart(
83
            sprintf(
84
                '?%s=1&id=%s',
85
                ShowColony::VIEW_IDENTIFIER,
86
                $colony->getId()
87
            ),
88
            $colony->getName()
89
        );
90
        $game->appendNavigationPart(
91
            sprintf(
92
                '?%s=1&id=%d',
93
                static::VIEW_IDENTIFIER,
94
                $colony->getId()
95
            ),
96
            _('Orbitalmanagement')
97
        );
98
        $game->setPagetitle(sprintf('%s Orbit', $colony->getName()));
99
        $game->setTemplateFile('html/orbitalmanagement.xhtml');
100
101
        $game->setTemplateVar('COLONY', $colony);
102
        $game->setTemplateVar('FLEETWRAPPERS', $list);
103
    }
104
}
105