Passed
Push — dev ( 91186a...20d5e2 )
by Janko
05:17
created

SubspaceLayerRenderer::render()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 3
nop 2
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 4
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Map\VisualPanel\Layer\Render;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Stu\Lib\Map\VisualPanel\Layer\Data\CellDataInterface;
9
use Stu\Lib\Map\VisualPanel\Layer\Data\AbstractSubspaceData;
10
use Stu\Lib\Map\VisualPanel\PanelAttributesInterface;
11
12
final class SubspaceLayerRenderer implements LayerRendererInterface
13
{
14 5
    public function __construct(
15
        private readonly int $zIndex,
16
        private readonly bool $isInverted = false
17 5
    ) {}
18
19
    /** @param AbstractSubspaceData $data */
20 5
    #[Override]
21
    public function render(CellDataInterface $data, PanelAttributesInterface $panel): string
22
    {
23 5
        if ($data->isDisabled()) {
0 ignored issues
show
Bug introduced by
The method isDisabled() does not exist on Stu\Lib\Map\VisualPanel\...\Data\CellDataInterface. It seems like you code against a sub-type of Stu\Lib\Map\VisualPanel\...\Data\CellDataInterface such as Stu\Lib\Map\VisualPanel\...ta\AbstractSubspaceData. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        if ($data->/** @scrutinizer ignore-call */ isDisabled()) {
Loading history...
24 1
            return '';
25
        }
26
27 4
        $subspaceCode = $this->getSubspaceCode($data);
28 4
        if ($subspaceCode === null) {
29 2
            return '';
30
        }
31
32 3
        return sprintf(
33 3
            '<img src="/assets/subspace/generated/%s.png" class="visualPanelLayer%s"
34 3
                style="z-index: %d; %s" />',
35 3
            $subspaceCode,
36 3
            $this->isInverted ? ' inverted' : '',
37 3
            $this->zIndex,
38 3
            $panel->getHeightAndWidth()
39 3
        );
40
    }
41
42 4
    private function getSubspaceCode(AbstractSubspaceData $data): ?string
43
    {
44 4
        if (!$this->isSubspaceCodeAvailable($data)) {
45 2
            return null;
46
        }
47
48 3
        return sprintf(
49 3
            '%d%d%d%d',
50 3
            $this->getCode($data->getDirection1Count()),
51 3
            $this->getCode($data->getDirection2Count()),
52 3
            $this->getCode($data->getDirection3Count()),
53 3
            $this->getCode($data->getDirection4Count())
54 3
        );
55
    }
56
57 4
    private function isSubspaceCodeAvailable(AbstractSubspaceData $data): bool
58
    {
59 4
        return $data->getDirection1Count() > 0
60 4
            || $data->getDirection2Count() > 0
61 4
            || $data->getDirection3Count() > 0
62 4
            || $data->getDirection4Count() > 0;
63
    }
64
65 3
    private function getCode(int $shipCount): int
66
    {
67 3
        if ($shipCount == 0) {
68 1
            return 0;
69
        }
70 3
        if ($shipCount == 1) {
71 2
            return 1;
72
        }
73 3
        if ($shipCount < 6) {
74 3
            return 2;
75
        }
76 2
        if ($shipCount < 11) {
77 2
            return 3;
78
        }
79 2
        if ($shipCount < 21) {
80 2
            return 4;
81
        }
82
83 1
        return 5;
84
    }
85
}
86