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

ShipCountLayerRenderer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 55
ccs 28
cts 28
cp 1
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getDisplayCount() 0 22 8
A __construct() 0 4 1
A render() 0 12 2
A getTachyonRange() 0 3 2
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