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

Map::setSystemsId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
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\Entity;
11
use Doctrine\ORM\Mapping\GeneratedValue;
12
use Doctrine\ORM\Mapping\Id;
13
use Doctrine\ORM\Mapping\Index;
14
use Doctrine\ORM\Mapping\JoinColumn;
15
use Doctrine\ORM\Mapping\ManyToOne;
16
use Doctrine\ORM\Mapping\OneToMany;
17
use Doctrine\ORM\Mapping\OneToOne;
18
use Doctrine\ORM\Mapping\OrderBy;
19
use Doctrine\ORM\Mapping\Table;
20
use Doctrine\ORM\Mapping\UniqueConstraint;
21
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...
22
use Stu\Component\Map\MapEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Map\MapEnum 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\Lib\SectorString;
24
use Stu\Orm\Repository\MapRepository;
25
26
#[Table(name: 'stu_map')]
27
#[Index(name: 'coordinates_idx', columns: ['cx', 'cy'])]
28
#[Index(name: 'coordinates_reverse_idx', columns: ['cy', 'cx'])]
29
#[Index(name: 'map_field_type_idx', columns: ['field_id'])]
30
#[Index(name: 'map_layer_idx', columns: ['layer_id'])]
31
#[Index(name: 'map_system_idx', columns: ['systems_id'])]
32
#[Index(name: 'map_system_type_idx', columns: ['system_type_id'])]
33
#[Index(name: 'map_influence_area_idx', columns: ['influence_area_id'])]
34
#[Index(name: 'map_bordertype_idx', columns: ['bordertype_id'])]
35
#[Index(name: 'map_admin_region_idx', columns: ['admin_region_id'])]
36
#[UniqueConstraint(name: 'map_coordinate_idx', columns: ['layer_id', 'cx', 'cy'])]
37
#[Entity(repositoryClass: MapRepository::class)]
38
class Map implements MapInterface
39
{
40
    #[Id]
41
    #[Column(type: 'integer')]
42
    #[GeneratedValue(strategy: 'IDENTITY')]
43
    private int $id;
44
45
    #[Column(type: 'integer')]
46
    private int $cx = 0;
47
48
    #[Column(type: 'integer')]
49
    private int $cy = 0;
50
51
    #[Column(type: 'integer')]
52
    private int $layer_id;
0 ignored issues
show
introduced by
The private property $layer_id is not used, and could be removed.
Loading history...
53
54
    #[Column(type: 'integer')]
55
    private int $field_id = 0;
56
57
    #[Column(type: 'integer', nullable: true)]
58
    private ?int $system_type_id = null;
59
60
    #[Column(type: 'integer', nullable: true)]
61
    private ?int $systems_id = 0;
62
63
    #[Column(type: 'integer', nullable: true)]
64
    private ?int $influence_area_id = 0;
65
66
    #[Column(type: 'integer', nullable: true)]
67
    private ?int $bordertype_id = 0;
68
69
    #[Column(type: 'integer', nullable: true)]
70
    private ?int $region_id = 0;
71
72
    #[Column(type: 'integer', nullable: true)]
73
    private ?int $admin_region_id = null;
74
75
    #[ManyToOne(targetEntity: 'Layer')]
76
    #[JoinColumn(name: 'layer_id', referencedColumnName: 'id')]
77
    private LayerInterface $layer;
78
79
    #[OneToOne(targetEntity: 'StarSystem', inversedBy: 'map')]
80
    #[JoinColumn(name: 'systems_id', referencedColumnName: 'id')]
81
    private ?StarSystemInterface $starSystem = null;
82
83
    #[ManyToOne(targetEntity: 'StarSystem')]
84
    #[JoinColumn(name: 'influence_area_id', referencedColumnName: 'id')]
85
    private ?StarSystemInterface $influenceArea = null;
86
87
    #[ManyToOne(targetEntity: 'MapFieldType')]
88
    #[JoinColumn(name: 'field_id', referencedColumnName: 'id')]
89
    private MapFieldTypeInterface $mapFieldType;
90
91
    #[ManyToOne(targetEntity: 'StarSystemType')]
92
    #[JoinColumn(name: 'system_type_id', referencedColumnName: 'id')]
93
    private ?StarSystemTypeInterface $starSystemType = null;
94
95
    #[ManyToOne(targetEntity: 'MapBorderType')]
96
    #[JoinColumn(name: 'bordertype_id', referencedColumnName: 'id')]
97
    private ?MapBorderTypeInterface $mapBorderType = null;
98
99
    #[ManyToOne(targetEntity: 'MapRegion')]
100
    #[JoinColumn(name: 'region_id', referencedColumnName: 'id')]
101
    private ?MapRegionInterface $mapRegion = null;
102
103
    #[ManyToOne(targetEntity: 'MapRegion')]
104
    #[JoinColumn(name: 'admin_region_id', referencedColumnName: 'id')]
105
    private ?MapRegionInterface $administratedRegion = null;
106
107
    /**
108
     * @var ArrayCollection<int, BuoyInterface>
109
     */
110
    #[OneToMany(targetEntity: 'Buoy', mappedBy: 'map')]
111
    private Collection $buoys;
112
113
    /**
114
     * @var ArrayCollection<int, ShipInterface>
115
     */
116
    #[OneToMany(targetEntity: 'Ship', mappedBy: 'map', fetch: 'EXTRA_LAZY')]
117
    private Collection $ships;
118
119
    /**
120
     * @var ArrayCollection<int, FlightSignatureInterface>
121
     */
122
    #[OneToMany(targetEntity: 'FlightSignature', mappedBy: 'map')]
123
    #[OrderBy(['time' => 'DESC'])]
124
    private Collection $signatures;
125
126
    /**
127
     * @var ArrayCollection<int, AnomalyInterface>
128
     */
129
    #[OneToMany(targetEntity: 'Anomaly', mappedBy: 'map', fetch: 'EXTRA_LAZY')]
130
    private Collection $anomalies;
131
132
    /**
133
     * @var ArrayCollection<int, WormholeEntryInterface>
134
     */
135
    #[OneToMany(targetEntity: 'WormholeEntry', mappedBy: 'map')]
136
    private Collection $wormholeEntries;
137
138
139
    public function __construct()
140
    {
141
        $this->ships = new ArrayCollection();
142
        $this->signatures = new ArrayCollection();
143
        $this->anomalies = new ArrayCollection();
144
        $this->wormholeEntries = new ArrayCollection();
145
        $this->buoys = new ArrayCollection();
146
    }
147
148
    #[Override]
149
    public function getId(): int
150
    {
151
        return $this->id;
152
    }
153
154
    #[Override]
155
    public function getCx(): int
156
    {
157
        return $this->cx;
158
    }
159
160
    #[Override]
161
    public function getX(): int
162
    {
163
        return $this->getCx();
164
    }
165
166
    #[Override]
167
    public function getCy(): int
168
    {
169
        return $this->cy;
170
    }
171
172
    #[Override]
173
    public function getY(): int
174
    {
175
        return $this->getCy();
176
    }
177
178
    #[Override]
179
    public function getFieldId(): int
180
    {
181
        return $this->field_id;
182
    }
183
184
    #[Override]
185
    public function setFieldId(int $fieldId): MapInterface
186
    {
187
        $this->field_id = $fieldId;
188
        return $this;
189
    }
190
191
    #[Override]
192
    public function getSystemsId(): ?int
193
    {
194
        return $this->systems_id;
195
    }
196
197
    #[Override]
198
    public function setSystemsId(?int $systems_id): MapInterface
199
    {
200
        $this->systems_id = $systems_id;
201
        return $this;
202
    }
203
204
    #[Override]
205
    public function getSystemTypeId(): ?int
206
    {
207
        return $this->system_type_id;
208
    }
209
210
    #[Override]
211
    public function setSystemTypeId(?int $system_type_id): MapInterface
212
    {
213
        $this->system_type_id = $system_type_id;
214
        return $this;
215
    }
216
217
    #[Override]
218
    public function getInfluenceAreaId(): ?int
219
    {
220
        return $this->influence_area_id;
221
    }
222
223
    #[Override]
224
    public function setInfluenceAreaId(?int $influenceAreaId): MapInterface
225
    {
226
        $this->influence_area_id = $influenceAreaId;
227
        return $this;
228
    }
229
230
    #[Override]
231
    public function getBordertypeId(): ?int
232
    {
233
        return $this->bordertype_id;
234
    }
235
236
    #[Override]
237
    public function setBordertypeId(?int $bordertype_id): MapInterface
238
    {
239
        $this->bordertype_id = $bordertype_id;
240
        return $this;
241
    }
242
243
    #[Override]
244
    public function getRegionId(): ?int
245
    {
246
        return $this->region_id;
247
    }
248
249
    #[Override]
250
    public function setRegionId(?int $region_id): MapInterface
251
    {
252
        $this->region_id = $region_id;
253
        return $this;
254
    }
255
256
    #[Override]
257
    public function getAdminRegionId(): ?int
258
    {
259
        return $this->admin_region_id;
260
    }
261
262
    #[Override]
263
    public function setAdminRegionId(?int $admin_region_id): MapInterface
264
    {
265
        $this->admin_region_id = $admin_region_id;
266
        return $this;
267
    }
268
269
    #[Override]
270
    public function getLayer(): LayerInterface
271
    {
272
        return $this->layer;
273
    }
274
275
    #[Override]
276
    public function getSystem(): ?StarSystemInterface
277
    {
278
        return $this->starSystem;
279
    }
280
281
    #[Override]
282
    public function setSystem(StarSystemInterface $starSystem): MapInterface
283
    {
284
        $this->starSystem = $starSystem;
285
        return $this;
286
    }
287
288
    #[Override]
289
    public function getInfluenceArea(): ?StarSystemInterface
290
    {
291
        return $this->influenceArea;
292
    }
293
294
    #[Override]
295
    public function setInfluenceArea(?StarSystemInterface $influenceArea): MapInterface
296
    {
297
        $this->influenceArea = $influenceArea;
298
        return $this;
299
    }
300
301
    #[Override]
302
    public function getFieldType(): MapFieldTypeInterface
303
    {
304
        return $this->mapFieldType;
305
    }
306
307
    #[Override]
308
    public function getStarSystemType(): ?StarSystemTypeInterface
309
    {
310
        return $this->starSystemType;
311
    }
312
313
    #[Override]
314
    public function getMapBorderType(): ?MapBorderTypeInterface
315
    {
316
        return $this->mapBorderType;
317
    }
318
319
    #[Override]
320
    public function getMapRegion(): ?MapRegionInterface
321
    {
322
        return $this->mapRegion;
323
    }
324
325
    #[Override]
326
    public function getAdministratedRegion(): ?MapRegionInterface
327
    {
328
        return $this->administratedRegion;
329
    }
330
331
    public function getBorder(): string
332
    {
333
        $borderType = $this->getMapBorderType();
334
        if ($borderType === null) {
335
            return '';
336
        }
337
        return 'border: 1px solid ' . $borderType->getColor();
338
    }
339
340
    #[Override]
341
    public function getShips(): Collection
342
    {
343
        return $this->ships
344
            ->filter(fn (ShipInterface $ship): bool => $ship->getStarsystemMap() === null);
345
    }
346
347
    #[Override]
348
    public function getAnomalies(): Collection
349
    {
350
        return $this->anomalies;
351
    }
352
353
    #[Override]
354
    public function getSignatures(): Collection
355
    {
356
        return $this->signatures;
357
    }
358
359
    #[Override]
360
    public function getRandomWormholeEntry(): ?WormholeEntryInterface
361
    {
362
        if ($this->wormholeEntries->isEmpty()) {
363
            return null;
364
        }
365
366
        $usableEntries =  array_filter(
367
            $this->wormholeEntries->toArray(),
368
            function (WormholeEntryInterface $entry): bool {
369
                $type = $entry->getType();
370
371
                return $entry->isUsable() && ($type === MapEnum::WORMHOLE_ENTRY_TYPE_BOTH ||
372
                    $type === MapEnum::WORMHOLE_ENTRY_TYPE_IN);
373
            }
374
        );
375
376
        return $usableEntries === [] ? null : $usableEntries[array_rand($usableEntries)];
377
    }
378
379
    #[Override]
380
    public function getSectorString(): string
381
    {
382
        return SectorString::getForMap($this);
383
    }
384
385
    /**
386
     * @return Collection<int, BuoyInterface>
387
     */
388
    #[Override]
389
    public function getBuoys(): Collection
390
    {
391
        return $this->buoys;
392
    }
393
}
394