Passed
Branch master (9bcc45)
by Janko
41:23
created

ShipBuildplan::getModulesSortedByOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 6
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\JoinColumn;
14
use Doctrine\ORM\Mapping\ManyToOne;
15
use Doctrine\ORM\Mapping\OneToMany;
16
use Doctrine\ORM\Mapping\OrderBy;
17
use Doctrine\ORM\Mapping\Table;
18
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...
19
use Stu\Component\Ship\ShipModuleTypeEnum;
20
use Stu\Orm\Repository\ShipBuildplanRepository;
21
22
#[Table(name: 'stu_buildplans')]
23
#[Entity(repositoryClass: ShipBuildplanRepository::class)]
24
//TODO uniqueIndex on signature
25
//TODO check on creation and tell existing name
26
class ShipBuildplan implements ShipBuildplanInterface
27
{
28
    #[Id]
29
    #[Column(type: 'integer')]
30
    #[GeneratedValue(strategy: 'IDENTITY')]
31
    private int $id;
32
33
    #[Column(type: 'integer')]
34
    private int $rump_id = 0;
35
36
    #[Column(type: 'integer')]
37
    private int $user_id = 0;
38
39
    #[Column(type: 'string')]
40
    private string $name = '';
41
42
    #[Column(type: 'integer')]
43
    private int $buildtime = 0;
44
45
    #[Column(type: 'string', length: 32, nullable: true)]
46
    private ?string $signature = '';
47
48
    #[Column(type: 'smallint')]
49
    private int $crew = 0;
50
51
    /**
52
     * @var Collection<int, ShipInterface>
53
     */
54
    #[OneToMany(targetEntity: 'Ship', mappedBy: 'buildplan', fetch: 'EXTRA_LAZY')]
55
    private Collection $ships;
56
57
    #[ManyToOne(targetEntity: 'ShipRump')]
58
    #[JoinColumn(name: 'rump_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
59
    private ShipRumpInterface $shipRump;
60
61
    #[ManyToOne(targetEntity: 'User')]
62
    #[JoinColumn(name: 'user_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
63
    private UserInterface $user;
64
65
    /**
66
     * @var Collection<int, BuildplanModuleInterface>
67
     */
68
    #[OneToMany(targetEntity: 'BuildplanModule', mappedBy: 'buildplan', indexBy: 'module_id', fetch: 'EXTRA_LAZY')]
69
    #[OrderBy(['module_id' => 'ASC'])]
70
    private Collection $modules;
71
72
    public function __construct()
73
    {
74
        $this->ships = new ArrayCollection();
75
        $this->modules = new ArrayCollection();
76
    }
77
78
    #[Override]
79
    public function getId(): int
80
    {
81
        return $this->id;
82
    }
83
84
    #[Override]
85
    public function getRumpId(): int
86
    {
87
        return $this->rump_id;
88
    }
89
90
    #[Override]
91
    public function setRumpId(int $shipRumpId): ShipBuildplanInterface
92
    {
93
        $this->rump_id = $shipRumpId;
94
95
        return $this;
96
    }
97
98
    #[Override]
99
    public function getUserId(): int
100
    {
101
        return $this->user_id;
102
    }
103
104
    #[Override]
105
    public function getUser(): UserInterface
106
    {
107
        return $this->user;
108
    }
109
110
    #[Override]
111
    public function setUser(UserInterface $user): ShipBuildplanInterface
112
    {
113
        $this->user = $user;
114
        return $this;
115
    }
116
117
    #[Override]
118
    public function getName(): string
119
    {
120
        return $this->name;
121
    }
122
123
    #[Override]
124
    public function setName(string $name): ShipBuildplanInterface
125
    {
126
        $this->name = $name;
127
128
        return $this;
129
    }
130
131
    #[Override]
132
    public function getBuildtime(): int
133
    {
134
        return $this->buildtime;
135
    }
136
137
    #[Override]
138
    public function setBuildtime(int $buildtime): ShipBuildplanInterface
139
    {
140
        $this->buildtime = $buildtime;
141
142
        return $this;
143
    }
144
145
    #[Override]
146
    public function getShipCount(): int
147
    {
148
        return $this->getShiplist()->count();
149
    }
150
151
    #[Override]
152
    public function getSignature(): ?string
153
    {
154
        return $this->signature;
155
    }
156
157
    #[Override]
158
    public function setSignature(?string $signature): ShipBuildplanInterface
159
    {
160
        $this->signature = $signature;
161
162
        return $this;
163
    }
164
165
    #[Override]
166
    public function getCrew(): int
167
    {
168
        return $this->crew;
169
    }
170
171
    #[Override]
172
    public function setCrew(int $crew): ShipBuildplanInterface
173
    {
174
        $this->crew = $crew;
175
176
        return $this;
177
    }
178
179
    #[Override]
180
    public function getShiplist(): Collection
181
    {
182
        return $this->ships;
183
    }
184
185
    #[Override]
186
    public function getRump(): ShipRumpInterface
187
    {
188
        return $this->shipRump;
189
    }
190
191
    #[Override]
192
    public function setRump(ShipRumpInterface $shipRump): ShipBuildplanInterface
193
    {
194
        $this->shipRump = $shipRump;
195
196
        return $this;
197
    }
198
199
    #[Override]
200
    public function getModulesByType(ShipModuleTypeEnum $type): array
201
    {
202
        return $this->getModules()
203
            ->filter(
204
                fn(BuildplanModuleInterface $buildplanModule): bool => $buildplanModule->getModuleType() === $type
205
            )
206
            ->toArray();
207
    }
208
209
    #[Override]
210
    public function getModules(): Collection
211
    {
212
        return $this->getModulesSortedByOrder();
213
    }
214
215
    /** @return Collection<int, BuildplanModuleInterface> */
216
    private function getModulesSortedByOrder(): Collection
217
    {
218
        $array = $this->modules->toArray();
219
220
        uasort($array, function (BuildplanModuleInterface $a, BuildplanModuleInterface $b) {
221
            return $a->getModuleType()->getOrder() <=> $b->getModuleType()->getOrder();
222
        });
223
224
        return new ArrayCollection($array);
225
    }
226
}
227