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

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