Passed
Push — dev ( 45bf8d...5b8f46 )
by Janko
10:07
created

VisualNavPanel   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 146
ccs 56
cts 72
cp 0.7778
rs 10
c 0
b 0
f 0
wmc 24

11 Methods

Rating   Name   Duplication   Size   Complexity  
A determinePanelCenter() 0 17 4
A getPanelCenter() 0 7 2
A createUserMapEntries() 0 18 3
A getParentMapLocation() 0 7 2
A isOnShipLevel() 0 7 2
A getPanelViewportPercentage() 0 4 2
A getEntryCallable() 0 9 1
A createBoundaries() 0 4 1
A __construct() 0 9 1
A isUserMapActive() 0 7 2
A loadLayers() 0 28 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Ui;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use RuntimeException;
9
use Stu\Component\Spacecraft\SpacecraftRumpEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft\SpacecraftRumpEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel;
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\Map\VisualPanel\PanelBoundaries;
15
use Stu\Lib\Map\VisualPanel\VisualNavPanelEntry;
16
use Stu\Module\Logging\LoggerUtilInterface;
17
use Stu\Orm\Entity\LayerInterface;
18
use Stu\Orm\Entity\LocationInterface;
19
use Stu\Orm\Entity\MapInterface;
20
use Stu\Orm\Entity\SpacecraftInterface;
21
use Stu\Orm\Entity\StarSystemMapInterface;
22
use Stu\Orm\Entity\UserInterface;
23
use Stu\Orm\Repository\UserMapRepositoryInterface;
24
25
class VisualNavPanel extends AbstractVisualPanel
26
{
27
    private LocationInterface|null $panelCenter = null;
28
29
    private ?bool $isOnShipLevel = null;
30
31 2
    public function __construct(
32
        PanelLayerCreationInterface $panelLayerCreation,
33
        private UserMapRepositoryInterface $userMapRepository,
34
        private SpacecraftInterface $currentSpacecraft,
35
        private UserInterface $user,
36
        LoggerUtilInterface $loggerUtil,
37
        private bool $tachyonFresh
38
    ) {
39 2
        parent::__construct($panelLayerCreation, $loggerUtil);
40
    }
41
42 1
    #[Override]
43
    protected function createBoundaries(): PanelBoundaries
44
    {
45 1
        return PanelBoundaries::fromLocation($this->getPanelCenter(), $this->currentSpacecraft->getSensorRange());
46
    }
47
48 1
    #[Override]
49
    protected function loadLayers(): void
50
    {
51 1
        $panelLayerCreation = $this->panelLayerCreation
52 1
            ->addShipCountLayer($this->tachyonFresh, $this->currentSpacecraft, SpacecraftCountLayerTypeEnum::ALL, 0)
53 1
            ->addBorderLayer($this->currentSpacecraft, $this->isOnShipLevel())
54 1
            ->addAnomalyLayer();
55
56 1
        $map = $this->getPanelCenter();
57
58 1
        if ($map instanceof MapInterface) {
59 1
            $layer = $map->getLayer();
60 1
            if ($layer === null) {
61
                throw new RuntimeException('this should not happen');
62
            }
63 1
            $panelLayerCreation->addMapLayer($layer);
64 1
            $this->createUserMapEntries($layer);
65
        } else {
66
            $panelLayerCreation
67
                ->addSystemLayer()
68
                ->addColonyShieldLayer();
69
        }
70
71 1
        if ($this->currentSpacecraft->getSubspaceState()) {
72
            $panelLayerCreation->addSubspaceLayer($this->user->getId(), SubspaceLayerTypeEnum::IGNORE_USER);
73
        }
74
75 1
        $this->layers = $panelLayerCreation->build($this);
76
    }
77
78 1
    #[Override]
79
    protected function getEntryCallable(): callable
80
    {
81 1
        return fn(int $x, int $y): VisualNavPanelEntry => new VisualNavPanelEntry(
82 1
            $x,
83 1
            $y,
84 1
            $this->isOnShipLevel(),
85 1
            $this->layers,
86 1
            $this->currentSpacecraft
87 1
        );
88
    }
89
90 1
    #[Override]
91
    protected function getPanelViewportPercentage(): int
92
    {
93 1
        return $this->currentSpacecraft->isStation() ? 50 : 33;
94
    }
95
96 1
    private function isOnShipLevel(): bool
97
    {
98 1
        if ($this->isOnShipLevel === null) {
99 1
            $this->isOnShipLevel = $this->currentSpacecraft->getLocation() === $this->getPanelCenter();
100
        }
101
102 1
        return $this->isOnShipLevel;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->isOnShipLevel could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
103
    }
104
105 1
    private function getPanelCenter(): LocationInterface
106
    {
107 1
        if ($this->panelCenter === null) {
108 1
            $this->panelCenter = $this->determinePanelCenter();
109
        }
110
111 1
        return $this->panelCenter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->panelCenter could return the type null which is incompatible with the type-hinted return Stu\Orm\Entity\LocationInterface. Consider adding an additional type-check to rule them out.
Loading history...
112
    }
113
114 1
    private function determinePanelCenter(): LocationInterface
115
    {
116 1
        $location = $this->currentSpacecraft->getLocation();
117 1
        if ($location instanceof MapInterface) {
118 1
            return $location;
119
        }
120
121
        if (
122
            $this->currentSpacecraft->getRump()->getRoleId() === SpacecraftRumpEnum::SHIP_ROLE_SENSOR
123
            || $this->currentSpacecraft->getRump()->getRoleId() === SpacecraftRumpEnum::SHIP_ROLE_BASE
124
        ) {
125
            $parentMapLocation = $this->getParentMapLocation($location);
126
127
            return $parentMapLocation ?? $location;
128
        }
129
130
        return $location;
131
    }
132
133
    private function getParentMapLocation(LocationInterface $location): ?MapInterface
134
    {
135
        if ($location instanceof StarSystemMapInterface) {
136
            return $location->getSystem()->getMap();
137
        }
138
139
        return null;
140
    }
141
142 1
    private function createUserMapEntries(LayerInterface $layer): void
143
    {
144 1
        $map = $this->currentSpacecraft->getMap();
145 1
        if ($map === null) {
146
            return;
147
        }
148
149 1
        $cx = $map->getX();
150 1
        $cy = $map->getY();
151 1
        $range = $this->currentSpacecraft->getSensorRange();
152
153 1
        if ($this->isUserMapActive($layer->getId())) {
154 1
            $this->userMapRepository->insertMapFieldsForUser(
155 1
                $this->user->getId(),
156 1
                $layer->getId(),
157 1
                $cx,
158 1
                $cy,
159 1
                $range
160 1
            );
161
        }
162
    }
163
164 1
    private function isUserMapActive(int $layerId): bool
165
    {
166 1
        if (!$this->user->hasColony()) {
167
            return false;
168
        }
169
170 1
        return !$this->user->hasExplored($layerId);
171
    }
172
}
173