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