Passed
Push — dev ( 36270f...5b972f )
by Janko
10:53
created

Module::getCostSorted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
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 10
ccs 0
cts 7
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 Stu\Module\ShipModule\ModuleTypeDescriptionMapper;
21
22
/**
23
 * @Entity(repositoryClass="Stu\Orm\Repository\ModuleRepository")
24
 * @Table(
25
 *     name="stu_modules",
26
 *     indexes={
27
 *         @Index(name="ship_rump_role_type_idx", columns={"rumps_role_id", "type"})
28
 *     }
29
 * )
30
 **/
31
class Module implements ModuleInterface
32
{
33
    /**
34
     * @Id
35
     * @Column(type="integer")
36
     * @GeneratedValue(strategy="IDENTITY")
37
     *
38
     */
39
    private int $id;
40
41
    /**
42
     * @Column(type="string")
43
     *
44
     */
45
    private string $name = '';
46
47
    /**
48
     * @Column(type="smallint")
49
     *
50
     */
51
    private int $level = 0;
52
53
    /**
54
     * @Column(type="smallint")
55
     *
56
     */
57
    private int $upgrade_factor = 0;
58
59
    /**
60
     * @Column(type="smallint")
61
     *
62
     */
63
    private int $default_factor = 0;
64
65
    /**
66
     * @Column(type="smallint")
67
     *
68
     */
69
    private int $downgrade_factor = 0;
70
71
    /**
72
     * @Column(type="smallint")
73
     *
74
     */
75
    private int $crew = 0;
76
77
    /**
78
     * @Column(type="integer")
79
     *
80
     */
81
    private int $type = 0;
82
83
    /**
84
     * @Column(type="integer", nullable=true)
85
     *
86
     */
87
    private ?int $research_id = 0;
88
89
    /**
90
     * @Column(type="integer")
91
     *
92
     */
93
    private int $commodity_id = 0;
94
95
    /**
96
     * @Column(type="boolean")
97
     *
98
     */
99
    private bool $viewable = false;
100
101
    /**
102
     * @Column(type="integer", nullable=true)
103
     *
104
     */
105
    private ?int $rumps_role_id = 0;
106
107
    /**
108
     * @Column(type="smallint")
109
     *
110
     */
111
    private int $ecost = 0;
112
113
    /**
114
     * @Column(type="integer", nullable=true)
115
     *
116
     */
117
    private ?int $faction_id = null;
118
119
    /**
120
     * @var ResearchInterface
121
     *
122
     * @ManyToOne(targetEntity="Research")
123
     * @JoinColumn(name="research_id", referencedColumnName="id")
124
     */
125
    private $research;
0 ignored issues
show
introduced by
The private property $research is not used, and could be removed.
Loading history...
126
127
    /**
128
     *
129
     * @ManyToOne(targetEntity="Commodity")
130
     * @JoinColumn(name="commodity_id", referencedColumnName="id", onDelete="CASCADE")
131
     */
132
    private CommodityInterface $commodity;
133
134
    /**
135
     * @var FactionInterface
136
     *
137
     * @ManyToOne(targetEntity="Faction")
138
     * @JoinColumn(name="faction_id", referencedColumnName="id")
139
     */
140
    private $faction;
141
142
    /**
143
     * @var ShipRumpRoleInterface
144
     *
145
     * @ManyToOne(targetEntity="ShipRumpRole")
146
     * @JoinColumn(name="rumps_role_id", referencedColumnName="id")
147
     */
148
    private $shipRumpRole;
0 ignored issues
show
introduced by
The private property $shipRumpRole is not used, and could be removed.
Loading history...
149
150
    /**
151
     * @var ArrayCollection<int, ModuleSpecialInterface>
152
     *
153
     * @OneToMany(targetEntity="ModuleSpecial", mappedBy="module")
154
     * @OrderBy({"special_id": "ASC"})
155
     */
156
    private Collection $moduleSpecials;
157
158
    /**
159
     * @var ArrayCollection<int, ModuleCostInterface>
160
     *
161
     * @OneToMany(targetEntity="ModuleCost", mappedBy="module")
162
     */
163
    private Collection $buildingCosts;
164
165
    /**
166
     * @var ArrayCollection<int, TorpedoHullInterface>
167
     *
168
     * @OneToMany(targetEntity="TorpedoHull", mappedBy="module", indexBy="torpedo_type")
169
     * @OrderBy({"torpedo_type": "ASC"})
170
     */
171
    private Collection $torpedoHull;
172
173
    /**
174
     * @var ArrayCollection<int, WeaponShieldInterface>
175
     *
176
     * @OneToMany(targetEntity="WeaponShield", mappedBy="module", indexBy="weapon_id")
177
     * @OrderBy({"weapon_id": "ASC"})
178
     */
179
    private Collection $weaponShield;
180
181
    /**
182
     * @OneToOne(targetEntity="Weapon", mappedBy="module")
183
     */
184
    private ?WeaponInterface $weapon = null;
185
186
    /** @var null|array<int> */
187
    private ?array $specialAbilities = null;
188
189
    public function __construct()
190
    {
191
        $this->moduleSpecials = new ArrayCollection();
192
        $this->buildingCosts = new ArrayCollection();
193
        $this->torpedoHull = new ArrayCollection();
194
        $this->weaponShield = new ArrayCollection();
195
    }
196
197
    public function getId(): int
198
    {
199
        return $this->id;
200
    }
201
202
    public function getName(): string
203
    {
204
        return $this->name;
205
    }
206
207
    public function setName(string $name): ModuleInterface
208
    {
209
        $this->name = $name;
210
211
        return $this;
212
    }
213
214
    public function getLevel(): int
215
    {
216
        return $this->level;
217
    }
218
219
    public function setLevel(int $level): ModuleInterface
220
    {
221
        $this->level = $level;
222
223
        return $this;
224
    }
225
226
    public function getUpgradeFactor(): int
227
    {
228
        return $this->upgrade_factor;
229
    }
230
231
    public function setUpgradeFactor(int $upgradeFactor): ModuleInterface
232
    {
233
        $this->upgrade_factor = $upgradeFactor;
234
235
        return $this;
236
    }
237
238
    public function getDefaultFactor(): int
239
    {
240
        return $this->default_factor;
241
    }
242
243
    public function setDefaultFactor(int $defaultFactor): ModuleInterface
244
    {
245
        $this->default_factor = $defaultFactor;
246
247
        return $this;
248
    }
249
250
    public function getDowngradeFactor(): int
251
    {
252
        return $this->downgrade_factor;
253
    }
254
255
    public function setDowngradeFactor(int $downgradeFactor): ModuleInterface
256
    {
257
        $this->downgrade_factor = $downgradeFactor;
258
259
        return $this;
260
    }
261
262
    public function getCrew(): int
263
    {
264
        return $this->crew;
265
    }
266
267
    public function getCrewByFactionAndRumpLvl(?int $factionId, int $rumpModuleLvl = null): int
268
    {
269
        $result = $this->getCrew();
270
271
        if (
272
            $this->getFactionId() !== null
273
            && $this->getFactionId() !== $factionId
274
        ) {
275
            $result += 1;
276
        }
277
278
        if (
279
            $rumpModuleLvl !== null
280
            && $this->getLevel() > $rumpModuleLvl
281
        ) {
282
            $result += 1;
283
        }
284
285
        return $result;
286
    }
287
288
    public function setCrew(int $crew): ModuleInterface
289
    {
290
        $this->crew = $crew;
291
292
        return $this;
293
    }
294
295
    public function getType(): int
296
    {
297
        return $this->type;
298
    }
299
300
    public function setType(int $type): ModuleInterface
301
    {
302
        $this->type = $type;
303
304
        return $this;
305
    }
306
307
    public function getResearchId(): int
308
    {
309
        return $this->research_id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->research_id could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
310
    }
311
312
    public function setResearchId(int $researchId): ModuleInterface
313
    {
314
        $this->research_id = $researchId;
315
316
        return $this;
317
    }
318
319
    public function getCommodityId(): int
320
    {
321
        return $this->commodity_id;
322
    }
323
324
    public function setCommodityId(int $commodityId): ModuleInterface
325
    {
326
        $this->commodity_id = $commodityId;
327
328
        return $this;
329
    }
330
331
    public function getViewable(): bool
332
    {
333
        return $this->viewable;
334
    }
335
336
    public function setViewable(bool $viewable): ModuleInterface
337
    {
338
        $this->viewable = $viewable;
339
340
        return $this;
341
    }
342
343
    public function getShipRumpRoleId(): int
344
    {
345
        return $this->rumps_role_id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->rumps_role_id could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
346
    }
347
348
    public function setShipRumpRoleId(int $shipRumpRoleId): ModuleInterface
349
    {
350
        $this->rumps_role_id = $shipRumpRoleId;
351
352
        return $this;
353
    }
354
355
    public function getWeapon(): ?WeaponInterface
356
    {
357
        return $this->weapon;
358
    }
359
360
    public function getEcost(): int
361
    {
362
        return $this->ecost;
363
    }
364
365
    public function setEcost(int $energyCosts): ModuleInterface
366
    {
367
        $this->ecost = $energyCosts;
368
369
        return $this;
370
    }
371
372
    public function getFactionId(): ?int
373
    {
374
        return $this->faction_id;
375
    }
376
377
    public function setFactionId(int $factionId): ?ModuleInterface
378
    {
379
        $this->faction_id = $factionId;
380
381
        return $this;
382
    }
383
384
    public function hasSpecial($special_id): bool
385
    {
386
        if ($this->specialAbilities === null) {
387
            $this->specialAbilities = array_map(
388
                fn (ModuleSpecialInterface $moduleSpecial): int => $moduleSpecial->getSpecialId(),
389
                $this->getSpecials()->toArray()
390
            );
391
        }
392
        return in_array((int)$special_id, $this->specialAbilities);
0 ignored issues
show
Bug introduced by
It seems like $this->specialAbilities can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

392
        return in_array((int)$special_id, /** @scrutinizer ignore-type */ $this->specialAbilities);
Loading history...
393
    }
394
395
    public function getSpecials(): Collection
396
    {
397
        return $this->moduleSpecials;
398
    }
399
400
    public function getCost(): Collection
401
    {
402
        return $this->buildingCosts;
403
    }
404
405
    public function getCostSorted(): array
406
    {
407
        $array = $this->getCost()->getValues();
408
409
        usort(
410
            $array,
411
            fn (ModuleCostInterface $a, ModuleCostInterface $b): int => $a->getCommodity()->getSort() <=> $b->getCommodity()->getSort()
412
        );
413
414
        return array_values($array);
415
    }
416
417
    public function getCommodity(): CommodityInterface
418
    {
419
        return $this->commodity;
420
    }
421
422
    public function getDescription(): string
423
    {
424
        return ModuleTypeDescriptionMapper::getDescription($this->getType());
425
    }
426
427
    public function getTorpedoHull(): Collection
428
    {
429
        return $this->torpedoHull;
430
    }
431
432
    public function getWeaponShield(): Collection
433
    {
434
        return $this->weaponShield;
435
    }
436
437
    public function getFaction(): ?FactionInterface
438
    {
439
        return $this->faction;
440
    }
441
}
442