Passed
Pull Request — master (#2028)
by Nico
23:40 queued 11:07
created

SpacecraftCountLayerRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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