Passed
Push — dev ( 5010f4...52d0f1 )
by Janko
07:47
created

SignaturePanelEntry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Map\VisualPanel;
6
7
use Stu\Component\Map\EncodedMapInterface;
8
use Stu\Orm\Entity\LayerInterface;
9
10
class SignaturePanelEntry implements VisualPanelElementInterface
11
{
12
    public const ONLY_BACKGROUND_IMAGE = 1;
13
14
    protected VisualPanelEntryData $data;
15
16
    private ?LayerInterface $layer;
17
18
    private EncodedMapInterface $encodedMap;
19
20
    private string $cssClass = 'lss';
21
22 5
    public function __construct(
23
        VisualPanelEntryData $data,
24
        ?LayerInterface $layer,
25
        EncodedMapInterface $encodedMap
26
    ) {
27 5
        $this->data = $data;
28 5
        $this->layer = $layer;
29 5
        $this->encodedMap = $encodedMap;
30
    }
31
32 4
    public function getLssCellData(): LssCellData
33
    {
34 4
        return new LssCellData(
35 4
            $this->getSystemBackgroundId(),
36 4
            $this->getFieldGraphicID(),
37 4
            $this->getMapGraphicPath(),
38 4
            $this->data->getShieldState(),
39 4
            $this->getSubspaceCode(),
40 4
            $this->getDisplayCount(),
41 4
        );
42
    }
43
44 4
    private function getFieldGraphicID(): ?int
45
    {
46 4
        $fieldId = $this->data->getMapfieldType();
47
48 4
        if ($fieldId === self::ONLY_BACKGROUND_IMAGE) {
49
            return null;
50
        }
51
52 4
        return $fieldId;
53
    }
54
55 4
    private function getSystemBackgroundId(): ?string
56
    {
57 4
        if ($this->data->getSystemId() === null) {
58
            return null;
59
        }
60
61 4
        return sprintf(
62 4
            '%02d%02d',
63 4
            $this->data->getPosY(),
64 4
            $this->data->getPosX()
65 4
        );
66
    }
67
68 4
    private function getMapGraphicPath(): ?string
69
    {
70 4
        $layer = $this->layer;
71 4
        if ($layer === null) {
72
            return null;
73
        }
74
75 4
        if ($layer->isEncoded()) {
76
77 3
            return $this->encodedMap->getEncodedMapPath(
78 3
                $this->data->getMapfieldType(),
79 3
                $layer
80 3
            );
81
        }
82
83 1
        return sprintf('%d/%d.png', $layer->getId(), $this->data->getMapfieldType());
84
    }
85
86 4
    private function getSubspaceCode(): ?string
87
    {
88 4
        if (!$this->data->isSubspaceCodeAvailable()) {
89
            return null;
90
        }
91
92 4
        return sprintf(
93 4
            '%d%d%d%d',
94 4
            $this->getCode($this->data->getDirection1Count()),
95 4
            $this->getCode($this->data->getDirection2Count()),
96 4
            $this->getCode($this->data->getDirection3Count()),
97 4
            $this->getCode($this->data->getDirection4Count())
98 4
        );
99
    }
100
101 4
    private function getCode(int $shipCount): int
102
    {
103
        //TODO use constant values
104 4
        if ($shipCount == 0) {
105
            return 0;
106
        }
107 4
        if ($shipCount == 1) {
108 4
            return 1;
109
        }
110 4
        if ($shipCount < 6) {
111 4
            return 2;
112
        }
113
        if ($shipCount < 11) {
114
            return 3;
115
        }
116
        if ($shipCount < 21) {
117
            return 4;
118
        }
119
120
        return 5;
121
    }
122
123 4
    protected function getDisplayCount(): ?string
124
    {
125 4
        if ($this->data->getShipCount() > 0) {
126 4
            return (string) $this->data->getShipCount();
127
        }
128
129
        return null;
130
    }
131
132
    public function getBorder(): string
133
    {
134
        // default border style
135
        return '#2d2d2d';
136
    }
137
138
    public function getCSSClass(): string
139
    {
140
        return $this->cssClass;
141
    }
142
143
    public function isRowIndex(): bool
144
    {
145
        return false;
146
    }
147
}
148