Passed
Pull Request — master (#2027)
by Nico
21:19 queued 10:33
created

VisualNavPanel::isUserMapActive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
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\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...
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
        return PanelBoundaries::fromLocation($this->getPanelCenter(), $this->wrapper->getSensorRange());
41
    }
42
43 1
    #[Override]
44
    protected function loadLayers(): void
45
    {
46 1
        $this->panelLayerConfiguration->configureLayers(
47 1
            $this->panelLayerCreation,
48 1
            $this->wrapper,
49 1
            $this->getPanelCenter(),
50 1
            $this->user,
51 1
            $this->tachyonFresh,
52 1
            $this->isOnShipLevel()
53 1
        );
54
55 1
        $this->layers = $this->panelLayerCreation->build($this);
56
    }
57
58 1
    #[Override]
59
    protected function getEntryCallable(): callable
60
    {
61 1
        return fn(int $x, int $y): VisualNavPanelEntry => new VisualNavPanelEntry(
62 1
            $x,
63 1
            $y,
64 1
            $this->isOnShipLevel(),
65 1
            $this->layers,
66 1
            $this->wrapper->get()
67 1
        );
68
    }
69
70 1
    #[Override]
71
    protected function getPanelViewportPercentage(): int
72
    {
73 1
        return $this->wrapper->get()->isStation() ? 50 : 33;
74
    }
75
76 1
    private function isOnShipLevel(): bool
77
    {
78 1
        if ($this->isOnShipLevel === null) {
79 1
            $this->isOnShipLevel = $this->wrapper->get()->getLocation() === $this->getPanelCenter();
80
        }
81
82 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...
83
    }
84
85 1
    private function getPanelCenter(): LocationInterface
86
    {
87 1
        if ($this->panelCenter === null) {
88 1
            $this->panelCenter = $this->determinePanelCenter();
89
        }
90
91 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...
92
    }
93
94 1
    private function determinePanelCenter(): LocationInterface
95
    {
96 1
        $location = $this->wrapper->get()->getLocation();
97 1
        if ($location instanceof MapInterface) {
98 1
            return $location;
99
        }
100
101
        if (
102
            $this->wrapper->get()->getRump()->getRoleId() === SpacecraftRumpEnum::SHIP_ROLE_SENSOR
103
            || $this->wrapper->get()->getRump()->getRoleId() === SpacecraftRumpEnum::SHIP_ROLE_BASE
104
        ) {
105
            $parentMapLocation = $this->getParentMapLocation($location);
106
107
            return $parentMapLocation ?? $location;
108
        }
109
110
        return $location;
111
    }
112
113
    private function getParentMapLocation(LocationInterface $location): ?MapInterface
114
    {
115
        if ($location instanceof StarSystemMapInterface) {
116
            return $location->getSystem()->getMap();
117
        }
118
119
        return null;
120
    }
121
}
122