Passed
Push — dev ( 3fd410...aa33df )
by Janko
13:32
created

Spacecraft::setIsInEmergency()   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\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\InheritanceType;
16
use Doctrine\ORM\Mapping\JoinColumn;
17
use Doctrine\ORM\Mapping\ManyToOne;
18
use Doctrine\ORM\Mapping\OneToMany;
19
use Doctrine\ORM\Mapping\OneToOne;
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 RuntimeException;
24
use Stu\Component\Spacecraft\SpacecraftModuleTypeEnum;
25
use Stu\Component\Spacecraft\SpacecraftStateEnum;
26
use Stu\Component\Spacecraft\SpacecraftLssModeEnum;
27
use Stu\Component\Spacecraft\SpacecraftTypeEnum;
28
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
29
use Stu\Component\Spacecraft\Trait\SpacecraftSystemExistenceTrait;
30
use Stu\Component\Spacecraft\Trait\SpacecrafCharacteristicsTrait;
31
use Stu\Component\Spacecraft\Trait\SpacecraftCrewTrait;
32
use Stu\Component\Spacecraft\Trait\SpacecraftHoldingWebTrait;
33
use Stu\Component\Spacecraft\Trait\SpacecraftHullColorStyleTrait;
34
use Stu\Component\Spacecraft\Trait\SpacecraftInteractionTrait;
35
use Stu\Component\Spacecraft\Trait\SpacecraftLocationTrait;
36
use Stu\Component\Spacecraft\Trait\SpacecraftShieldsTrait;
37
use Stu\Component\Spacecraft\Trait\SpacecraftStateTrait;
38
use Stu\Component\Spacecraft\Trait\SpacecraftStorageTrait;
39
use Stu\Component\Spacecraft\Trait\SpacecraftSystemHealthTrait;
40
use Stu\Component\Spacecraft\Trait\SpacecraftSystemStateTrait;
41
use Stu\Component\Spacecraft\Trait\SpacecraftTorpedoTrait;
42
use Stu\Orm\Repository\SpacecraftRepository;
43
44
#[Table(name: 'stu_spacecraft')]
45
#[Entity(repositoryClass: SpacecraftRepository::class)]
46
#[InheritanceType('JOINED')]
47
#[DiscriminatorColumn(name: 'type', type: 'string')]
48
#[DiscriminatorMap([
49
    SpacecraftTypeEnum::SHIP->value => Ship::class,
50
    SpacecraftTypeEnum::STATION->value => Station::class,
51
    SpacecraftTypeEnum::THOLIAN_WEB->value => TholianWeb::class
52
])]
53
abstract class Spacecraft implements SpacecraftInterface
54
{
55
    use SpacecraftSystemStateTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...cecraftSystemStateTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
56
    use SpacecraftSystemExistenceTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...aftSystemExistenceTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
57
    use SpacecraftSystemHealthTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...ecraftSystemHealthTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
58
    use SpacecraftShieldsTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...\SpacecraftShieldsTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
59
    use SpacecraftCrewTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft\Trait\SpacecraftCrewTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
60
    use SpacecraftLocationTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...SpacecraftLocationTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
61
    use SpacecraftStorageTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...\SpacecraftStorageTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
62
    use SpacecraftInteractionTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...cecraftInteractionTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
63
    use SpacecraftHoldingWebTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...acecraftHoldingWebTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
64
    use SpacecraftHullColorStyleTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...raftHullColorStyleTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
65
    use SpacecraftTorpedoTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...\SpacecraftTorpedoTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
66
    use SpacecrafCharacteristicsTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...rafCharacteristicsTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
67
    use SpacecraftStateTrait;
0 ignored issues
show
Bug introduced by
The trait Stu\Component\Spacecraft...it\SpacecraftStateTrait requires the property $value which is not provided by Stu\Orm\Entity\Spacecraft.
Loading history...
68
69
    #[Id]
70
    #[Column(type: 'integer')]
71
    #[GeneratedValue(strategy: 'IDENTITY')]
72
    private ?int $id = null;
73
74
    #[Column(type: 'integer')]
75
    private int $user_id = 0;
76
77
    #[Column(type: 'integer')]
78
    private int $rump_id = 0;
0 ignored issues
show
introduced by
The private property $rump_id is not used, and could be removed.
Loading history...
79
80
    #[Column(type: 'integer', nullable: true)]
81
    private ?int $plan_id = null;
0 ignored issues
show
introduced by
The private property $plan_id is not used, and could be removed.
Loading history...
82
83
    #[Column(type: 'string')]
84
    private string $name = '';
85
86
    #[Column(type: 'smallint', length: 1, enumType: SpacecraftLssModeEnum::class)]
87
    private SpacecraftLssModeEnum $lss_mode = SpacecraftLssModeEnum::NORMAL;
88
89
    #[Column(type: 'integer', length: 6)]
90
    private int $huelle = 0;
91
92
    #[Column(type: 'integer', length: 6)]
93
    private int $max_huelle = 0;
94
95
    #[Column(type: 'integer', length: 6)]
96
    private int $schilde = 0;
97
98
    #[Column(type: 'integer', length: 6)]
99
    private int $max_schilde = 0;
100
101
    #[Column(type: 'integer', nullable: true)]
102
    private ?int $tractored_ship_id = null;
0 ignored issues
show
introduced by
The private property $tractored_ship_id is not used, and could be removed.
Loading history...
103
104
    #[Column(type: 'integer', nullable: true)]
105
    private ?int $holding_web_id = null;
0 ignored issues
show
introduced by
The private property $holding_web_id is not used, and could be removed.
Loading history...
106
107
    #[Column(type: 'integer', nullable: true)]
108
    private ?int $database_id = null;
109
110
    private bool $is_destroyed = false;
111
112
    #[Column(type: 'boolean')]
113
    private bool $disabled = false;
114
115
    #[Column(type: 'smallint', enumType: SpacecraftStateEnum::class)]
116
    private SpacecraftStateEnum $state = SpacecraftStateEnum::NONE;
117
118
    #[Column(type: 'integer')]
119
    private int $location_id = 0;
0 ignored issues
show
introduced by
The private property $location_id is not used, and could be removed.
Loading history...
120
121
    #[OneToOne(targetEntity: 'Ship')]
122
    #[JoinColumn(name: 'tractored_ship_id', referencedColumnName: 'id')]
123
    private ?ShipInterface $tractoredShip = null;
124
125
    #[ManyToOne(targetEntity: 'TholianWeb')]
126
    #[JoinColumn(name: 'holding_web_id', referencedColumnName: 'id')]
127
    private ?TholianWebInterface $holdingWeb = null;
128
129
    #[ManyToOne(targetEntity: 'User')]
130
    #[JoinColumn(name: 'user_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
131
    private UserInterface $user;
132
133
    /**
134
     * @var ArrayCollection<int, CrewAssignmentInterface>
135
     */
136
    #[OneToMany(targetEntity: 'CrewAssignment', mappedBy: 'spacecraft', indexBy: 'id')]
137
    #[OrderBy(['id' => 'ASC'])]
138
    private Collection $crew;
139
140
    #[OneToOne(targetEntity: 'TorpedoStorage', mappedBy: 'spacecraft')]
141
    private ?TorpedoStorageInterface $torpedoStorage = null;
142
143
    /**
144
     * @var ArrayCollection<int, SpacecraftSystemInterface>
145
     */
146
    #[OneToMany(targetEntity: 'SpacecraftSystem', mappedBy: 'spacecraft', indexBy: 'system_type')]
147
    #[OrderBy(['system_type' => 'ASC'])]
148
    private Collection $systems;
149
150
    #[ManyToOne(targetEntity: 'SpacecraftRump')]
151
    #[JoinColumn(name: 'rump_id', referencedColumnName: 'id')]
152
    private SpacecraftRumpInterface $rump;
153
154
    #[ManyToOne(targetEntity: 'SpacecraftBuildplan')]
155
    #[JoinColumn(name: 'plan_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
156
    private ?SpacecraftBuildplanInterface $buildplan = null;
157
158
    /**
159
     * @var ArrayCollection<int, StorageInterface>
160
     */
161
    #[OneToMany(targetEntity: 'Storage', mappedBy: 'spacecraft', indexBy: 'commodity_id')]
162
    #[OrderBy(['commodity_id' => 'ASC'])]
163
    private Collection $storage;
164
165
    #[ManyToOne(targetEntity: 'Location')]
166
    #[JoinColumn(name: 'location_id', referencedColumnName: 'id')]
167
    private LocationInterface $location;
168
169
    /**
170
     * @var ArrayCollection<int, ShipLogInterface>
171
     */
172
    #[OneToMany(targetEntity: 'ShipLog', mappedBy: 'spacecraft', fetch: 'EXTRA_LAZY')]
173
    #[OrderBy(['id' => 'DESC'])]
174
    private Collection $logbook;
175
176
    #[OneToOne(targetEntity: 'ShipTakeover', mappedBy: 'source')]
177
    private ?ShipTakeoverInterface $takeoverActive = null;
178
179
    #[OneToOne(targetEntity: 'ShipTakeover', mappedBy: 'target')]
180
    private ?ShipTakeoverInterface $takeoverPassive = null;
181
182 3
    public function __construct()
183
    {
184 3
        $this->crew = new ArrayCollection();
185 3
        $this->systems = new ArrayCollection();
186 3
        $this->storage = new ArrayCollection();
187 3
        $this->logbook = new ArrayCollection();
188
    }
189
190 43
    #[Override]
191
    public function getId(): int
192
    {
193 43
        if ($this->id === null) {
194
            throw new RuntimeException(sprintf('entity "%s" not yet persisted', $this->getName()));
195
        }
196
197 43
        return $this->id;
198
    }
199
200 1
    #[Override]
201
    public function getUserId(): int
202
    {
203 1
        return $this->user_id;
204
    }
205
206 7
    #[Override]
207
    public function getUserName(): string
208
    {
209 7
        return $this->getUser()->getName();
210
    }
211
212 35
    #[Override]
213
    public function getName(): string
214
    {
215 35
        return $this->name;
216
    }
217
218
    #[Override]
219
    public function setName(string $name): SpacecraftInterface
220
    {
221
        $this->name = $name;
222
        return $this;
223
    }
224
225 2
    #[Override]
226
    public function getLssMode(): SpacecraftLssModeEnum
227
    {
228 2
        return $this->lss_mode;
229
    }
230
231
    #[Override]
232
    public function setLssMode(SpacecraftLssModeEnum $lssMode): SpacecraftInterface
233
    {
234
        $this->lss_mode = $lssMode;
235
        return $this;
236
    }
237
238 15
    #[Override]
239
    public function getHull(): int
240
    {
241 15
        return $this->huelle;
242
    }
243
244
    #[Override]
245
    public function setHuell(int $hull): SpacecraftInterface
246
    {
247
        $this->huelle = $hull;
248
        return $this;
249
    }
250
251 15
    #[Override]
252
    public function getMaxHull(): int
253
    {
254 15
        return $this->max_huelle;
255
    }
256
257
    #[Override]
258
    public function setMaxHuell(int $maxHull): SpacecraftInterface
259
    {
260
        $this->max_huelle = $maxHull;
261
        return $this;
262
    }
263
264 9
    #[Override]
265
    public function getShield(): int
266
    {
267 9
        return $this->schilde;
268
    }
269
270
    #[Override]
271
    public function setShield(int $schilde): SpacecraftInterface
272
    {
273
        $this->schilde = $schilde;
274
        return $this;
275
    }
276
277
    #[Override]
278
    public function setMaxShield(int $maxShields): SpacecraftInterface
279
    {
280
        $this->max_schilde = $maxShields;
281
        return $this;
282
    }
283
284 1
    #[Override]
285
    public function getDatabaseId(): ?int
286
    {
287 1
        return $this->database_id;
288
    }
289
290
    #[Override]
291
    public function setDatabaseId(?int $databaseEntryId): SpacecraftInterface
292
    {
293
        $this->database_id = $databaseEntryId;
294
        return $this;
295
    }
296
297 2
    #[Override]
298
    public function isDestroyed(): bool
299
    {
300 2
        return $this->is_destroyed;
301
    }
302
303
    #[Override]
304
    public function setIsDestroyed(bool $isDestroyed): SpacecraftInterface
305
    {
306
        $this->is_destroyed = $isDestroyed;
307
        return $this;
308
    }
309
310
    #[Override]
311
    public function isDisabled(): bool
312
    {
313
        return $this->disabled;
314
    }
315
316
    #[Override]
317
    public function setDisabled(bool $isDisabled): SpacecraftInterface
318
    {
319
        $this->disabled = $isDisabled;
320
        return $this;
321
    }
322
323
    /**
324
     * proportional to tractor beam system status
325
     */
326 1
    #[Override]
327
    public function getTractorPayload(): int
328
    {
329 1
        if (!$this->hasSpacecraftSystem(SpacecraftSystemTypeEnum::TRACTOR_BEAM)) {
330
            return 0;
331
        }
332
333 1
        return (int) (ceil($this->getRump()->getTractorPayload()
334 1
            * $this->getSpacecraftSystem(SpacecraftSystemTypeEnum::TRACTOR_BEAM)->getStatus() / 100));
335
    }
336
337 5
    #[Override]
338
    public function getState(): SpacecraftStateEnum
339
    {
340 5
        return $this->state;
341
    }
342
343
    #[Override]
344
    public function setState(SpacecraftStateEnum $state): SpacecraftInterface
345
    {
346
        $this->state = $state;
347
        return $this;
348
    }
349
350 13
    #[Override]
351
    public function getCrewAssignments(): Collection
352
    {
353 13
        return $this->crew;
354
    }
355
356 47
    #[Override]
357
    public function getUser(): UserInterface
358
    {
359 47
        return $this->user;
360
    }
361
362
    #[Override]
363
    public function setUser(UserInterface $user): SpacecraftInterface
364
    {
365
        $this->user = $user;
366
        return $this;
367
    }
368
369 2
    #[Override]
370
    public function getModules(): array
371
    {
372 2
        $modules = [];
373
374 2
        $buildplan = $this->getBuildplan();
375 2
        if ($buildplan === null) {
376
            return $modules;
377
        }
378
379 2
        foreach ($buildplan->getModulesOrdered() as $obj) {
380 2
            $module = $obj->getModule();
381 2
            $index = $module->getType() === SpacecraftModuleTypeEnum::SPECIAL ? $module->getId() : $module->getType()->value;
382 2
            $modules[$index] = $module;
383
        }
384
385 2
        return $modules;
386
    }
387
388 3
    #[Override]
389
    public function getTorpedoStorage(): ?TorpedoStorageInterface
390
    {
391 3
        return $this->torpedoStorage;
392
    }
393
394
    #[Override]
395
    public function setTorpedoStorage(?TorpedoStorageInterface $torpedoStorage): SpacecraftInterface
396
    {
397
        $this->torpedoStorage = $torpedoStorage;
398
        return $this;
399
    }
400
401 15
    #[Override]
402
    public function getStorage(): Collection
403
    {
404 15
        return $this->storage;
405
    }
406
407 39
    #[Override]
408
    public function getLocation(): MapInterface|StarSystemMapInterface
409
    {
410
        if (
411 39
            $this->location instanceof MapInterface
412 39
            || $this->location instanceof StarSystemMapInterface
413
        ) {
414 39
            return $this->location;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->location returns the type Stu\Orm\Entity\LocationInterface which includes types incompatible with the type-hinted return Stu\Orm\Entity\MapInterf...\StarSystemMapInterface.
Loading history...
415
        }
416
417
        throw new RuntimeException('unknown type');
418
    }
419
420 1
    #[Override]
421
    public function getLogbook(): Collection
422
    {
423 1
        return $this->logbook;
424
    }
425
426 4
    #[Override]
427
    public function getTakeoverActive(): ?ShipTakeoverInterface
428
    {
429 4
        return $this->takeoverActive;
430
    }
431
432
    #[Override]
433
    public function setTakeoverActive(?ShipTakeoverInterface $takeover): SpacecraftInterface
434
    {
435
        $this->takeoverActive = $takeover;
436
437
        return $this;
438
    }
439
440 4
    #[Override]
441
    public function getTakeoverPassive(): ?ShipTakeoverInterface
442
    {
443 4
        return $this->takeoverPassive;
444
    }
445
446
    #[Override]
447
    public function setTakeoverPassive(?ShipTakeoverInterface $takeover): SpacecraftInterface
448
    {
449
        $this->takeoverPassive = $takeover;
450
451
        return $this;
452
    }
453
454 3
    #[Override]
455
    public function setLocation(LocationInterface $location): SpacecraftInterface
456
    {
457 3
        $this->location = $location;
458
459 3
        return $this;
460
    }
461
462 11
    #[Override]
463
    public function getBeamFactor(): int
464
    {
465 11
        return $this->getRump()->getBeamFactor();
466
    }
467
468 13
    #[Override]
469
    public function getBuildplan(): ?SpacecraftBuildplanInterface
470
    {
471 13
        return $this->buildplan;
472
    }
473
474
    #[Override]
475
    public function setBuildplan(?SpacecraftBuildplanInterface $spacecraftBuildplan): SpacecraftInterface
476
    {
477
        $this->buildplan = $spacecraftBuildplan;
478
        return $this;
479
    }
480
481 40
    #[Override]
482
    public function getSystems(): Collection
483
    {
484 40
        return $this->systems;
485
    }
486
487 2
    #[Override]
488
    public function getTractoredShip(): ?ShipInterface
489
    {
490 2
        return $this->tractoredShip;
491
    }
492
493
    #[Override]
494
    public function setTractoredShip(?ShipInterface $ship): SpacecraftInterface
495
    {
496
        $this->tractoredShip = $ship;
497
        return $this;
498
    }
499
500 8
    #[Override]
501
    public function getHoldingWeb(): ?TholianWebInterface
502
    {
503 8
        return $this->holdingWeb;
504
    }
505
506
    #[Override]
507
    public function setHoldingWeb(?TholianWebInterface $web): SpacecraftInterface
508
    {
509
        $this->holdingWeb = $web;
510
511
        return $this;
512
    }
513
514 1
    #[Override]
515
    public function getRepairRate(): int
516
    {
517
        // @todo
518 1
        return 100;
519
    }
520
521 36
    #[Override]
522
    public function getRump(): SpacecraftRumpInterface
523
    {
524 36
        return $this->rump;
525
    }
526
527 19
    #[Override]
528
    public function getRumpId(): int
529
    {
530 19
        return $this->getRump()->getId();
531
    }
532
533 19
    #[Override]
534
    public function getRumpName(): string
535
    {
536 19
        return $this->getRump()->getName();
537
    }
538
539
    #[Override]
540
    public function setRump(SpacecraftRumpInterface $shipRump): SpacecraftInterface
541
    {
542
        $this->rump = $shipRump;
543
        return $this;
544
    }
545
546 2
    #[Override]
547
    public function getHref(): string
548
    {
549 2
        $moduleView = $this->getType()->getModuleView();
550 2
        if ($moduleView === null) {
551
            return '';
552
        }
553
554 2
        return sprintf(
555 2
            '%s?%s=1&id=%d',
556 2
            $moduleView->getPhpPage(),
557 2
            $this->getType()->getViewIdentifier(),
558 2
            $this->getId()
559 2
        );
560
    }
561
562
    #[Override]
563
    public function __toString(): string
564
    {
565
        if ($this->id !== null) {
566
            return sprintf(
567
                "id: %d, name: %s,\nhull: %d/%d, shields %d/%d",
568
                $this->getId(),
569
                $this->getName(),
570
                $this->huelle,
571
                $this->max_huelle,
572
                $this->schilde,
573
                $this->max_schilde
574
            );
575
        }
576
577
        return $this->getName();
578
    }
579
}
580