Passed
Push — dev ( 42b6d7...a9da1d )
by Nico
20:25 queued 09:26
created

checkAndAddSpacecraftSignature()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 58
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 33
c 0
b 0
f 0
nc 11
nop 3
dl 0
loc 58
ccs 0
cts 34
cp 0
crap 156
rs 6.9666

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\Spacecraft\Lib\Ui;
6
7
use RuntimeException;
8
use Stu\Orm\Entity\Spacecraft;
9
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
10
use Stu\Lib\Map\FieldTypeEffectEnum;
11
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Spacecraftcount\SpacecraftCountLayerTypeEnum;
12
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Subspace\SubspaceLayerTypeEnum;
13
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerCreationInterface;
14
use Stu\Lib\Trait\LayerExplorationTrait;
15
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
16
use Stu\Orm\Entity\Layer;
17
use Stu\Orm\Entity\Location;
18
use Stu\Orm\Entity\Map;
19
use Stu\Orm\Entity\User;
20
use Stu\Orm\Repository\UserMapRepositoryInterface;
21
22
class PanelLayerConfiguration
23
{
24
    use LayerExplorationTrait;
25
26 1
    public function __construct(private UserMapRepositoryInterface $userMapRepository) {}
27
28 1
    public function configureLayers(
29
        PanelLayerCreationInterface $panelLayerCreation,
30
        SpacecraftWrapperInterface $wrapper,
31
        Location $panelCenter,
32
        User $currentUser,
33
        bool $tachyonFresh,
34
        bool $isShipOnLevel
35
    ): void {
36
37 1
        $spacecraft = $wrapper->get();
38 1
        if ($wrapper->get()->getSubspaceState()) {
39
            $panelLayerCreation->addSubspaceLayer($currentUser->getId(), SubspaceLayerTypeEnum::IGNORE_USER);
40
            $this->checkAndAddSpacecraftSignature($wrapper, $spacecraft, $panelLayerCreation);
41
        }
42
43 1
        $isLssMalfunctioning = $spacecraft->getLocation()->getFieldType()->hasEffect(FieldTypeEffectEnum::LSS_MALFUNCTION);
44 1
        if ($isLssMalfunctioning) {
45
            return;
46
        }
47
48 1
        $panelLayerCreation
49 1
            ->addShipCountLayer($tachyonFresh, $spacecraft, SpacecraftCountLayerTypeEnum::ALL, 0)
50 1
            ->addBorderLayer($wrapper, $isShipOnLevel)
51 1
            ->addAnomalyLayer();
52
53 1
        if ($panelCenter instanceof Map) {
54 1
            $layer = $panelCenter->getLayer();
55 1
            if ($layer === null) {
56
                throw new RuntimeException('this should not happen');
57
            }
58 1
            $panelLayerCreation->addMapLayer($layer);
59 1
            $this->createUserMapEntries($wrapper, $layer, $currentUser);
60
        } else {
61
            $panelLayerCreation
62
                ->addSystemLayer()
63
                ->addColonyShieldLayer();
64
        }
65
    }
66
67 1
    private function createUserMapEntries(SpacecraftWrapperInterface $wrapper, Layer $layer, User $currentUser): void
68
    {
69 1
        $map = $wrapper->get()->getMap();
70 1
        if ($map === null) {
71
            return;
72
        }
73
74 1
        $cx = $map->getX();
75 1
        $cy = $map->getY();
76 1
        $range = $wrapper->getLssSystemData()?->getSensorRange() ?? 0;
77
78 1
        if ($this->isUserMapActive($layer, $currentUser)) {
79 1
            $this->userMapRepository->insertMapFieldsForUser(
80 1
                $currentUser->getId(),
81 1
                $layer->getId(),
82 1
                $cx,
83 1
                $cy,
84 1
                $range
85 1
            );
86
        }
87
    }
88 1
    private function isUserMapActive(Layer $layer, User $currentUser): bool
89
    {
90 1
        if (!$currentUser->hasColony()) {
91
            return false;
92
        }
93
94 1
        return !$this->hasExplored($currentUser, $layer);
95
    }
96
97
    private function checkAndAddSpacecraftSignature(
98
        SpacecraftWrapperInterface $wrapper,
99
        Spacecraft $spacecraft,
100
        PanelLayerCreationInterface $panelLayerCreation
101
    ): void {
102
103
        $hasSubspaceScanner = $spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::SUBSPACE_SCANNER);
104
        if (!$hasSubspaceScanner) {
105
            return;
106
        }
107
108
        $isSubspaceScannerHealthy = $spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::SUBSPACE_SCANNER);
109
        if (!$isSubspaceScannerHealthy) {
110
            return;
111
        }
112
113
        $isSubspaceScannerActive = $spacecraft->getSystemState(SpacecraftSystemTypeEnum::SUBSPACE_SCANNER);
114
        if (!$isSubspaceScannerActive) {
115
            return;
116
        }
117
        $hasMatrixScanner = $spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::MATRIX_SCANNER);
118
        if (!$hasMatrixScanner) {
119
            return;
120
        }
121
122
        $isMatrixScannerHealthy = $spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::MATRIX_SCANNER);
123
        if (!$isMatrixScannerHealthy) {
124
            return;
125
        }
126
        $subspaceSystem = $spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::SUBSPACE_SCANNER);
127
        if ($subspaceSystem->getData() === null) {
128
            return;
129
        }
130
131
        $subspaceSystemData = $wrapper->getSubSpaceSystemData();
132
        if ($subspaceSystemData === null) {
133
            return;
134
        }
135
136
        $analyzeTime = $subspaceSystemData->getAnalyzeTime();
137
        if ($analyzeTime === null) {
138
            return;
139
        }
140
141
        $currentTime = time();
142
        $minTime = $analyzeTime + (3 * 60);
143
        $maxTime = $analyzeTime + (10 * 60);
144
145
        if (!($currentTime >= $minTime && $currentTime <= $maxTime)) {
146
            return;
147
        }
148
149
        $spacecraftId = $subspaceSystemData->getSpacecraftId();
150
        if ($spacecraftId === null) {
151
            return;
152
        }
153
154
        $panelLayerCreation->addSpacecraftSignatureLayer($spacecraftId);
155
    }
156
}
157