Test Failed
Push — dev ( 6e2bec...ec3020 )
by Nico
11:28
created

ShowSectorScan::handle()   B

Complexity

Conditions 11
Paths 18

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 11
eloc 35
c 3
b 1
f 0
nc 18
nop 1
dl 0
loc 54
ccs 0
cts 34
cp 0
crap 132
rs 7.3166

How to fix   Long Method    Complexity   

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\Ship\View\ShowSectorScan;
6
7
use request;
8
use Stu\Component\Map\EncodedMapInterface;
9
use Stu\Component\Ship\FlightSignatureVisibilityEnum;
10
use Stu\Lib\SignatureWrapper;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Control\ViewControllerInterface;
13
use Stu\Module\Ship\Lib\ShipLoaderInterface;
14
use Stu\Orm\Entity\MapInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
use Stu\Orm\Repository\FlightSignatureRepositoryInterface;
17
use Stu\Orm\Repository\BuoyRepositoryInterface;
18
19
final class ShowSectorScan implements ViewControllerInterface
20
{
21
    public const VIEW_IDENTIFIER = 'SHOW_SECTOR_SCAN';
22
23
    private ShipLoaderInterface $shipLoader;
24
25
    private FlightSignatureRepositoryInterface $flightSignatureRepository;
26
27
    private EncodedMapInterface $encodedMap;
28
29
    private BuoyRepositoryInterface $buoyRepository;
30
31
    /** @var array<int> */
32
    private array $fadedSignaturesUncloaked = [];
33
34
    /** @var array<int> */
35
    private array $fadedSignaturesCloaked = [];
36
37
    public function __construct(
38
        ShipLoaderInterface $shipLoader,
39
        FlightSignatureRepositoryInterface $flightSignatureRepository,
40
        EncodedMapInterface $encodedMap,
41
        BuoyRepositoryInterface $buoyRepository
42
    ) {
43
        $this->shipLoader = $shipLoader;
44
        $this->flightSignatureRepository = $flightSignatureRepository;
45
        $this->encodedMap = $encodedMap;
46
        $this->buoyRepository = $buoyRepository;
47
    }
48
49
    public function handle(GameControllerInterface $game): void
50
    {
51
        $userId = $game->getUser()->getId();
52
53
        $buoy = null;
54
55
        $game->setPageTitle("Sektor Scan");
56
        $game->setMacroInAjaxWindow('html/shipmacros.xhtml/sectorscan');
57
        $game->setTemplateVar('ERROR', true);
58
59
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
60
            request::indInt('id'),
61
            $userId,
62
            true
63
        );
64
        $ship = $wrapper->get();
65
66
        if (!$ship->getNbs()) {
67
            $game->addInformation("Die Nahbereichssensoren sind nicht aktiv");
68
            return;
69
        }
70
71
        $epsSystem = $wrapper->getEpsSystemData();
72
        if ($epsSystem === null || $epsSystem->getEps() < 1) {
73
            $game->addInformation("Nicht genügend Energie vorhanden (1 benötigt)");
74
            return;
75
        }
76
77
        $epsSystem->lowerEps(1)->update();
78
79
        $mapField = $ship->getCurrentMapField();
80
81
        $colonyClass = $mapField->getFieldType()->getColonyClass();
82
        if ($colonyClass !== null) {
83
            $game->checkDatabaseItem($colonyClass->getDatabaseId());
84
        }
85
        if ($mapField->getSystem() !== null && $mapField->getFieldType()->getIsSystem()) {
86
            $game->checkDatabaseItem($mapField->getSystem()->getSystemType()->getDatabaseEntryId());
87
        }
88
        if ($ship->getStarsystemMap() !== null) {
89
            $buoy = $this->buoyRepository->findBySysMapId($ship->getStarsystemMap()->getId());
90
        }
91
        if ($ship->getMap() !== null) {
92
            $buoy = $this->buoyRepository->findByMapId($ship->getMap()->getId());
93
        }
94
95
        $game->setTemplateVar('SIGNATURES', $this->getSignatures($mapField->getId(), $ship->getSystem() !== null, $userId));
96
        $game->setTemplateVar('OTHER_SIG_COUNT', empty($this->fadedSignaturesUncloaked) ? null : count($this->fadedSignaturesUncloaked));
97
        $game->setTemplateVar('OTHER_CLOAKED_COUNT', empty($this->fadedSignaturesCloaked) ? null : count($this->fadedSignaturesCloaked));
98
        $game->setTemplateVar('SHIP', $ship);
99
        $game->setTemplateVar('MAP_PATH', $this->getMapPath($ship));
100
        $game->setTemplateVar('BUOY', $buoy);
101
102
        $game->setTemplateVar('ERROR', false);
103
    }
104
105
    /**
106
     * @return array<int, SignatureWrapper>
107
     */
108
    private function getSignatures(int $fieldId, bool $isSystem, int $ignoreId): array
109
    {
110
        $allSigs = $this->flightSignatureRepository->getVisibleSignatures($fieldId, $isSystem, $ignoreId);
111
112
        $filteredSigs = [];
113
114
        foreach ($allSigs as $sig) {
115
            $id = $sig->getShipId();
116
            $name = $sig->getShipName();
117
118
            if (!array_key_exists($id . '_' . $name, $filteredSigs)) {
119
                $wrapper = new SignatureWrapper($sig);
120
121
                if ($wrapper->getRump() === null) {
122
                    if ($sig->isCloaked()) {
123
                        if ($sig->getTime() > (time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_CLOAKED)) {
124
                            $this->fadedSignaturesCloaked[$id] = $id;
125
                        }
126
                    } else {
127
                        $this->fadedSignaturesUncloaked[$id] = $id;
128
                    }
129
                } else {
130
                    $filteredSigs[$id . '_' . $name] = $wrapper;
131
                }
132
            }
133
        }
134
135
        return $filteredSigs;
136
    }
137
138
    private function getMapPath(ShipInterface $ship): string
139
    {
140
        $currentMapField = $ship->getCurrentMapField();
141
142
        if ($currentMapField instanceof MapInterface) {
143
            return $this->encodedMap->getEncodedMapPath($currentMapField->getFieldId(), $currentMapField->getLayer());
144
        } else {
145
            return sprintf('%d.png', $currentMapField->getFieldId());
146
        }
147
    }
148
}