Passed
Push — dev ( 91186a...20d5e2 )
by Janko
05:17
created

NavPanel   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 93.18%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 76
ccs 41
cts 44
cp 0.9318
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A getLeft() 0 7 2
A getShipPosition() 0 5 1
A getUp() 0 7 2
A getSpacecraft() 0 3 1
A getRight() 0 8 2
A getMapBorders() 0 18 3
A getDown() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Map\NavPanel;
6
7
use RuntimeException;
8
use Stu\Orm\Entity\Spacecraft;
9
10
class NavPanel
11
{
12 2
    public function __construct(private Spacecraft $spacecraft) {}
13
14 2
    public function getSpacecraft(): Spacecraft
15
    {
16 2
        return $this->spacecraft;
17
    }
18
19
    /** @return array{cx: int, cy: int} */
20 2
    public function getShipPosition(): array
21
    {
22 2
        return [
23 2
            "cx" => $this->getSpacecraft()->getPosX(),
24 2
            "cy" => $this->getSpacecraft()->getPosY()
25 2
        ];
26
    }
27
28
    /** @return array{mx: int, my: int} */
29 2
    public function getMapBorders(): array
30
    {
31 2
        $starSystem = $this->getSpacecraft()->getSystem();
32
33 2
        if ($starSystem !== null) {
34 1
            return [
35 1
                "mx" => $starSystem->getMaxX(),
36 1
                "my" => $starSystem->getMaxY()
37 1
            ];
38
        }
39
40 1
        $layer = $this->getSpacecraft()->getLayer();
41 1
        if ($layer === null) {
42
            throw new RuntimeException('this should not happen');
43
        }
44 1
        return [
45 1
            "mx" => $layer->getWidth(),
46 1
            "my" => $layer->getHeight()
47 1
        ];
48
    }
49
50 2
    public function getLeft(): NavPanelButtonInterface
51
    {
52 2
        $coords = $this->getShipPosition();
53 2
        if ($coords['cx'] - 1 < 1) {
54
            return new NavPanelButton("-", true);
55
        }
56 2
        return new NavPanelButton(($coords['cx'] - 1) . "|" . $coords['cy']);
57
    }
58
59 2
    public function getRight(): NavPanelButtonInterface
60
    {
61 2
        $coords = $this->getShipPosition();
62 2
        $borders = $this->getMapBorders();
63 2
        if ($coords['cx'] + 1 > $borders['mx']) {
64 1
            return new NavPanelButton("-", true);
65
        }
66 1
        return new NavPanelButton(($coords['cx'] + 1) . "|" . $coords['cy']);
67
    }
68
69 2
    public function getUp(): NavPanelButtonInterface
70
    {
71 2
        $coords = $this->getShipPosition();
72 2
        if ($coords['cy'] - 1 < 1) {
73
            return new NavPanelButton("-", true);
74
        }
75 2
        return new NavPanelButton($coords['cx'] . "|" . ($coords['cy'] - 1));
76
    }
77
78 2
    public function getDown(): NavPanelButtonInterface
79
    {
80 2
        $coords = $this->getShipPosition();
81 2
        $borders = $this->getMapBorders();
82 2
        if ($coords['cy'] + 1 > $borders['my']) {
83 1
            return new NavPanelButton("-", true);
84
        }
85 1
        return new NavPanelButton($coords['cx'] . "|" . ($coords['cy'] + 1));
86
    }
87
}
88