Passed
Push — dev ( 996fbb...25004f )
by Janko
16:40
created

VisualNavPanel   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 80.43%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 102
ccs 37
cts 46
cp 0.8043
rs 10
c 0
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A loadLayers() 0 13 1
A determinePanelCenter() 0 17 4
A getPanelCenter() 0 7 2
A getParentMapLocation() 0 7 2
A isOnShipLevel() 0 7 2
A getPanelViewportPercentage() 0 4 2
A getEntryCallable() 0 9 1
A createBoundaries() 0 6 1
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 Stu\Component\Spacecraft\SpacecraftRumpRoleEnum;
9
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel;
10
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerCreationInterface;
11
use Stu\Lib\Map\VisualPanel\PanelBoundaries;
12
use Stu\Lib\Map\VisualPanel\VisualNavPanelEntry;
13
use Stu\Module\Logging\LoggerUtilInterface;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
use Stu\Orm\Entity\LocationInterface;
16
use Stu\Orm\Entity\MapInterface;
17
use Stu\Orm\Entity\StarSystemMapInterface;
18
use Stu\Orm\Entity\UserInterface;
19
20
class VisualNavPanel extends AbstractVisualPanel
21
{
22
    private LocationInterface|null $panelCenter = null;
23
24
    private ?bool $isOnShipLevel = null;
25
26 2
    public function __construct(
27
        PanelLayerCreationInterface $panelLayerCreation,
28
        private PanelLayerConfiguration $panelLayerConfiguration,
29
        private SpacecraftWrapperInterface $wrapper,
30
        private UserInterface $user,
31
        LoggerUtilInterface $loggerUtil,
32
        private bool $tachyonFresh
33
    ) {
34 2
        parent::__construct($panelLayerCreation, $loggerUtil);
35
    }
36
37 1
    #[Override]
38
    protected function createBoundaries(): PanelBoundaries
39
    {
40 1
        $range = $this->wrapper->getLssSystemData()?->getSensorRange() ?? 0;
41
42 1
        return PanelBoundaries::fromLocation($this->getPanelCenter(), $range);
43
    }
44
45 1
    #[Override]
46
    protected function loadLayers(): void
47
    {
48 1
        $this->panelLayerConfiguration->configureLayers(
49 1
            $this->panelLayerCreation,
50 1
            $this->wrapper,
51 1
            $this->getPanelCenter(),
52 1
            $this->user,
53 1
            $this->tachyonFresh,
54 1
            $this->isOnShipLevel()
55 1
        );
56
57 1
        $this->layers = $this->panelLayerCreation->build($this);
58
    }
59
60 1
    #[Override]
61
    protected function getEntryCallable(): callable
62
    {
63 1
        return fn(int $x, int $y): VisualNavPanelEntry => new VisualNavPanelEntry(
64 1
            $x,
65 1
            $y,
66 1
            $this->isOnShipLevel(),
67 1
            $this->layers,
68 1
            $this->wrapper->get()
69 1
        );
70
    }
71
72 1
    #[Override]
73
    protected function getPanelViewportPercentage(): int
74
    {
75 1
        return $this->wrapper->get()->isStation() ? 50 : 33;
76
    }
77
78 1
    private function isOnShipLevel(): bool
79
    {
80 1
        if ($this->isOnShipLevel === null) {
81 1
            $this->isOnShipLevel = $this->wrapper->get()->getLocation() === $this->getPanelCenter();
82
        }
83
84 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...
85
    }
86
87 1
    private function getPanelCenter(): LocationInterface
88
    {
89 1
        if ($this->panelCenter === null) {
90 1
            $this->panelCenter = $this->determinePanelCenter();
91
        }
92
93 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...
94
    }
95
96 1
    private function determinePanelCenter(): LocationInterface
97
    {
98 1
        $location = $this->wrapper->get()->getLocation();
99 1
        if ($location instanceof MapInterface) {
100 1
            return $location;
101
        }
102
103
        if (
104
            $this->wrapper->get()->getRump()->getRoleId() === SpacecraftRumpRoleEnum::SHIP_ROLE_SENSOR
105
            || $this->wrapper->get()->getRump()->getRoleId() === SpacecraftRumpRoleEnum::SHIP_ROLE_BASE
106
        ) {
107
            $parentMapLocation = $this->getParentMapLocation($location);
108
109
            return $parentMapLocation ?? $location;
110
        }
111
112
        return $location;
113
    }
114
115
    private function getParentMapLocation(LocationInterface $location): ?MapInterface
116
    {
117
        if ($location instanceof StarSystemMapInterface) {
118
            return $location->getSystem()->getMap();
119
        }
120
121
        return null;
122
    }
123
}
124