Passed
Push — dev ( 91186a...20d5e2 )
by Janko
05:17
created

PanelLayerConfiguration   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 90.2%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 93
ccs 46
cts 51
cp 0.902
rs 10
c 2
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isUserMapActive() 0 7 2
A __construct() 0 4 1
A createUserMapEntries() 0 18 3
A checkAndAddSpacecraftSignature() 0 15 3
A configureLayers() 0 36 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Ui;
6
7
use RuntimeException;
8
use Stu\Lib\Map\FieldTypeEffectEnum;
9
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Spacecraftcount\SpacecraftCountLayerTypeEnum;
10
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Subspace\SubspaceLayerTypeEnum;
11
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerCreationInterface;
12
use Stu\Lib\Trait\LayerExplorationTrait;
13
use Stu\Module\Control\StuTime;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
use Stu\Orm\Entity\Layer;
16
use Stu\Orm\Entity\Location;
17
use Stu\Orm\Entity\Map;
18
use Stu\Orm\Entity\User;
19
use Stu\Orm\Repository\UserMapRepositoryInterface;
20
21
class PanelLayerConfiguration
22
{
23
    use LayerExplorationTrait;
24
25 1
    public function __construct(
26
        private readonly UserMapRepositoryInterface $userMapRepository,
27
        private readonly StuTime $stuTime
28 1
    ) {}
29
30 2
    public function configureLayers(
31
        PanelLayerCreationInterface $panelLayerCreation,
32
        SpacecraftWrapperInterface $wrapper,
33
        Location $panelCenter,
34
        User $currentUser,
35
        bool $tachyonFresh,
36
        bool $isShipOnLevel
37
    ): void {
38
39 2
        $spacecraft = $wrapper->get();
40 2
        if ($wrapper->get()->getSubspaceState()) {
41 1
            $panelLayerCreation->addSubspaceLayer($currentUser->getId(), SubspaceLayerTypeEnum::IGNORE_USER);
42 1
            $this->checkAndAddSpacecraftSignature($wrapper, $panelLayerCreation);
43
        }
44
45 2
        $isLssMalfunctioning = $spacecraft->getLocation()->getFieldType()->hasEffect(FieldTypeEffectEnum::LSS_MALFUNCTION);
46 2
        if ($isLssMalfunctioning) {
47
            return;
48
        }
49
50 2
        $panelLayerCreation
51 2
            ->addShipCountLayer($tachyonFresh, $spacecraft, SpacecraftCountLayerTypeEnum::ALL, 0)
52 2
            ->addBorderLayer($wrapper, $isShipOnLevel)
53 2
            ->addAnomalyLayer();
54
55 2
        if ($panelCenter instanceof Map) {
56 1
            $layer = $panelCenter->getLayer();
57 1
            if ($layer === null) {
58
                throw new RuntimeException('this should not happen');
59
            }
60 1
            $panelLayerCreation->addMapLayer($layer);
61 1
            $this->createUserMapEntries($wrapper, $layer, $currentUser);
62
        } else {
63 1
            $panelLayerCreation
64 1
                ->addSystemLayer()
65 1
                ->addColonyShieldLayer();
66
        }
67
    }
68
69 1
    private function createUserMapEntries(SpacecraftWrapperInterface $wrapper, Layer $layer, User $currentUser): void
70
    {
71 1
        $map = $wrapper->get()->getMap();
72 1
        if ($map === null) {
73
            return;
74
        }
75
76 1
        $cx = $map->getX();
77 1
        $cy = $map->getY();
78 1
        $range = $wrapper->getLssSystemData()?->getSensorRange() ?? 0;
79
80 1
        if ($this->isUserMapActive($layer, $currentUser)) {
81 1
            $this->userMapRepository->insertMapFieldsForUser(
82 1
                $currentUser->getId(),
83 1
                $layer->getId(),
84 1
                $cx,
85 1
                $cy,
86 1
                $range
87 1
            );
88
        }
89
    }
90 1
    private function isUserMapActive(Layer $layer, User $currentUser): bool
91
    {
92 1
        if (!$currentUser->hasColony()) {
93
            return false;
94
        }
95
96 1
        return !$this->hasExplored($currentUser, $layer);
97
    }
98
99 1
    private function checkAndAddSpacecraftSignature(
100
        SpacecraftWrapperInterface $wrapper,
101
        PanelLayerCreationInterface $panelLayerCreation
102
    ): void {
103
104 1
        $subspaceSystemData = $wrapper->getSubspaceSystemData();
105 1
        if ($subspaceSystemData === null) {
106
            return;
107
        }
108
109 1
        $flightSig = $subspaceSystemData->getHighlightedFlightSig($this->stuTime->time());
110 1
        if ($flightSig !== null) {
111 1
            $spacecraftId = $flightSig->getShipId();
112 1
            $rumpId = $flightSig->getRump()->getId();
113 1
            $panelLayerCreation->addSpacecraftSignatureLayer($spacecraftId, $rumpId);
114
        }
115
    }
116
}
117