Passed
Push — dev ( 0f4afd...29c427 )
by Janko
25:18 queued 14:48
created

SpacecraftCountLayerRenderer::getDisplayCount()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 13
nc 5
nop 1
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 8
rs 8.4444
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\SpacecraftCountData;
10
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerEnum;
11
use Stu\Lib\Map\VisualPanel\PanelAttributesInterface;
12
use Stu\Orm\Entity\SpacecraftInterface;
13
14
final class SpacecraftCountLayerRenderer implements LayerRendererInterface
15
{
16 44
    public function __construct(private bool $showCloakedEverywhere, private ?SpacecraftInterface $currentSpacecraft) {}
17
18
    /** @param SpacecraftCountData $data */
19 44
    #[Override]
20
    public function render(CellDataInterface $data, PanelAttributesInterface $panel): string
21
    {
22 44
        $displayCount = $this->getDisplayCount($data);
23 44
        if ($displayCount === null) {
24 14
            return '';
25
        }
26
27 34
        return sprintf(
28 34
            '<div style="%s z-index: %d;" class="centered">%s</div>',
29 34
            $panel->getFontSize(),
30 34
            PanelLayerEnum::SPACECRAFT_COUNT->value,
31 34
            $displayCount
32 34
        );
33
    }
34
35 44
    private function getDisplayCount(SpacecraftCountData $data): ?string
36
    {
37 44
        if ($data->getSpacecraftCount() > 0) {
38 5
            return (string) $data->getSpacecraftCount();
39
        }
40 43
        if ($data->hasCloakedShips()) {
41 39
            if ($this->showCloakedEverywhere) {
42 1
                return "?";
43
            }
44
45 38
            $currentSpacecraft = $this->currentSpacecraft;
46
47
            if (
48 38
                $currentSpacecraft !== null
49 38
                && $currentSpacecraft->getTachyonState()
50 38
                && abs($data->getPosX() - $currentSpacecraft->getPosX()) <= $this->getTachyonRange($currentSpacecraft)
51 38
                && abs($data->getPosY() - $currentSpacecraft->getPosY()) <= $this->getTachyonRange($currentSpacecraft)
52
            ) {
53 28
                return "?";
54
            }
55
        }
56 14
        return null;
57
    }
58
59 36
    private function getTachyonRange(SpacecraftInterface $spacecraft): int
60
    {
61 36
        return $spacecraft->isStation() ? 7 : 3;
62
    }
63
}
64