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

ShowOrbitManagement::handle()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 62
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 40
c 1
b 1
f 0
nc 10
nop 1
dl 0
loc 62
ccs 0
cts 48
cp 0
crap 30
rs 8.9688

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
        $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