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

SubspaceLayerRenderer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 61
ccs 37
cts 38
cp 0.9737
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCode() 0 19 6
A getSubspaceCode() 0 12 2
A isSubspaceCodeAvailable() 0 6 4
A render() 0 13 2
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