Passed
Pull Request — master (#1825)
by Nico
59:43 queued 25:38
created

Location::getCx()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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