Passed
Push — dev ( 02e99e...928e2e )
by Nico
12:03
created

Module::getWeaponShield()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
     * @var ResearchInterface
115
     *
116
     * @ManyToOne(targetEntity="Research")
117
     * @JoinColumn(name="research_id", referencedColumnName="id")
118
     */
119
    private $research;
0 ignored issues
show
introduced by
The private property $research is not used, and could be removed.
Loading history...
120
121
    /**
122
     * @var CommodityInterface
123
     *
124
     * @ManyToOne(targetEntity="Commodity")
125
     * @JoinColumn(name="commodity_id", referencedColumnName="id", onDelete="CASCADE")
126
     */
127
    private $commodity;
128
129
    /**
130
     * @var ShipRumpRoleInterface
131
     *
132
     * @ManyToOne(targetEntity="ShipRumpRole")
133
     * @JoinColumn(name="rumps_role_id", referencedColumnName="id")
134
     */
135
    private $shipRumpRole;
0 ignored issues
show
introduced by
The private property $shipRumpRole is not used, and could be removed.
Loading history...
136
137
    /**
138
     * @var ArrayCollection<int, ModuleSpecialInterface>
139
     *
140
     * @OneToMany(targetEntity="ModuleSpecial", mappedBy="module")
141
     * @OrderBy({"special_id": "ASC"})
142
     */
143
    private $moduleSpecials;
144
145
    /**
146
     * @var ArrayCollection<int, ModuleCostInterface>
147
     *
148
     * @OneToMany(targetEntity="ModuleCost", mappedBy="module")
149
     */
150
    private $buildingCosts;
151
152
    /**
153
     * @var ArrayCollection<int, TorpedoHullInterface>
154
     *
155
     * @OneToMany(targetEntity="TorpedoHull", mappedBy="module", indexBy="torpedo_type")
156
     * @OrderBy({"torpedo_type": "ASC"})
157
     */
158
    private $torpedoHull;
159
160
    /**
161
     * @var ArrayCollection<int, WeaponShieldInterface>
162
     *
163
     * @OneToMany(targetEntity="WeaponShield", mappedBy="module", indexBy="weapon_id")
164
     * @OrderBy({"weapon_id": "ASC"})
165
     */
166
    private $weaponShield;
167
168
    /**
169
     * @OneToOne(targetEntity="Weapon", mappedBy="module")
170
     */
171
    private ?WeaponInterface $weapon;
172
173
    /** @var null|array<int> */
174
    private $specialAbilities;
175
176
    public function __construct()
177
    {
178
        $this->moduleSpecials = new ArrayCollection();
179
        $this->buildingCosts = new ArrayCollection();
180
        $this->torpedoHull = new ArrayCollection();
181
        $this->weaponShield = new ArrayCollection();
182
    }
183
184
    public function getId(): int
185
    {
186
        return $this->id;
187
    }
188
189
    public function getName(): string
190
    {
191
        return $this->name;
192
    }
193
194
    public function setName(string $name): ModuleInterface
195
    {
196
        $this->name = $name;
197
198
        return $this;
199
    }
200
201
    public function getLevel(): int
202
    {
203
        return $this->level;
204
    }
205
206
    public function setLevel(int $level): ModuleInterface
207
    {
208
        $this->level = $level;
209
210
        return $this;
211
    }
212
213
    public function getUpgradeFactor(): int
214
    {
215
        return $this->upgrade_factor;
216
    }
217
218
    public function setUpgradeFactor(int $upgradeFactor): ModuleInterface
219
    {
220
        $this->upgrade_factor = $upgradeFactor;
221
222
        return $this;
223
    }
224
225
    public function getDefaultFactor(): int
226
    {
227
        return $this->default_factor;
228
    }
229
230
    public function setDefaultFactor(int $defaultFactor): ModuleInterface
231
    {
232
        $this->default_factor = $defaultFactor;
233
234
        return $this;
235
    }
236
237
    public function getDowngradeFactor(): int
238
    {
239
        return $this->downgrade_factor;
240
    }
241
242
    public function setDowngradeFactor(int $downgradeFactor): ModuleInterface
243
    {
244
        $this->downgrade_factor = $downgradeFactor;
245
246
        return $this;
247
    }
248
249
    public function getCrew(): int
250
    {
251
        return $this->crew;
252
    }
253
254
    public function setCrew(int $crew): ModuleInterface
255
    {
256
        $this->crew = $crew;
257
258
        return $this;
259
    }
260
261
    public function getType(): int
262
    {
263
        return $this->type;
264
    }
265
266
    public function setType(int $type): ModuleInterface
267
    {
268
        $this->type = $type;
269
270
        return $this;
271
    }
272
273
    public function getResearchId(): int
274
    {
275
        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...
276
    }
277
278
    public function setResearchId(int $researchId): ModuleInterface
279
    {
280
        $this->research_id = $researchId;
281
282
        return $this;
283
    }
284
285
    public function getCommodityId(): int
286
    {
287
        return $this->commodity_id;
288
    }
289
290
    public function setCommodityId(int $commodityId): ModuleInterface
291
    {
292
        $this->commodity_id = $commodityId;
293
294
        return $this;
295
    }
296
297
    public function getViewable(): bool
298
    {
299
        return $this->viewable;
300
    }
301
302
    public function setViewable(bool $viewable): ModuleInterface
303
    {
304
        $this->viewable = $viewable;
305
306
        return $this;
307
    }
308
309
    public function getShipRumpRoleId(): int
310
    {
311
        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...
312
    }
313
314
    public function setShipRumpRoleId(int $shipRumpRoleId): ModuleInterface
315
    {
316
        $this->rumps_role_id = $shipRumpRoleId;
317
318
        return $this;
319
    }
320
321
    public function getWeapon(): ?WeaponInterface
322
    {
323
        return $this->weapon;
324
    }
325
326
    public function getEcost(): int
327
    {
328
        return $this->ecost;
329
    }
330
331
    public function setEcost(int $energyCosts): ModuleInterface
332
    {
333
        $this->ecost = $energyCosts;
334
335
        return $this;
336
    }
337
338
    public function hasSpecial($special_id): bool
339
    {
340
        if ($this->specialAbilities === null) {
341
            $this->specialAbilities = array_map(
342
                function (ModuleSpecialInterface $moduleSpecial): int {
343
                    return (int)$moduleSpecial->getSpecialId();
344
                },
345
                $this->getSpecials()->toArray()
346
            );
347
        }
348
        return in_array((int)$special_id, $this->specialAbilities);
349
    }
350
351
    public function getSpecials(): Collection
352
    {
353
        return $this->moduleSpecials;
354
    }
355
356
    public function getCost(): Collection
357
    {
358
        return $this->buildingCosts;
359
    }
360
361
    public function getCostSorted(): array
362
    {
363
        $array = $this->getCost()->getValues();
364
365
        usort(
366
            $array,
367
            function (ModuleCostInterface $a, ModuleCostInterface $b): int {
368
                if ($a->getCommodity()->getSort() == $b->getCommodity()->getSort()) {
369
                    return 0;
370
                }
371
                return ($a->getCommodity()->getSort() < $b->getCommodity()->getSort()) ? -1 : 1;
372
            }
373
        );
374
375
        return array_values($array);
376
    }
377
378
    public function getCommodity(): CommodityInterface
379
    {
380
        return $this->commodity;
381
    }
382
383
    public function getDescription(): string
384
    {
385
        return ModuleTypeDescriptionMapper::getDescription($this->getType());
386
    }
387
388
    public function getTorpedoHull(): Collection
389
    {
390
        return $this->torpedoHull;
391
    }
392
393
394
    public function getWeaponShield(): Collection
395
    {
396
        return $this->weaponShield;
397
    }
398
}
399