Passed
Push — master ( 1d5530...fba2a1 )
by Nico
56:37 queued 25:57
created

Location::getAnomalies()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
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 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\DiscriminatorColumn;
11
use Doctrine\ORM\Mapping\DiscriminatorMap;
12
use Doctrine\ORM\Mapping\Entity;
13
use Doctrine\ORM\Mapping\GeneratedValue;
14
use Doctrine\ORM\Mapping\Id;
15
use Doctrine\ORM\Mapping\Index;
16
use Doctrine\ORM\Mapping\InheritanceType;
17
use Doctrine\ORM\Mapping\JoinColumn;
18
use Doctrine\ORM\Mapping\ManyToOne;
19
use Doctrine\ORM\Mapping\OneToMany;
20
use Doctrine\ORM\Mapping\OrderBy;
21
use Doctrine\ORM\Mapping\Table;
22
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Stu\Component\Anomaly\Type\AnomalyTypeEnum;
24
use Stu\Orm\Repository\LocationRepository;
25
26
#[Table(name: 'stu_location')]
27
#[Index(name: 'location_coords_idx', columns: ['layer_id', 'cx', 'cy'])]
28
#[Index(name: 'location_field_type_idx', columns: ['field_id'])]
29
#[Entity(repositoryClass: LocationRepository::class)]
30
#[InheritanceType('JOINED')]
31
#[DiscriminatorColumn(name: 'discr', type: 'string')]
32
#[DiscriminatorMap(['map' => Map::class, 'systemMap' => StarSystemMap::class])]
33
abstract class Location implements LocationInterface
34
{
35
    #[Id]
36
    #[Column(type: 'integer')]
37
    #[GeneratedValue(strategy: 'IDENTITY')]
38
    private int $id;
39
40
    #[Column(type: 'integer', nullable: true)]
41
    private ?int $layer_id = null;
0 ignored issues
show
introduced by
The private property $layer_id is not used, and could be removed.
Loading history...
42
43
    #[Column(type: 'integer', nullable: true)]
44
    private ?int $cx = null;
45
46
    #[Column(type: 'integer', nullable: true)]
47
    private ?int $cy = null;
48
49
    #[Column(type: 'integer')]
50
    private int $field_id = 0;
51
52
    #[ManyToOne(targetEntity: 'Layer')]
53
    #[JoinColumn(name: 'layer_id', referencedColumnName: 'id')]
54
    protected ?LayerInterface $layer;
55
56
    #[ManyToOne(targetEntity: 'MapFieldType')]
57
    #[JoinColumn(name: 'field_id', referencedColumnName: 'id')]
58
    private MapFieldTypeInterface $mapFieldType;
59
60
    /**
61
     * @var ArrayCollection<int, ShipInterface>
62
     */
63
    #[OneToMany(targetEntity: 'Ship', mappedBy: 'location', fetch: 'EXTRA_LAZY')]
64
    private Collection $ships;
65
66
    /**
67
     * @var ArrayCollection<int, FlightSignatureInterface>
68
     */
69
    #[OneToMany(targetEntity: 'FlightSignature', mappedBy: 'location')]
70
    #[OrderBy(['time' => 'DESC'])]
71
    private Collection $signatures;
72
73
    /**
74
     * @var ArrayCollection<int, BuoyInterface>
75
     */
76
    #[OneToMany(targetEntity: 'Buoy', mappedBy: 'location')]
77
    private Collection $buoys;
78
79
    /**
80
     * @var ArrayCollection<int, AnomalyInterface>
81
     */
82
    #[OneToMany(targetEntity: 'Anomaly', mappedBy: 'location', fetch: 'EXTRA_LAZY')]
83
    private Collection $anomalies;
84
85
    public function __construct()
86
    {
87
        $this->ships = new ArrayCollection();
88
        $this->signatures = new ArrayCollection();
89
        $this->buoys = new ArrayCollection();
90
        $this->anomalies = new ArrayCollection();
91
    }
92
93
    #[Override]
94
    public function getId(): int
95
    {
96
        return $this->id;
97
    }
98
99
    #[Override]
100
    public function getCx(): ?int
101
    {
102
        return $this->cx;
103
    }
104
105
    #[Override]
106
    public function getCy(): ?int
107
    {
108
        return $this->cy;
109
    }
110
111
    #[Override]
112
    public function getFieldId(): int
113
    {
114
        return $this->field_id;
115
    }
116
117
    #[Override]
118
    public function getFieldType(): MapFieldTypeInterface
119
    {
120
        return $this->mapFieldType;
121
    }
122
123
    #[Override]
124
    public function setFieldType(MapFieldTypeInterface $mapFieldType): LocationInterface
125
    {
126
        $this->mapFieldType = $mapFieldType;
127
128
        return $this;
129
    }
130
131
    #[Override]
132
    public function getShips(): Collection
133
    {
134
        return $this->ships;
135
    }
136
137
    #[Override]
138
    public function getBuoys(): Collection
139
    {
140
        return $this->buoys;
141
    }
142
143
    #[Override]
144
    public function getAnomalies(bool $onlyActive = true): Collection
145
    {
146
        return $this->anomalies
147
            ->filter(fn (AnomalyInterface $anomaly): bool => !$onlyActive || $anomaly->isActive());
148
    }
149
150
    #[Override]
151
    public function hasAnomaly(AnomalyTypeEnum $type): bool
152
    {
153
        return $this->getAnomalies()
154
            ->exists(fn (int $key, AnomalyInterface $anomaly): bool => $anomaly->getAnomalyType()->getId() === $type->value);
155
    }
156
157
    #[Override]
158
    public function getSignatures(): Collection
159
    {
160
        return $this->signatures;
161
    }
162
163
    /**
164
     * @return Collection<int, WormholeEntryInterface>
165
     */
166
    protected abstract function getWormholeEntries(): Collection;
167
168
    #[Override]
169
    public function getRandomWormholeEntry(): ?WormholeEntryInterface
170
    {
171
        $wormholeEntries = $this->getWormholeEntries();
172
        if ($wormholeEntries->isEmpty()) {
173
            return null;
174
        }
175
176
        $usableEntries = $wormholeEntries
177
            ->filter(fn (WormholeEntryInterface $entry): bool => $entry->isUsable($this))
178
            ->toArray();
179
180
        return $usableEntries === [] ? null : $usableEntries[array_rand($usableEntries)];
181
    }
182
183
    #[Override]
184
    public function isMap(): bool
185
    {
186
        return $this instanceof MapInterface;
187
    }
188
189
    #[Override]
190
    public function isOverWormhole(): bool
191
    {
192
        return $this->isMap() && $this->getRandomWormholeEntry() !== null;
193
    }
194
}
195