Passed
Pull Request — master (#1837)
by Nico
24:51
created

Location::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Map;
6
7
use Doctrine\Common\Collections\Collection;
8
use InvalidArgumentException;
9
use Stu\Component\Anomaly\Type\AnomalyTypeEnum;
10
use Stu\Orm\Entity\AnomalyInterface;
11
use Stu\Orm\Entity\LayerInterface;
12
use Stu\Orm\Entity\MapInterface;
13
use Stu\Orm\Entity\ShipInterface;
14
use Stu\Orm\Entity\StarSystemMapInterface;
15
16
class Location
17
{
18
    private MapInterface|StarSystemMapInterface $location;
19
    private ?MapInterface $parentMap;
20
21 11
    public function __construct(?MapInterface $map, ?StarSystemMapInterface $sysMap)
22
    {
23
        if (
24 11
            $map === null && $sysMap === null
25
        ) {
26 1
            throw new InvalidArgumentException('At least on of Map or systemMap has to be filled');
27
        }
28
29 10
        if ($sysMap !== null) {
30
31
            if (
32 3
                $map === null
33 3
                && !$sysMap->getSystem()->isWormhole()
34
            ) {
35 1
                throw new InvalidArgumentException('Map can only be null in Wormholes');
36
            }
37
38 2
            if ($sysMap->getSystem()->getMap() !== $map) {
39 1
                throw new InvalidArgumentException('System of SystemMap does not belong to current Map Field');
40
            }
41
        }
42
43 8
        $this->location = $sysMap ?? $map;
44 8
        $this->parentMap = $map;
45
    }
46
47
    public function get(): MapInterface|StarSystemMapInterface
48
    {
49
        return $this->location;
50
    }
51
52
    public function getParentMapLocation(): ?Location
53
    {
54
        $parentMap = $this->parentMap;
55
        if ($parentMap === null) {
56
            return null;
57
        }
58
59
        return new Location($parentMap, null);
60
    }
61
62
    /**
63
     * @return Collection<int, ShipInterface>
64
     */
65 2
    public function getShips(): Collection
66
    {
67 2
        return $this->location->getShips();
68
    }
69
70 1
    public function getSectorString(): string
71
    {
72 1
        return $this->location->getSectorString();
73
    }
74
75
    /**
76
     * @return Collection<int, AnomalyInterface>
77
     */
78 5
    public function getAnomalies(): Collection
79
    {
80 5
        return $this->location
81 5
            ->getAnomalies()
82 5
            ->filter(fn (AnomalyInterface $anomaly): bool => $anomaly->isActive());
83
    }
84
85 4
    public function hasAnomaly(AnomalyTypeEnum $type): bool
86
    {
87 4
        foreach ($this->getAnomalies() as $anomaly) {
88 2
            if ($anomaly->getAnomalyType()->getId() === $type->value) {
89 1
                return true;
90
            }
91
        }
92
93 3
        return false;
94
    }
95
96
    public function isMap(): bool
97
    {
98
        return $this->location instanceof MapInterface;
99
    }
100
101
    public function getId(): int
102
    {
103
        return $this->location->getId();
104
    }
105
106
    public function getLayer(): ?LayerInterface
107
    {
108
        if ($this->location instanceof MapInterface) {
109
            return $this->location->getLayer();
110
        }
111
112
        $parentMap = $this->location->getSystem()->getMap();
113
        if ($parentMap === null) {
114
            return null;
115
        }
116
117
        return $parentMap->getLayer();
118
    }
119
120
    public function getCx(): ?int
121
    {
122
        if ($this->parentMap === null) {
123
            return null;
124
        }
125
126
        return $this->parentMap->getCx();
127
    }
128
129
    public function getCy(): ?int
130
    {
131
        if ($this->parentMap === null) {
132
            return null;
133
        }
134
135
        return $this->parentMap->getCy();
136
    }
137
}
138