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

ShowOrbitManagement::handle()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 64
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 39
nc 10
nop 1
dl 0
loc 64
ccs 0
cts 47
cp 0
crap 30
rs 8.9848
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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