Passed
Push — master ( 5a11dc...578008 )
by Janko
08:39
created

Spacecraft::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 8
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\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 LogicException;
23
use RuntimeException;
24
use Stu\Component\Spacecraft\SpacecraftModuleTypeEnum;
25
use Stu\Component\Spacecraft\SpacecraftStateEnum;
26
use Stu\Component\Spacecraft\SpacecraftTypeEnum;
27
use Stu\Component\Spacecraft\Trait\SpacecraftSystemExistenceTrait;
28
use Stu\Component\Spacecraft\Trait\SpacecrafCharacteristicsTrait;
29
use Stu\Component\Spacecraft\Trait\SpacecraftCrewTrait;
30
use Stu\Component\Spacecraft\Trait\SpacecraftHoldingWebTrait;
31
use Stu\Component\Spacecraft\Trait\SpacecraftHrefTrait;
32
use Stu\Component\Spacecraft\Trait\SpacecraftHullColorStyleTrait;
33
use Stu\Component\Spacecraft\Trait\SpacecraftInteractionTrait;
34
use Stu\Component\Spacecraft\Trait\SpacecraftLocationTrait;
35
use Stu\Component\Spacecraft\Trait\SpacecraftShieldsTrait;
36
use Stu\Component\Spacecraft\Trait\SpacecraftStateTrait;
37
use Stu\Component\Spacecraft\Trait\SpacecraftStorageTrait;
38
use Stu\Component\Spacecraft\Trait\SpacecraftSystemHealthTrait;
39
use Stu\Component\Spacecraft\Trait\SpacecraftSystemStateTrait;
40
use Stu\Component\Spacecraft\Trait\SpacecraftTorpedoTrait;
41
use Stu\Lib\Interaction\EntityWithInteractionCheckInterface;
42
use Stu\Lib\Map\EntityWithLocationInterface;
43
use Stu\Lib\Transfer\EntityWithStorageInterface;
44
use Stu\Module\Spacecraft\Lib\Crew\EntityWithCrewAssignmentsInterface;
45
use Stu\Module\Spacecraft\Lib\Destruction\SpacecraftDestroyerInterface;
46
use Stu\Orm\Repository\SpacecraftRepository;
47
48
#[Table(name: 'stu_spacecraft')]
49
#[Entity(repositoryClass: SpacecraftRepository::class)]
50
#[InheritanceType('JOINED')]
51
#[DiscriminatorColumn(name: 'type', type: 'string')]
52
#[DiscriminatorMap([
53
    SpacecraftTypeEnum::SHIP->value => Ship::class,
54
    SpacecraftTypeEnum::STATION->value => Station::class,
55
    SpacecraftTypeEnum::THOLIAN_WEB->value => TholianWeb::class
56
])]
57
abstract class Spacecraft implements
58
    SpacecraftDestroyerInterface,
59
    EntityWithStorageInterface,
60
    EntityWithLocationInterface,
61
    EntityWithCrewAssignmentsInterface,
62
    EntityWithInteractionCheckInterface
63
{
64
    use SpacecraftSystemStateTrait;
65
    use SpacecraftSystemExistenceTrait;
66
    use SpacecraftSystemHealthTrait;
67
    use SpacecraftShieldsTrait;
68
    use SpacecraftCrewTrait;
69
    use SpacecraftLocationTrait;
70
    use SpacecraftStorageTrait;
71
    use SpacecraftInteractionTrait;
72
    use SpacecraftHoldingWebTrait;
73
    use SpacecraftHullColorStyleTrait;
74
    use SpacecraftTorpedoTrait;
75
    use SpacecrafCharacteristicsTrait;
76
    use SpacecraftStateTrait;
77
    use SpacecraftHrefTrait;
78
79
    public const int SYSTEM_ECOST_DOCK = 1;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 79 at column 21
Loading history...
80
81
    #[Id]
82
    #[Column(type: 'integer')]
83
    #[GeneratedValue(strategy: 'IDENTITY')]
84
    private ?int $id = null;
85
86
    #[Column(type: 'integer')]
87
    private int $user_id = 0;
88
89
    #[Column(type: 'integer')]
90
    private int $rump_id = 0;
91
92
    #[Column(type: 'integer', nullable: true)]
93
    private ?int $plan_id = null;
94
95
    #[Column(type: 'string')]
96
    private string $name = '';
97
98
    #[Column(type: 'integer', length: 6)]
99
    private int $max_huelle = 0;
100
101
    #[Column(type: 'integer', length: 6)]
102
    private int $max_schilde = 0;
103
104
    #[Column(type: 'integer', nullable: true)]
105
    private ?int $tractored_ship_id = null;
106
107
    #[Column(type: 'integer', nullable: true)]
108
    private ?int $holding_web_id = null;
109
110
    #[Column(type: 'integer', nullable: true)]
111
    private ?int $database_id = null;
112
113
    #[Column(type: 'integer')]
114
    private int $location_id = 0;
115
116
    #[OneToOne(targetEntity: SpacecraftCondition::class, mappedBy: 'spacecraft', fetch: 'EAGER', cascade: ['all'])]
117
    private ?SpacecraftCondition $condition;
118
119
    #[OneToOne(targetEntity: Ship::class)]
120
    #[JoinColumn(name: 'tractored_ship_id', referencedColumnName: 'id')]
121
    private ?Ship $tractoredShip = null;
122
123
    #[ManyToOne(targetEntity: TholianWeb::class)]
124
    #[JoinColumn(name: 'holding_web_id', referencedColumnName: 'id')]
125
    private ?TholianWeb $holdingWeb = null;
126
127
    #[ManyToOne(targetEntity: User::class)]
128
    #[JoinColumn(name: 'user_id', nullable: false, referencedColumnName: 'id', onDelete: 'CASCADE')]
129
    private User $user;
130
131
    /**
132
     * @var ArrayCollection<int, CrewAssignment>
133
     */
134
    #[OneToMany(targetEntity: CrewAssignment::class, mappedBy: 'spacecraft', indexBy: 'crew_id')]
135
    #[OrderBy(['crew' => 'ASC'])]
136
    private Collection $crew;
137
138
    #[OneToOne(targetEntity: TorpedoStorage::class, mappedBy: 'spacecraft')]
139
    private ?TorpedoStorage $torpedoStorage = null;
140
141
    /**
142
     * @var ArrayCollection<int, SpacecraftSystem>
143
     */
144
    #[OneToMany(targetEntity: SpacecraftSystem::class, mappedBy: 'spacecraft', indexBy: 'system_type')]
145
    #[OrderBy(['system_type' => 'ASC'])]
146
    private Collection $systems;
147
148
    #[ManyToOne(targetEntity: SpacecraftRump::class)]
149
    #[JoinColumn(name: 'rump_id', nullable: false, referencedColumnName: 'id')]
150
    private SpacecraftRump $rump;
151
152
    #[ManyToOne(targetEntity: SpacecraftBuildplan::class)]
153
    #[JoinColumn(name: 'plan_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
154
    private ?SpacecraftBuildplan $buildplan = null;
155
156
    /**
157
     * @var ArrayCollection<int, Storage>
158
     */
159
    #[OneToMany(targetEntity: Storage::class, mappedBy: 'spacecraft', indexBy: 'commodity_id')]
160
    #[OrderBy(['commodity_id' => 'ASC'])]
161
    private Collection $storage;
162
163
    #[ManyToOne(targetEntity: Location::class)]
164
    #[JoinColumn(name: 'location_id', nullable: false, referencedColumnName: 'id')]
165
    private Location $location;
166
167
    /**
168
     * @var ArrayCollection<int, ShipLog>
169
     */
170
    #[OneToMany(targetEntity: ShipLog::class, mappedBy: 'spacecraft', fetch: 'EXTRA_LAZY')]
171
    #[OrderBy(['id' => 'DESC'])]
172
    private Collection $logbook;
173
174
    #[OneToOne(targetEntity: ShipTakeover::class, mappedBy: 'source')]
175
    private ?ShipTakeover $takeoverActive = null;
176
177
    #[OneToOne(targetEntity: ShipTakeover::class, mappedBy: 'target')]
178
    private ?ShipTakeover $takeoverPassive = null;
179
180 6
    public function __construct()
181
    {
182 6
        $this->crew = new ArrayCollection();
183 6
        $this->systems = new ArrayCollection();
184 6
        $this->storage = new ArrayCollection();
185 6
        $this->logbook = new ArrayCollection();
186
    }
187
188 43
    public function getId(): int
189
    {
190 43
        if ($this->id === null) {
191
            throw new RuntimeException(sprintf('entity "%s" not yet persisted', $this->getName()));
192
        }
193
194 43
        return $this->id;
195
    }
196
197 18
    public function getCondition(): SpacecraftCondition
198
    {
199 18
        return $this->condition ?? throw new LogicException('Spacecraft has no condition');;
200
    }
201
202 3
    public function setCondition(SpacecraftCondition $condition): Spacecraft
203
    {
204 3
        $this->condition = $condition;
205
206 3
        return $this;
207
    }
208
209
    public abstract function getType(): SpacecraftTypeEnum;
210
211
    public abstract function getFleet(): ?Fleet;
212
213 1
    public function getUserId(): int
214
    {
215 1
        return $this->user_id;
216
    }
217
218 7
    public function getUserName(): string
219
    {
220 7
        return $this->getUser()->getName();
221
    }
222
223 35
    public function getName(): string
224
    {
225 35
        return $this->name;
226
    }
227
228
    public function setName(string $name): Spacecraft
229
    {
230
        $this->name = $name;
231
        return $this;
232
    }
233
234 15
    public function getMaxHull(): int
235
    {
236 15
        return $this->max_huelle;
237
    }
238
239
    public function setMaxHuell(int $maxHull): Spacecraft
240
    {
241
        $this->max_huelle = $maxHull;
242
        return $this;
243
    }
244
245
    public function setMaxShield(int $maxShields): Spacecraft
246
    {
247
        $this->max_schilde = $maxShields;
248
        return $this;
249
    }
250
251 1
    public function getDatabaseId(): ?int
252
    {
253 1
        return $this->database_id;
254
    }
255
256
    public function setDatabaseId(?int $databaseEntryId): Spacecraft
257
    {
258
        $this->database_id = $databaseEntryId;
259
        return $this;
260
    }
261
262 13
    public function getCrewAssignments(): Collection
263
    {
264 13
        return $this->crew;
265
    }
266
267 48
    public function getUser(): User
268
    {
269 48
        return $this->user;
270
    }
271
272
    public function setUser(User $user): Spacecraft
273
    {
274
        $this->user = $user;
275
        return $this;
276
    }
277
278
    /** @return array<int, Module>*/
279 2
    public function getModules(): array
280
    {
281 2
        $modules = [];
282
283 2
        $buildplan = $this->getBuildplan();
284 2
        if ($buildplan === null) {
285
            return $modules;
286
        }
287
288 2
        foreach ($buildplan->getModulesOrdered() as $obj) {
289 2
            $module = $obj->getModule();
290 2
            $index = $module->getType() === SpacecraftModuleTypeEnum::SPECIAL ? $module->getId() : $module->getType()->value;
291 2
            $modules[$index] = $module;
292
        }
293
294 2
        return $modules;
295
    }
296
297 3
    public function getTorpedoStorage(): ?TorpedoStorage
298
    {
299 3
        return $this->torpedoStorage;
300
    }
301
302
    public function setTorpedoStorage(?TorpedoStorage $torpedoStorage): Spacecraft
303
    {
304
        $this->torpedoStorage = $torpedoStorage;
305
        return $this;
306
    }
307
308 15
    public function getStorage(): Collection
309
    {
310 15
        return $this->storage;
311
    }
312
313 39
    public function getLocation(): Map|StarSystemMap
314
    {
315
        if (
316 39
            $this->location instanceof Map
317 39
            || $this->location instanceof StarSystemMap
318
        ) {
319 39
            return $this->location;
320
        }
321
322
        throw new RuntimeException('unknown type');
323
    }
324
325
    /**
326
     * @return Collection<int, ShipLog> Ordered by id
327
     */
328 1
    public function getLogbook(): Collection
329
    {
330 1
        return $this->logbook;
331
    }
332
333
    public function getTakeoverActive(): ?ShipTakeover
334
    {
335
        return $this->takeoverActive;
336
    }
337
338
    public function setTakeoverActive(?ShipTakeover $takeover): Spacecraft
339
    {
340
        $this->takeoverActive = $takeover;
341
342
        return $this;
343
    }
344
345 4
    public function getTakeoverPassive(): ?ShipTakeover
346
    {
347 4
        return $this->takeoverPassive;
348
    }
349
350
    public function setTakeoverPassive(?ShipTakeover $takeover): Spacecraft
351
    {
352
        $this->takeoverPassive = $takeover;
353
354
        return $this;
355
    }
356
357 3
    public function setLocation(Location $location): Spacecraft
358
    {
359 3
        $this->location = $location;
360
361 3
        return $this;
362
    }
363
364 13
    public function getBuildplan(): ?SpacecraftBuildplan
365
    {
366 13
        return $this->buildplan;
367
    }
368
369
    public function setBuildplan(?SpacecraftBuildplan $spacecraftBuildplan): Spacecraft
370
    {
371
        $this->buildplan = $spacecraftBuildplan;
372
        return $this;
373
    }
374
375
    /**
376
     * @return Collection<int, SpacecraftSystem>
377
     */
378 42
    public function getSystems(): Collection
379
    {
380 42
        return $this->systems;
381
    }
382
383 2
    public function getTractoredShip(): ?Ship
384
    {
385 2
        return $this->tractoredShip;
386
    }
387
388
    public function setTractoredShip(?Ship $ship): Spacecraft
389
    {
390
        $this->tractoredShip = $ship;
391
        return $this;
392
    }
393
394 8
    public function getHoldingWeb(): ?TholianWeb
395
    {
396 8
        return $this->holdingWeb;
397
    }
398
399
    public function setHoldingWeb(?TholianWeb $web): Spacecraft
400
    {
401
        $this->holdingWeb = $web;
402
403
        return $this;
404
    }
405
406 36
    public function getRump(): SpacecraftRump
407
    {
408 36
        return $this->rump;
409
    }
410
411 19
    public function getRumpId(): int
412
    {
413 19
        return $this->getRump()->getId();
414
    }
415
416 19
    public function getRumpName(): string
417
    {
418 19
        return $this->getRump()->getName();
419
    }
420
421
    public function setRump(SpacecraftRump $shipRump): Spacecraft
422
    {
423
        $this->rump = $shipRump;
424
        return $this;
425
    }
426
427 5
    public function getState(): SpacecraftStateEnum
428
    {
429 5
        return $this->getCondition()->getState();
430
    }
431
432
    public function __toString(): string
433
    {
434
        if ($this->id !== null) {
435
            return sprintf(
436
                "id: %d, name: %s",
437
                $this->getId(),
438
                $this->getName()
439
            );
440
        }
441
442
        return $this->getName();
443
    }
444
}
445