Passed
Push — dev ( c29d7a...3a4f7a )
by Nico
06:58
created

Module::getTorpedoHull()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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_id")
156
     */
157
    private $torpedoHull;
158
159
    /**
160
     * @OneToOne(targetEntity="Weapon", mappedBy="module")
161
     */
162
    private ?WeaponInterface $weapon;
163
164
    /** @var null|array<int> */
165
    private $specialAbilities;
166
167
    public function __construct()
168
    {
169
        $this->moduleSpecials = new ArrayCollection();
170
        $this->buildingCosts = new ArrayCollection();
171
    }
172
173
    public function getId(): int
174
    {
175
        return $this->id;
176
    }
177
178
    public function getName(): string
179
    {
180
        return $this->name;
181
    }
182
183
    public function setName(string $name): ModuleInterface
184
    {
185
        $this->name = $name;
186
187
        return $this;
188
    }
189
190
    public function getLevel(): int
191
    {
192
        return $this->level;
193
    }
194
195
    public function setLevel(int $level): ModuleInterface
196
    {
197
        $this->level = $level;
198
199
        return $this;
200
    }
201
202
    public function getUpgradeFactor(): int
203
    {
204
        return $this->upgrade_factor;
205
    }
206
207
    public function setUpgradeFactor(int $upgradeFactor): ModuleInterface
208
    {
209
        $this->upgrade_factor = $upgradeFactor;
210
211
        return $this;
212
    }
213
214
    public function getDefaultFactor(): int
215
    {
216
        return $this->default_factor;
217
    }
218
219
    public function setDefaultFactor(int $defaultFactor): ModuleInterface
220
    {
221
        $this->default_factor = $defaultFactor;
222
223
        return $this;
224
    }
225
226
    public function getDowngradeFactor(): int
227
    {
228
        return $this->downgrade_factor;
229
    }
230
231
    public function setDowngradeFactor(int $downgradeFactor): ModuleInterface
232
    {
233
        $this->downgrade_factor = $downgradeFactor;
234
235
        return $this;
236
    }
237
238
    public function getCrew(): int
239
    {
240
        return $this->crew;
241
    }
242
243
    public function setCrew(int $crew): ModuleInterface
244
    {
245
        $this->crew = $crew;
246
247
        return $this;
248
    }
249
250
    public function getType(): int
251
    {
252
        return $this->type;
253
    }
254
255
    public function setType(int $type): ModuleInterface
256
    {
257
        $this->type = $type;
258
259
        return $this;
260
    }
261
262
    public function getResearchId(): int
263
    {
264
        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...
265
    }
266
267
    public function setResearchId(int $researchId): ModuleInterface
268
    {
269
        $this->research_id = $researchId;
270
271
        return $this;
272
    }
273
274
    public function getCommodityId(): int
275
    {
276
        return $this->commodity_id;
277
    }
278
279
    public function setCommodityId(int $commodityId): ModuleInterface
280
    {
281
        $this->commodity_id = $commodityId;
282
283
        return $this;
284
    }
285
286
    public function getViewable(): bool
287
    {
288
        return $this->viewable;
289
    }
290
291
    public function setViewable(bool $viewable): ModuleInterface
292
    {
293
        $this->viewable = $viewable;
294
295
        return $this;
296
    }
297
298
    public function getShipRumpRoleId(): int
299
    {
300
        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...
301
    }
302
303
    public function setShipRumpRoleId(int $shipRumpRoleId): ModuleInterface
304
    {
305
        $this->rumps_role_id = $shipRumpRoleId;
306
307
        return $this;
308
    }
309
310
    public function getWeapon(): ?WeaponInterface
311
    {
312
        return $this->weapon;
313
    }
314
315
    public function getEcost(): int
316
    {
317
        return $this->ecost;
318
    }
319
320
    public function setEcost(int $energyCosts): ModuleInterface
321
    {
322
        $this->ecost = $energyCosts;
323
324
        return $this;
325
    }
326
327
    public function hasSpecial($special_id): bool
328
    {
329
        if ($this->specialAbilities === null) {
330
            $this->specialAbilities = array_map(
331
                function (ModuleSpecialInterface $moduleSpecial): int {
332
                    return (int)$moduleSpecial->getSpecialId();
333
                },
334
                $this->getSpecials()->toArray()
335
            );
336
        }
337
        return in_array((int)$special_id, $this->specialAbilities);
338
    }
339
340
    public function getSpecials(): Collection
341
    {
342
        return $this->moduleSpecials;
343
    }
344
345
    public function getCost(): Collection
346
    {
347
        return $this->buildingCosts;
348
    }
349
350
    public function getCostSorted(): array
351
    {
352
        $array = $this->getCost()->getValues();
353
354
        usort(
355
            $array,
356
            function (ModuleCostInterface $a, ModuleCostInterface $b): int {
357
                if ($a->getCommodity()->getSort() == $b->getCommodity()->getSort()) {
358
                    return 0;
359
                }
360
                return ($a->getCommodity()->getSort() < $b->getCommodity()->getSort()) ? -1 : 1;
361
            }
362
        );
363
364
        return array_values($array);
365
    }
366
367
    public function getCommodity(): CommodityInterface
368
    {
369
        return $this->commodity;
370
    }
371
372
    public function getDescription(): string
373
    {
374
        return ModuleTypeDescriptionMapper::getDescription($this->getType());
375
    }
376
377
    public function getTorpedoHull(): Collection
378
    {
379
        return $this->torpedoHull;
380
    }
381
}