Passed
Push — master ( 2de9e3...c87c14 )
by Janko
09:57
created

SubspaceLayerRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 4
    public function __construct(
15
        private readonly int $zIndex,
16
        private readonly bool $isInverted = false
17 4
    ) {}
18
19
    /** @param AbstractSubspaceData $data */
20 4
    #[Override]
21
    public function render(CellDataInterface $data, PanelAttributesInterface $panel): string
22
    {
23 4
        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 3
        $subspaceCode = $this->getSubspaceCode($data);
28 3
        if ($subspaceCode === null) {
29 1
            return '';
30
        }
31
32 2
        return sprintf(
33 2
            '<img src="/assets/subspace/generated/%s.png" class="visualPanelLayer%s"
34 2
                style="z-index: %d; %s" />',
35 2
            $subspaceCode,
36 2
            $this->isInverted ? ' inverted' : '',
37 2
            $this->zIndex,
38 2
            $panel->getHeightAndWidth()
39 2
        );
40
    }
41
42 3
    private function getSubspaceCode(AbstractSubspaceData $data): ?string
43
    {
44 3
        if (!$this->isSubspaceCodeAvailable($data)) {
45 1
            return null;
46
        }
47
48 2
        return sprintf(
49 2
            '%d%d%d%d',
50 2
            $this->getCode($data->getDirection1Count()),
51 2
            $this->getCode($data->getDirection2Count()),
52 2
            $this->getCode($data->getDirection3Count()),
53 2
            $this->getCode($data->getDirection4Count())
54 2
        );
55
    }
56
57 3
    private function isSubspaceCodeAvailable(AbstractSubspaceData $data): bool
58
    {
59 3
        return $data->getDirection1Count() > 0
60 3
            || $data->getDirection2Count() > 0
61 3
            || $data->getDirection3Count() > 0
62 3
            || $data->getDirection4Count() > 0;
63
    }
64
65 2
    private function getCode(int $shipCount): int
66
    {
67 2
        if ($shipCount == 0) {
68
            return 0;
69
        }
70 2
        if ($shipCount == 1) {
71 1
            return 1;
72
        }
73 2
        if ($shipCount < 6) {
74 2
            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