Passed
Push — master ( 4cbcfa...1ee8de )
by Nico
53:18 queued 29:25
created

Location::hasAnomaly()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
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
21 10
    public function __construct(?MapInterface $map, ?StarSystemMapInterface $sysMap)
22
    {
23
        if (
24 10
            $map === null && $sysMap === null
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($map === null && $sysMa...ull && $sysMap !== null, Probably Intended Meaning: $map === null && ($sysMa...ll && $sysMap !== null)
Loading history...
25 10
            || $map !== null && $sysMap !== null
26
        ) {
27 2
            throw new InvalidArgumentException('Either map or systemMap has to be filled');
28
        }
29
30 8
        $this->location = $map ?? $sysMap;
31
    }
32
33
    public function get(): MapInterface|StarSystemMapInterface
34
    {
35
        return $this->location;
36
    }
37
38
    /**
39
     * @return Collection<int, ShipInterface>
40
     */
41 2
    public function getShips(): Collection
42
    {
43 2
        return $this->location->getShips();
44
    }
45
46 1
    public function getSectorString(): string
47
    {
48 1
        return $this->location->getSectorString();
49
    }
50
51
    /**
52
     * @return ReadableCollection<int, AnomalyInterface>
53
     */
54 5
    public function getAnomalies(): ReadableCollection
55
    {
56 5
        return $this->location->getAnomalies()->filter(fn (AnomalyInterface $anomaly): bool => $anomaly->isActive());
57
    }
58
59 4
    public function hasAnomaly(AnomalyTypeEnum $type): bool
60
    {
61 4
        foreach ($this->getAnomalies() as $anomaly) {
62 2
            if ($anomaly->getAnomalyType()->getId() === $type->value) {
63 1
                return true;
64
            }
65
        }
66
67 3
        return false;
68
    }
69
70
    public function isMap(): bool
71
    {
72
        return $this->location instanceof MapInterface;
73
    }
74
75
    public function getId(): int
76
    {
77
        return $this->location->getId();
78
    }
79
80
    public function getLayer(): ?LayerInterface
81
    {
82
        if ($this->location instanceof MapInterface) {
83
            return $this->location->getLayer();
84
        }
85
86
        $parentMap = $this->location->getSystem()->getMapField();
87
        if ($parentMap === null) {
88
            return null;
89
        }
90
91
        return $parentMap->getLayer();
92
    }
93
}
94