Passed
Push — dev ( 828982...b58723 )
by Nico
16:18
created

Location::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 2
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\Lib\Map\FieldTypeEffectEnum;
25
use Stu\Orm\Repository\LocationRepository;
26
27
#[Table(name: 'stu_location')]
28
#[Index(name: 'location_coords_idx', columns: ['layer_id', 'cx', 'cy'])]
29
#[Index(name: 'location_coords_reverse_idx', columns: ['layer_id', 'cy', 'cx'])]
30
#[Index(name: 'location_field_type_idx', columns: ['field_id'])]
31
#[Entity(repositoryClass: LocationRepository::class)]
32
#[InheritanceType('JOINED')]
33
#[DiscriminatorColumn(name: 'discr', type: 'string')]
34
#[DiscriminatorMap([
35
    'map' => Map::class,
36
    'systemMap' => StarSystemMap::class
37
])]
38
abstract class Location implements LocationInterface
39
{
40
    #[Id]
41
    #[Column(type: 'integer')]
42
    #[GeneratedValue(strategy: 'IDENTITY')]
43
    private int $id;
44
45
    #[Column(type: 'integer', nullable: true)]
46
    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...
47
48
    #[Column(type: 'integer', nullable: true)]
49
    private ?int $cx = null;
50
51
    #[Column(type: 'integer', nullable: true)]
52
    private ?int $cy = null;
53
54
    #[Column(type: 'integer')]
55
    private int $field_id = 0;
56
57
    #[ManyToOne(targetEntity: 'Layer')]
58
    #[JoinColumn(name: 'layer_id', referencedColumnName: 'id')]
59
    protected ?LayerInterface $layer;
60
61
    #[ManyToOne(targetEntity: 'MapFieldType')]
62
    #[JoinColumn(name: 'field_id', referencedColumnName: 'id')]
63
    private MapFieldTypeInterface $mapFieldType;
64
65
    /**
66
     * @var ArrayCollection<int, SpacecraftInterface>
67
     */
68
    #[OneToMany(targetEntity: 'Spacecraft', mappedBy: 'location', indexBy: 'id', fetch: 'EXTRA_LAZY')]
69
    private Collection $spacecrafts;
70
71
    /**
72
     * @var ArrayCollection<int, TrumfieldInterface>
73
     */
74
    #[OneToMany(targetEntity: 'Trumfield', mappedBy: 'location', indexBy: 'id', fetch: 'EXTRA_LAZY')]
75
    private Collection $trumfields;
76
77
    /**
78
     * @var ArrayCollection<int, FlightSignatureInterface>
79
     */
80
    #[OneToMany(targetEntity: 'FlightSignature', mappedBy: 'location')]
81
    #[OrderBy(['time' => 'DESC'])]
82
    private Collection $signatures;
83
84
    /**
85
     * @var ArrayCollection<int, BuoyInterface>
86
     */
87
    #[OneToMany(targetEntity: 'Buoy', mappedBy: 'location', indexBy: 'id', fetch: 'EXTRA_LAZY')]
88
    private Collection $buoys;
89
90
    /**
91
     * @var ArrayCollection<int, AnomalyInterface>
92
     */
93
    #[OneToMany(targetEntity: 'Anomaly', mappedBy: 'location', indexBy: 'anomaly_type_id', fetch: 'EXTRA_LAZY')]
94
    private Collection $anomalies;
95
96
    /**
97
     * @var ArrayCollection<int, LocationMiningInterface>
98
     */
99
    #[OneToMany(targetEntity: 'LocationMining', mappedBy: 'location')]
100
    private Collection $locationMinings;
101
102
    public function __construct()
103
    {
104
        $this->spacecrafts = new ArrayCollection();
105
        $this->trumfields = new ArrayCollection();
106
        $this->signatures = new ArrayCollection();
107
        $this->buoys = new ArrayCollection();
108
        $this->anomalies = new ArrayCollection();
109
    }
110
111 7
    #[Override]
112
    public function getId(): int
113
    {
114 7
        return $this->id;
115
    }
116
117 20
    #[Override]
118
    public function getCx(): ?int
119
    {
120 20
        return $this->cx;
121
    }
122
123 20
    #[Override]
124
    public function getCy(): ?int
125
    {
126 20
        return $this->cy;
127
    }
128
129 3
    #[Override]
130
    public function getFieldId(): int
131
    {
132 3
        return $this->field_id;
133
    }
134
135 3
    #[Override]
136
    public function getFieldType(): MapFieldTypeInterface
137
    {
138 3
        return $this->mapFieldType;
139
    }
140
141
    #[Override]
142
    public function setFieldType(MapFieldTypeInterface $mapFieldType): LocationInterface
143
    {
144
        $this->mapFieldType = $mapFieldType;
145
146
        return $this;
147
    }
148
149 5
    #[Override]
150
    public function getSpacecrafts(): Collection
151
    {
152 5
        return $this->spacecrafts;
153
    }
154
155 1
    #[Override]
156
    public function getSpacecraftsWithoutCloak(): Collection
157
    {
158 1
        return $this->spacecrafts
159 1
            ->filter(fn(SpacecraftInterface $spacecraft): bool => !$spacecraft->hasCloak());
160
    }
161
162 1
    #[Override]
163
    public function getSpacecraftsWithoutVacation(): Collection
164
    {
165 1
        return $this->spacecrafts
166 1
            ->filter(fn(SpacecraftInterface $spacecraft): bool => !$spacecraft->getUser()->isVacationRequestOldEnough());
167
    }
168
169 1
    #[Override]
170
    public function getTrumfields(): Collection
171
    {
172 1
        return $this->trumfields;
173
    }
174
175 2
    #[Override]
176
    public function getBuoys(): Collection
177
    {
178 2
        return $this->buoys;
179
    }
180
181 2
    #[Override]
182
    public function getAnomalies(): Collection
183
    {
184 2
        return $this->anomalies;
185
    }
186
187
    public function addAnomaly(AnomalyInterface $anomaly): void
188
    {
189
        $this->anomalies->set($anomaly->getAnomalyType()->getId(), $anomaly);
190
    }
191
192
    #[Override]
193
    public function hasAnomaly(AnomalyTypeEnum $type): bool
194
    {
195
        return $this->anomalies->containsKey($type->value);
196
    }
197
198
    #[Override]
199
    public function getAnomaly(AnomalyTypeEnum $type): ?AnomalyInterface
200
    {
201
        return $this->anomalies->get($type->value);
202
    }
203
204
    #[Override]
205
    public function getSignatures(): Collection
206
    {
207
        return $this->signatures;
208
    }
209
210
    /**
211
     * @return Collection<int, WormholeEntryInterface>
212
     */
213
    protected abstract function getWormholeEntries(): Collection;
214
215 2
    #[Override]
216
    public function getRandomWormholeEntry(): ?WormholeEntryInterface
217
    {
218 2
        $wormholeEntries = $this->getWormholeEntries();
219 2
        if ($wormholeEntries->isEmpty()) {
220 2
            return null;
221
        }
222
223
        $usableEntries = $wormholeEntries
224
            ->filter(fn(WormholeEntryInterface $entry): bool => $entry->isUsable($this))
225
            ->toArray();
226
227
        return $usableEntries === [] ? null : $usableEntries[array_rand($usableEntries)];
228
    }
229
230
    #[Override]
231
    public function isMap(): bool
232
    {
233
        return $this instanceof MapInterface;
234
    }
235
236
    #[Override]
237
    public function isOverWormhole(): bool
238
    {
239
        return $this->isMap() && $this->getRandomWormholeEntry() !== null;
240
    }
241
242
    #[Override]
243
    public function getLocationMinings(): Collection
244
    {
245
        return $this->locationMinings;
246
    }
247
248
    #[Override]
249
    public function isAnomalyForbidden(): bool
250
    {
251
        return $this->getFieldType()->hasEffect(FieldTypeEffectEnum::NO_ANOMALIES);
252
    }
253
}