Passed
Push — dev ( 45bf8d...5b8f46 )
by Janko
10:07
created

PanelBoundaries   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 84%

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 139
ccs 63
cts 75
cp 0.84
rs 10
c 0
b 0
f 0
wmc 20

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getMinY() 0 3 1
A isOnMap() 0 3 1
A getMaxY() 0 3 1
A getRowRange() 0 3 1
A getMinX() 0 3 1
A getMaxX() 0 3 1
A getColumnRange() 0 3 1
A __construct() 0 1 1
A getParentId() 0 3 1
A fromArray() 0 8 1
A createLocationWithRange() 0 13 1
A fromSystemMap() 0 8 1
A extendBy() 0 6 1
A stretch() 0 6 1
A fromLocation() 0 10 3
A fromSystem() 0 8 1
A fromMap() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Map\VisualPanel;
6
7
use RuntimeException;
8
use Stu\Orm\Entity\LayerInterface;
9
use Stu\Orm\Entity\LocationInterface;
10
use Stu\Orm\Entity\MapInterface;
11
use Stu\Orm\Entity\StarSystemInterface;
12
use Stu\Orm\Entity\StarSystemMapInterface;
13
14
final class PanelBoundaries
15
{
16 8
    public function __construct(private int $minX, private int $maxX, private int $minY, private int $maxY, private LayerInterface|StarSystemInterface $parent) {}
17
18
    /** @return array<int> */
19 5
    public function getColumnRange(): array
20
    {
21 5
        return range($this->minX, $this->maxX);
22
    }
23
24
    /** @return array<int> */
25 5
    public function getRowRange(): array
26
    {
27 5
        return range($this->minY, $this->maxY);
28
    }
29
30 8
    public function getMinX(): int
31
    {
32 8
        return $this->minX;
33
    }
34
35 8
    public function getMaxX(): int
36
    {
37 8
        return $this->maxX;
38
    }
39
40 8
    public function getMinY(): int
41
    {
42 8
        return $this->minY;
43
    }
44
45 8
    public function getMaxY(): int
46
    {
47 8
        return $this->maxY;
48
    }
49
50 4
    public function getParentId(): int
51
    {
52 4
        return $this->parent->getId();
53
    }
54
55 8
    public function isOnMap(): bool
56
    {
57 8
        return $this->parent instanceof LayerInterface;
58
    }
59
60
    public function extendBy(LocationInterface $location): void
61
    {
62
        $this->minX = min($this->minX, $location->getX());
63
        $this->maxX = max($this->maxX, $location->getX());
64
        $this->minY = min($this->minY, $location->getY());
65
        $this->maxY = max($this->maxY, $location->getY());
66
    }
67
68
    public function stretch(int $amount): void
69
    {
70
        $this->minX -= $amount;
71
        $this->maxX += $amount;
72
        $this->minY -= $amount;
73
        $this->maxY += $amount;
74
    }
75
76
    /**
77
     * @param array{minx: int, maxx: int, miny: int, maxy: int} $array
78
     */
79 1
    public static function fromArray(array $array, LayerInterface $layer): PanelBoundaries
80
    {
81 1
        return new PanelBoundaries(
82 1
            $array['minx'],
83 1
            $array['maxx'],
84 1
            $array['miny'],
85 1
            $array['maxy'],
86 1
            $layer
87 1
        );
88
    }
89
90 1
    public static function fromSystem(StarSystemInterface $system): PanelBoundaries
91
    {
92 1
        return new PanelBoundaries(
93 1
            1,
94 1
            $system->getMaxX(),
95 1
            1,
96 1
            $system->getMaxY(),
97 1
            $system
98 1
        );
99
    }
100
101 6
    public static function fromLocation(LocationInterface $location, int $range): PanelBoundaries
102
    {
103 6
        if ($location instanceof MapInterface) {
104 2
            return self::fromMap($location, $range);
105
        }
106 4
        if ($location instanceof StarSystemMapInterface) {
107 4
            return self::fromSystemMap($location, $range);
108
        }
109
110
        throw new RuntimeException('unsupported location type');
111
    }
112
113 2
    private static function fromMap(MapInterface $map, int $range): PanelBoundaries
114
    {
115 2
        $layer = $map->getLayer();
116 2
        if ($layer === null) {
117
            throw new RuntimeException('this should not happen');
118
        }
119
120 2
        return self::createLocationWithRange(
121 2
            $map,
122 2
            $layer->getWidth(),
123 2
            $layer->getHeight(),
124 2
            $layer,
125 2
            $range
126 2
        );
127
    }
128
129 4
    private static function fromSystemMap(StarSystemMapInterface $systemMap, int $range): PanelBoundaries
130
    {
131 4
        return self::createLocationWithRange(
132 4
            $systemMap,
133 4
            $systemMap->getSystem()->getMaxX(),
134 4
            $systemMap->getSystem()->getMaxY(),
135 4
            $systemMap->getSystem(),
136 4
            $range
137 4
        );
138
    }
139
140 6
    private static function createLocationWithRange(
141
        LocationInterface $location,
142
        int $width,
143
        int $height,
144
        LayerInterface|StarSystemInterface $parent,
145
        int $range
146
    ): PanelBoundaries {
147 6
        return new PanelBoundaries(
148 6
            max(1, $location->getX() - $range),
149 6
            min($width, $location->getX() + $range),
150 6
            max(1, $location->getY() - $range),
151 6
            min($height, $location->getY() + $range),
152 6
            $parent
153 6
        );
154
    }
155
}
156