Passed
Push — master ( 6b02b9...a180a6 )
by Nico
26:39 queued 18:43
created

VisualNavPanel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 19
ccs 9
cts 9
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Ui;
6
7
use RuntimeException;
8
use Stu\Component\Ship\ShipRumpEnum;
9
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel;
10
use Stu\Lib\Map\VisualPanel\VisualPanelEntryData;
11
use Stu\Lib\Map\VisualPanel\VisualPanelRow;
12
use Stu\Lib\Map\VisualPanel\VisualPanelRowIndex;
13
use Stu\Module\Logging\LoggerUtilInterface;
14
use Stu\Orm\Entity\ShipInterface;
15
use Stu\Orm\Entity\UserInterface;
16
use Stu\Orm\Repository\ShipRepositoryInterface;
17
use Stu\Orm\Repository\UserMapRepositoryInterface;
18
19
class VisualNavPanel extends AbstractVisualPanel
20
{
21
    private ShipInterface $currentShip;
22
23
    private UserInterface $user;
24
25
    private bool $isTachyonSystemActive;
26
27
    private bool $tachyonFresh;
28
29
    private UserMapRepositoryInterface $userMapRepository;
30
31
    private ShipRepositoryInterface $shipRepository;
32
33
    private ShipUiFactoryInterface $shipUiFactory;
34
35 1
    public function __construct(
36
        ShipUiFactoryInterface $shipUiFactory,
37
        UserMapRepositoryInterface $userMapRepository,
38
        ShipRepositoryInterface $shipRepository,
39
        ShipInterface $currentShip,
40
        UserInterface $user,
41
        LoggerUtilInterface $loggerUtil,
42
        bool $isTachyonSystemActive,
43
        bool $tachyonFresh
44
    ) {
45 1
        parent::__construct($loggerUtil);
46
47 1
        $this->userMapRepository = $userMapRepository;
48 1
        $this->shipRepository = $shipRepository;
49 1
        $this->currentShip = $currentShip;
50 1
        $this->user = $user;
51 1
        $this->isTachyonSystemActive = $isTachyonSystemActive;
52 1
        $this->tachyonFresh = $tachyonFresh;
53 1
        $this->shipUiFactory = $shipUiFactory;
54
    }
55
56
    /**
57
     * @return array<VisualPanelEntryData>
58
     */
59
    private function getOuterSystemResult(): array
60
    {
61
        $cx = $this->currentShip->getCX();
62
        $cy = $this->currentShip->getCY();
63
        $range = $this->currentShip->getSensorRange();
64
65
        $layerId = $this->currentShip->getLayerId();
66
        if (!$this->user->hasExplored($layerId)) {
67
            $this->userMapRepository->insertMapFieldsForUser(
68
                $this->user->getId(),
69
                $layerId,
70
                $cx,
71
                $cy,
72
                $range
73
            );
74
        }
75
76
        return $this->shipRepository->getSensorResultOuterSystem(
77
            $cx,
78
            $cy,
79
            $layerId,
80
            $range,
81
            $this->currentShip->getSubspaceState(),
82
            $this->user->getId()
83
        );
84
    }
85
86
    /**
87
     * @return array<VisualPanelEntryData>
88
     */
89
    private function getInnerSystemResult(): iterable
90
    {
91
        return $this->shipRepository->getSensorResultInnerSystem(
92
            $this->currentShip,
93
            $this->user->getId()
94
        );
95
    }
96
97
    protected function loadLSS(): array
98
    {
99
        if ($this->loggerUtil->doLog()) {
100
            $startTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $startTime is dead and can be removed.
Loading history...
101
        }
102
        if ($this->showOuterMap()) {
103
            $result = $this->getOuterSystemResult();
104
        } else {
105
            $result = $this->getInnerSystemResult();
106
        }
107
        if ($this->loggerUtil->doLog()) {
108
            $endTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $endTime is dead and can be removed.
Loading history...
109
        }
110
111
        $currentShip = $this->currentShip;
112
113
        if ($this->loggerUtil->doLog()) {
114
            $startTime = microtime(true);
115
        }
116
117
        $rows = [];
118
119
        foreach ($result as $data) {
120
            $y = $data->getPosY();
121
122
            if ($y < 1) {
123
                continue;
124
            }
125
126
            //create new row if y changed
127
            if (!array_key_exists($y, $rows)) {
128
                $navPanelRow = new VisualPanelRow();
129
                $rowIndex = new VisualPanelRowIndex($y, 'th');
130
                $navPanelRow->addEntry($rowIndex);
131
132
                $rows[$y] = $navPanelRow;
133
            }
134
135
            $navPanelRow = $rows[$y];
136
            $entry = $this->shipUiFactory->createVisualNavPanelEntry(
137
                $data,
138
                $currentShip->getLayer(),
139
                $currentShip,
140
                $this->isTachyonSystemActive,
141
                $this->tachyonFresh
142
            );
143
            $navPanelRow->addEntry($entry);
144
        }
145
        if ($this->loggerUtil->doLog()) {
146
            $endTime = microtime(true);
147
            //$this->loggerUtil->log(sprintf("\tloadLSS-loop, seconds: %F", $endTime - $startTime));
148
        }
149
150
        return $rows;
151
    }
152
153
    /**
154
     * @return array<array{value: int}>
155
     */
156
    public function getHeadRow(): array
157
    {
158
        if ($this->headRow === null) {
159
            $cx = $this->showOuterMap() ? $this->currentShip->getCx() : $this->currentShip->getPosX();
160
            $range = $this->currentShip->getSensorRange();
161
162
            $min = $cx - $range;
163
            $max = $cx + $range;
164
165
            $row = [];
166
167
            while ($min <= $max) {
168
                if ($min < 1) {
169
                    $min++;
170
                    continue;
171
                }
172
                if ($this->showOuterMap()) {
173
                    if ($this->currentShip->getLayer() === null) {
174
                        throw new RuntimeException('layer should not be null if outside of system');
175
                    }
176
177
                    if ($min > $this->currentShip->getLayer()->getWidth()) {
178
                        break;
179
                    }
180
                }
181
                if (!$this->showOuterMap()) {
182
                    if ($this->currentShip->getSystem() === null) {
183
                        throw new RuntimeException('system should not be null if inside of system');
184
                    }
185
186
                    if ($min > $this->currentShip->getSystem()->getMaxX()) {
187
                        break;
188
                    }
189
                }
190
                $row[]['value'] = $min;
191
                $min++;
192
            }
193
194
            $this->headRow = $row;
195
        }
196
197
        return $this->headRow;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->headRow could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
198
    }
199
200
    protected function getPanelViewportPercentage(): int
201
    {
202
        return $this->currentShip->isBase() ? 50 : 33;
203
    }
204
205
    private function showOuterMap(): bool
206
    {
207
        return $this->currentShip->getSystem() === null
208
            || $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_SENSOR
209
            || $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE;
210
    }
211
}
212