Passed
Push — master ( 55cfd1...cb4d54 )
by Nico
22:41
created

VisualNavPanel::getHeadRow()   B

Complexity

Conditions 11
Paths 13

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 24
c 0
b 0
f 0
nc 13
nop 0
dl 0
loc 42
ccs 0
cts 25
cp 0
crap 132
rs 7.3166

How to fix   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\Ship\Lib\Ui;
6
7
use Stu\Component\Ship\ShipRumpEnum;
8
use Stu\Lib\Map\Location;
9
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel;
10
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Shipcount\ShipcountLayerTypeEnum;
11
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Subspace\SubspaceLayerTypeEnum;
12
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerCreationInterface;
13
use Stu\Lib\Map\VisualPanel\PanelBoundaries;
14
use Stu\Lib\Map\VisualPanel\VisualNavPanelEntry;
15
use Stu\Module\Logging\LoggerUtilInterface;
16
use Stu\Orm\Entity\MapInterface;
17
use Stu\Orm\Entity\ShipInterface;
18
use Stu\Orm\Entity\UserInterface;
19
use Stu\Orm\Repository\UserMapRepositoryInterface;
20
21
class VisualNavPanel extends AbstractVisualPanel
22
{
23
    private ShipInterface $currentShip;
24
25
    private UserInterface $user;
26
27
    private bool $tachyonFresh;
28
29
    private UserMapRepositoryInterface $userMapRepository;
30
31
    private ?Location $panelCenter = null;
32
33
    private ?bool $isOnShipLevel = null;
34
35 1
    public function __construct(
36
        PanelLayerCreationInterface $panelLayerCreation,
37
        UserMapRepositoryInterface $userMapRepository,
38
        ShipInterface $currentShip,
39
        UserInterface $user,
40
        LoggerUtilInterface $loggerUtil,
41
        bool $tachyonFresh
42
    ) {
43 1
        parent::__construct($panelLayerCreation, $loggerUtil);
44
45 1
        $this->userMapRepository = $userMapRepository;
46 1
        $this->currentShip = $currentShip;
47 1
        $this->user = $user;
48 1
        $this->tachyonFresh = $tachyonFresh;
49
    }
50
51
    protected function createBoundaries(): PanelBoundaries
52
    {
53
        return PanelBoundaries::fromLocation($this->getPanelCenter(), $this->currentShip->getSensorRange());
54
    }
55
56
    protected function loadLayers(): void
57
    {
58
59
        $panelLayerCreation = $this->panelLayerCreation
60
            ->addShipCountLayer($this->tachyonFresh, $this->currentShip, ShipcountLayerTypeEnum::ALL, 0)
61
            ->addBorderLayer($this->currentShip, $this->isOnShipLevel());
62
63
        $map = $this->getPanelCenter()->get();
64
65
        if ($map instanceof MapInterface) {
66
            $panelLayerCreation->addMapLayer($map->getLayer());
67
            $this->createUserMapEntries();
68
        } else {
69
            $panelLayerCreation
70
                ->addSystemLayer()
71
                ->addColonyShieldLayer();
72
        }
73
74
        if ($this->currentShip->getSubspaceState()) {
75
            $panelLayerCreation->addSubspaceLayer($this->user->getId(), SubspaceLayerTypeEnum::IGNORE_USER);
76
        }
77
78
        $this->layers = $panelLayerCreation->build($this);
79
    }
80
81
    protected function getEntryCallable(): callable
82
    {
83
        return fn (int $x, int $y) => new VisualNavPanelEntry(
84
            $x,
85
            $y,
86
            $this->isOnShipLevel(),
87
            $this->layers,
88
            $this->currentShip
89
        );
90
    }
91
92
    protected function getPanelViewportPercentage(): int
93
    {
94
        return $this->currentShip->isBase() ? 50 : 33;
95
    }
96
97
    private function isOnShipLevel(): bool
98
    {
99
        if ($this->isOnShipLevel === null) {
100
            $this->isOnShipLevel = $this->currentShip->getLocation()->get() === $this->getPanelCenter()->get();
101
        }
102
103
        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...
104
    }
105
106
    private function getPanelCenter(): Location
107
    {
108
        if ($this->panelCenter === null) {
109
            $this->panelCenter = $this->determinePanelCenter();
110
        }
111
112
        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\Lib\Map\Location. Consider adding an additional type-check to rule them out.
Loading history...
113
    }
114
115
    private function determinePanelCenter(): Location
116
    {
117
        $map = $this->currentShip->getCurrentMapField();
118
        if ($map instanceof MapInterface) {
119
            return new Location($map, null);
120
        }
121
122
        if (
123
            $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_SENSOR
124
            || $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE
125
        ) {
126
            $mapOfSystem = $map->getSystem()->getMapField();
127
128
            return $mapOfSystem === null ? new Location(null, $map) : new Location($mapOfSystem, null);
129
        }
130
131
        return new Location(null, $map);
132
    }
133
134
    private function createUserMapEntries(): void
135
    {
136
        $cx = $this->currentShip->getCX();
137
        $cy = $this->currentShip->getCY();
138
        $range = $this->currentShip->getSensorRange();
139
140
        $layerId = $this->currentShip->getLayerId();
141
        if ($this->isUserMapActive($layerId)) {
142
            $this->userMapRepository->insertMapFieldsForUser(
143
                $this->user->getId(),
144
                $layerId,
145
                $cx,
146
                $cy,
147
                $range
148
            );
149
        }
150
    }
151
152
    private function isUserMapActive(int $layerId): bool
153
    {
154
        if (!$this->user->hasColony()) {
155
            return false;
156
        }
157
158
        return !$this->user->hasExplored($layerId);
159
    }
160
}
161