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

BorderLayerRenderer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getBorderColor() 0 33 7
A __construct() 0 4 1
A isCurrentShipPosition() 0 5 3
A render() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Map\VisualPanel\Layer\Render;
6
7
use Stu\Component\Ship\ShipLSSModeEnum;
8
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel;
9
use Stu\Lib\Map\VisualPanel\Layer\Data\BorderData;
10
use Stu\Lib\Map\VisualPanel\Layer\Data\CellDataInterface;
11
use Stu\Orm\Entity\ShipInterface;
12
13
final class BorderLayerRenderer implements LayerRendererInterface
14
{
15
    public const DEFAULT_BORDER_COLOR = '#2d2d2d';
16
17
    private ?ShipInterface $currentShip;
18
19
    private ?bool $isOnShipLevel;
20
21 10
    public function __construct(?ShipInterface $currentShip, ?bool $isOnShipLevel)
22
    {
23 10
        $this->currentShip = $currentShip;
24 10
        $this->isOnShipLevel = $isOnShipLevel;
25
    }
26
27
    /** @param BorderData $data */
28 10
    public function render(CellDataInterface $data, AbstractVisualPanel $panel): string
29
    {
30 10
        return sprintf(
31 10
            'border:1px solid %s;',
32 10
            $this->getBorderColor($data)
33 10
        );
34
    }
35
36 10
    public function getBorderColor(BorderData $data): string
37
    {
38 10
        if ($this->currentShip === null) {
39
40 1
            return self::DEFAULT_BORDER_COLOR;
41
        }
42
43
        // current position gets grey border
44 9
        if ($this->isCurrentShipPosition($data, $this->currentShip)) {
45 1
            return '#9b9b9b';
46
        }
47
48
        // hierarchy based border style
49
        if (
50 8
            $this->currentShip->getLSSmode() == ShipLSSModeEnum::LSS_BORDER
51
        ) {
52 4
            $factionColor = $data->getFactionColor();
53 4
            if (!empty($factionColor)) {
54 1
                return $factionColor;
55
            }
56
57 3
            $allyColor = $data->getAllyColor();
58 3
            if (!empty($allyColor)) {
59 1
                return $allyColor;
60
            }
61
62 2
            $userColor = $data->getUserColor();
63 2
            if (!empty($userColor)) {
64 1
                return $userColor;
65
            }
66
        }
67
68 5
        return self::DEFAULT_BORDER_COLOR;
69
    }
70
71 9
    private function isCurrentShipPosition(BorderData $data, ShipInterface $currentShip): bool
72
    {
73 9
        return $this->isOnShipLevel === true
74 9
            && $data->getPosX() == $currentShip->getPosX()
75 9
            && $data->getPosY() == $currentShip->getPosY();
76
    }
77
}
78