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

SignaturePanel::getViewport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Admin\View\ShowSignatures;
6
7
use RuntimeException;
8
use Stu\Component\Map\EncodedMapInterface;
9
use Stu\Component\Map\MapEnum;
10
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel;
11
use Stu\Lib\Map\VisualPanel\SignaturePanelEntry;
12
use Stu\Lib\Map\VisualPanel\VisualPanelEntryData;
13
use Stu\Lib\Map\VisualPanel\VisualPanelRow;
14
use Stu\Lib\Map\VisualPanel\VisualPanelRowIndex;
15
use Stu\Module\Logging\LoggerUtilInterface;
16
use Stu\Orm\Entity\LayerInterface;
17
use Stu\Orm\Repository\ShipRepositoryInterface;
18
19
class SignaturePanel extends AbstractVisualPanel
20
{
21
    private int $userId;
22
    private int $allyId;
23
24
    /** @var array{minx: int, maxx: int, miny: int, maxy: int} */
25
    private array $data;
26
27
    private ShipRepositoryInterface $shipRepository;
28
29
    private EncodedMapInterface $encodedMap;
30
31
    private LayerInterface $layer;
32
33
    /**
34
     * @param array{minx: int, maxx: int, miny: int, maxy: int} $entry
35
     */
36
    public function __construct(
37
        ShipRepositoryInterface $shipRepository,
38
        EncodedMapInterface $encodedMap,
39
        LayerInterface $layer,
40
        int $userId,
41
        int $allyId,
42
        LoggerUtilInterface $loggerUtil,
43
        array $entry
44
    ) {
45
        parent::__construct($loggerUtil);
46
47
        $this->shipRepository = $shipRepository;
48
        $this->encodedMap = $encodedMap;
49
        $this->layer = $layer;
50
        $this->userId = $userId;
51
        $this->allyId = $allyId;
52
        $this->data = $entry;
53
    }
54
55
    /**
56
     * @return array<VisualPanelEntryData>
57
     */
58
    private function getOuterSystemResult(): array
59
    {
60
        if ($this->userId !== 0) {
61
            return $this->shipRepository->getSignaturesOuterSystemOfUser(
62
                $this->data['minx'],
63
                $this->data['maxx'],
64
                $this->data['miny'],
65
                $this->data['maxy'],
66
                MapEnum::LAYER_ID_CRAGGANMORE,
67
                $this->userId
68
            );
69
        } elseif ($this->allyId !== 0) {
70
            return $this->shipRepository->getSignaturesOuterSystemOfAlly(
71
                $this->data['minx'],
72
                $this->data['maxx'],
73
                $this->data['miny'],
74
                $this->data['maxy'],
75
                MapEnum::LAYER_ID_CRAGGANMORE,
76
                $this->allyId
77
            );
78
        }
79
80
        throw new RuntimeException('either userId or allyId has to be set');
81
    }
82
83
    protected function loadLSS(): array
84
    {
85
        if ($this->loggerUtil->doLog()) {
86
            $startTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $startTime is dead and can be removed.
Loading history...
87
        }
88
        $result = $this->getOuterSystemResult();
89
90
        if ($this->loggerUtil->doLog()) {
91
            $endTime = microtime(true);
0 ignored issues
show
Unused Code introduced by
The assignment to $endTime is dead and can be removed.
Loading history...
92
            //$this->loggerUtil->log(sprintf("\tloadLSS-query, seconds: %F", $endTime - $startTime));
93
        }
94
95
        $y = 0;
96
97
        if ($this->loggerUtil->doLog()) {
98
            $startTime = microtime(true);
99
        }
100
101
        $rows = [];
102
103
        foreach ($result as $data) {
104
            if ($data->getPosY() < 1) {
105
                continue;
106
            }
107
            if ($data->getPosY() != $y) {
108
                $y = $data->getPosY();
109
                $rows[$y] = new VisualPanelRow();
110
                $rowIndex = new VisualPanelRowIndex($y, 'th');
111
                $rows[$y]->addEntry($rowIndex);
112
            }
113
            $entry = new SignaturePanelEntry(
114
                $data,
115
                $this->layer,
116
                $this->encodedMap
117
            );
118
            $rows[$y]->addEntry($entry);
119
        }
120
        if ($this->loggerUtil->doLog()) {
121
            $endTime = microtime(true);
122
            //$this->loggerUtil->log(sprintf("\tloadLSS-loop, seconds: %F", $endTime - $startTime));
123
        }
124
125
        return $rows;
126
    }
127
128
    public function getHeadRow(): array
129
    {
130
        if ($this->headRow === null) {
131
            $min = $this->data['minx'];
132
            $max = $this->data['maxx'];
133
134
            foreach (range($min, $max) as $x) {
135
                $row[]['value'] = $x;
136
            }
137
138
            $this->headRow = $row;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $row seems to be defined by a foreach iteration on line 134. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
139
        }
140
        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...
141
    }
142
143
    protected function getPanelViewportPercentage(): int
144
    {
145
        return 100;
146
    }
147
}
148