Passed
Push — master ( 55cfd1...cb4d54 )
by Nico
22:41
created

SubspaceLayerRenderer::getCode()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
crap 6.0208
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Map\VisualPanel\Layer\Render;
6
7
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel;
8
use Stu\Lib\Map\VisualPanel\Layer\Data\CellDataInterface;
9
use Stu\Lib\Map\VisualPanel\Layer\Data\SubspaceData;
10
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerEnum;
11
12
final class SubspaceLayerRenderer implements LayerRendererInterface
13
{
14
    /** @param SubspaceData $data */
15 3
    public function render(CellDataInterface $data, AbstractVisualPanel $panel): string
16
    {
17 3
        $subspaceCode = $this->getSubspaceCode($data);
18 3
        if ($subspaceCode === null) {
19 1
            return '';
20
        }
21
22 2
        return sprintf(
23 2
            '<img src="/assets/subspace/generated/%s.png" class="lssSubspaceOverShield"
24 2
                style="z-index: %d; %s" />',
25 2
            $subspaceCode,
26 2
            PanelLayerEnum::SUBSPACE_SIGNATURES->value,
27 2
            $panel->getHeightAndWidth()
28 2
        );
29
    }
30
31 3
    private function getSubspaceCode(SubspaceData $data): ?string
32
    {
33 3
        if (!$this->isSubspaceCodeAvailable($data)) {
34 1
            return null;
35
        }
36
37 2
        return sprintf(
38 2
            '%d%d%d%d',
39 2
            $this->getCode($data->getDirection1Count()),
40 2
            $this->getCode($data->getDirection2Count()),
41 2
            $this->getCode($data->getDirection3Count()),
42 2
            $this->getCode($data->getDirection4Count())
43 2
        );
44
    }
45
46 3
    private function isSubspaceCodeAvailable(SubspaceData $data): bool
47
    {
48 3
        return $data->getDirection1Count() > 0
49 3
            || $data->getDirection2Count() > 0
50 3
            || $data->getDirection3Count() > 0
51 3
            || $data->getDirection4Count() > 0;
52
    }
53
54 2
    private function getCode(int $shipCount): int
55
    {
56 2
        if ($shipCount == 0) {
57
            return 0;
58
        }
59 2
        if ($shipCount == 1) {
60 1
            return 1;
61
        }
62 2
        if ($shipCount < 6) {
63 2
            return 2;
64
        }
65 2
        if ($shipCount < 11) {
66 2
            return 3;
67
        }
68 2
        if ($shipCount < 21) {
69 2
            return 4;
70
        }
71
72 1
        return 5;
73
    }
74
}
75