Passed
Push — dev ( 72e750...cd9670 )
by Nico
25:25 queued 15:41
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->isUserMapActive($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
    private function isUserMapActive(int $layerId): bool
87
    {
88
        if (!$this->user->hasColony()) {
89
            return false;
90
        }
91
92
        return !$this->user->hasExplored($layerId);
93
    }
94
95
    /**
96
     * @return array<VisualPanelEntryData>
97
     */
98
    private function getInnerSystemResult(): iterable
99
    {
100
        return $this->shipRepository->getSensorResultInnerSystem(
101
            $this->currentShip,
102
            $this->user->getId()
103
        );
104
    }
105
106
    protected function loadLSS(): array
107
    {
108
        if ($this->loggerUtil->doLog()) {
109
            $startTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $startTime is dead and can be removed.
Loading history...
110
        }
111
        if ($this->showOuterMap()) {
112
            $result = $this->getOuterSystemResult();
113
        } else {
114
            $result = $this->getInnerSystemResult();
115
        }
116
        if ($this->loggerUtil->doLog()) {
117
            $endTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $endTime is dead and can be removed.
Loading history...
118
        }
119
120
        $currentShip = $this->currentShip;
121
122
        if ($this->loggerUtil->doLog()) {
123
            $startTime = microtime(true);
124
        }
125
126
        $rows = [];
127
128
        foreach ($result as $data) {
129
            $y = $data->getPosY();
130
131
            if ($y < 1) {
132
                continue;
133
            }
134
135
            //create new row if y changed
136
            if (!array_key_exists($y, $rows)) {
137
                $navPanelRow = new VisualPanelRow();
138
                $rowIndex = new VisualPanelRowIndex($y, 'th');
139
                $navPanelRow->addEntry($rowIndex);
140
141
                $rows[$y] = $navPanelRow;
142
            }
143
144
            $navPanelRow = $rows[$y];
145
            $entry = $this->shipUiFactory->createVisualNavPanelEntry(
146
                $data,
147
                $currentShip->getLayer(),
148
                $currentShip,
149
                $this->isTachyonSystemActive,
150
                $this->tachyonFresh
151
            );
152
            $navPanelRow->addEntry($entry);
153
        }
154
        if ($this->loggerUtil->doLog()) {
155
            $endTime = microtime(true);
156
            //$this->loggerUtil->log(sprintf("\tloadLSS-loop, seconds: %F", $endTime - $startTime));
157
        }
158
159
        return $rows;
160
    }
161
162
    protected function loadExtendedLSS(): array
163
    {
164
        if ($this->loggerUtil->doLog()) {
165
            $startTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $startTime is dead and can be removed.
Loading history...
166
        }
167
        if ($this->showOuterMap()) {
168
            $result = $this->getExtendedOuterSystemResult();
169
        } else {
170
            $result = $this->getInnerSystemResult();
171
        }
172
        if ($this->loggerUtil->doLog()) {
173
            $endTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $endTime is dead and can be removed.
Loading history...
174
        }
175
176
        $currentShip = $this->currentShip;
177
178
        if ($this->loggerUtil->doLog()) {
179
            $startTime = microtime(true);
180
        }
181
182
        $extendedrows = [];
183
184
        foreach ($result as $data) {
185
            $y = $data->getPosY();
186
187
            if ($y < 1) {
188
                continue;
189
            }
190
191
            //create new row if y changed
192
            if (!array_key_exists($y, $extendedrows)) {
193
                $extendednavPanelRow = new VisualPanelRow();
194
                $rowIndex = new VisualPanelRowIndex($y, 'th');
195
                $extendednavPanelRow->addEntry($rowIndex);
196
197
                $extendedrows[$y] = $extendednavPanelRow;
198
            }
199
200
            $extendednavPanelRow = $extendedrows[$y];
201
            $extendedentry = $this->shipUiFactory->createVisualNavPanelEntry(
202
                $data,
203
                $currentShip->getLayer(),
204
                $currentShip,
205
                $this->isTachyonSystemActive,
206
                $this->tachyonFresh
207
            );
208
            $extendednavPanelRow->addEntry($extendedentry);
209
        }
210
        if ($this->loggerUtil->doLog()) {
211
            $endTime = microtime(true);
212
            //$this->loggerUtil->log(sprintf("\tloadLSS-loop, seconds: %F", $endTime - $startTime));
213
        }
214
215
        return $extendedrows;
216
    }
217
218
    /**
219
     * @return array<array{value: int}>
220
     */
221
    public function getHeadRow(): array
222
    {
223
        if ($this->headRow === null) {
224
            $cx = $this->showOuterMap() ? $this->currentShip->getCx() : $this->currentShip->getPosX();
225
            $range = $this->currentShip->getSensorRange();
226
227
            $min = $cx - $range;
228
            $max = $cx + $range;
229
230
            $row = [];
231
232
            while ($min <= $max) {
233
                if ($min < 1) {
234
                    $min++;
235
                    continue;
236
                }
237
                if ($this->showOuterMap()) {
238
                    if ($this->currentShip->getLayer() === null) {
239
                        throw new RuntimeException('layer should not be null if outside of system');
240
                    }
241
242
                    if ($min > $this->currentShip->getLayer()->getWidth()) {
243
                        break;
244
                    }
245
                }
246
                if (!$this->showOuterMap()) {
247
                    if ($this->currentShip->getSystem() === null) {
248
                        throw new RuntimeException('system should not be null if inside of system');
249
                    }
250
251
                    if ($min > $this->currentShip->getSystem()->getMaxX()) {
252
                        break;
253
                    }
254
                }
255
                $row[]['value'] = $min;
256
                $min++;
257
            }
258
259
            $this->headRow = $row;
260
        }
261
262
        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...
263
    }
264
265
    public function getExtendedHeadRow(): array
266
    {
267
        if ($this->extendedheadRow === null) {
268
            $cx = $this->showOuterMap() ? $this->currentShip->getCx() : $this->currentShip->getPosX();
269
            $range = $this->currentShip->getSensorRange() * 2;
270
            $min = $cx - $range;
271
            $max = $cx + $range;
272
273
            $row = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $row is dead and can be removed.
Loading history...
274
275
            while ($min <= $max) {
276
                if ($min < 1) {
277
                    $min++;
278
                    continue;
279
                }
280
                if ($this->showOuterMap()) {
281
                    if ($this->currentShip->getLayer() === null) {
282
                        throw new RuntimeException('layer should not be null if outside of system');
283
                    }
284
285
                    if ($min > $this->currentShip->getLayer()->getWidth()) {
286
                        break;
287
                    }
288
                }
289
                if (!$this->showOuterMap()) {
290
                    if ($this->currentShip->getSystem() === null) {
291
                        throw new RuntimeException('system should not be null if inside of system');
292
                    }
293
294
                    if ($min > $this->currentShip->getSystem()->getMaxX()) {
295
                        break;
296
                    }
297
                }
298
                $extendedrow[]['value'] = $min;
299
                $min++;
300
            }
301
302
            $this->extendedheadRow = $extendedrow;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $extendedrow does not seem to be defined for all execution paths leading up to this point.
Loading history...
303
        }
304
305
        return $this->extendedheadRow;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->extendedheadRow 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...
306
    }
307
308
    protected function getPanelViewportPercentage(): int
309
    {
310
        return $this->currentShip->isBase() ? 50 : 33;
311
    }
312
313
    /**
314
     * @return array<VisualPanelEntryData>
315
     */
316
    private function getExtendedOuterSystemResult(): array
317
    {
318
        $cx = $this->currentShip->getCX();
319
        $cy = $this->currentShip->getCY();
320
        $range = $this->currentShip->getSensorRange() * 2;
321
322
        $layerId = $this->currentShip->getLayerId();
323
324
        return $this->shipRepository->getSensorResultOuterSystem(
325
            $cx,
326
            $cy,
327
            $layerId,
328
            $range,
329
            $this->currentShip->getSubspaceState(),
330
            $this->user->getId()
331
        );
332
    }
333
334
335
    private function showOuterMap(): bool
336
    {
337
        return $this->currentShip->getSystem() === null
338
            || $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_SENSOR
339
            || $this->currentShip->getRump()->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE;
340
    }
341
}
342