Passed
Push — dev ( 45bf8d...5b8f46 )
by Janko
10:07
created

Location   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Test Coverage

Coverage 44.9%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 88
dl 0
loc 205
ccs 22
cts 49
cp 0.449
rs 10
c 1
b 0
f 1
wmc 22

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getCx() 0 4 1
A getTrumfields() 0 4 1
A getCy() 0 4 1
A getId() 0 4 1
A __construct() 0 7 1
A getSpacecrafts() 0 4 1
A getFieldType() 0 4 1
A setFieldType() 0 6 1
A getFieldId() 0 4 1
A getBuoys() 0 4 1
A isOverWormhole() 0 4 2
A addAnomaly() 0 3 1
A getAnomaly() 0 4 1
A isMap() 0 4 1
A getLocationMinings() 0 4 1
A getSignatures() 0 4 1
A getRandomWormholeEntry() 0 13 3
A hasAnomaly() 0 4 1
A getAnomalies() 0 4 1
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\Index;
16
use Doctrine\ORM\Mapping\InheritanceType;
17
use Doctrine\ORM\Mapping\JoinColumn;
18
use Doctrine\ORM\Mapping\ManyToOne;
19
use Doctrine\ORM\Mapping\OneToMany;
20
use Doctrine\ORM\Mapping\OrderBy;
21
use Doctrine\ORM\Mapping\Table;
22
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...
23
use Stu\Component\Anomaly\Type\AnomalyTypeEnum;
24
use Stu\Orm\Repository\LocationRepository;
25
26
#[Table(name: 'stu_location')]
27
#[Index(name: 'location_coords_idx', columns: ['layer_id', 'cx', 'cy'])]
28
#[Index(name: 'location_coords_reverse_idx', columns: ['layer_id', 'cy', 'cx'])]
29
#[Index(name: 'location_field_type_idx', columns: ['field_id'])]
30
#[Entity(repositoryClass: LocationRepository::class)]
31
#[InheritanceType('JOINED')]
32
#[DiscriminatorColumn(name: 'discr', type: 'string')]
33
#[DiscriminatorMap([
34
    'map' => Map::class,
35
    'systemMap' => StarSystemMap::class
36
])]
37
abstract class Location implements LocationInterface
38
{
39
    #[Id]
40
    #[Column(type: 'integer')]
41
    #[GeneratedValue(strategy: 'IDENTITY')]
42
    private int $id;
43
44
    #[Column(type: 'integer', nullable: true)]
45
    private ?int $layer_id = null;
0 ignored issues
show
introduced by
The private property $layer_id is not used, and could be removed.
Loading history...
46
47
    #[Column(type: 'integer', nullable: true)]
48
    private ?int $cx = null;
49
50
    #[Column(type: 'integer', nullable: true)]
51
    private ?int $cy = null;
52
53
    #[Column(type: 'integer')]
54
    private int $field_id = 0;
55
56
    #[ManyToOne(targetEntity: 'Layer')]
57
    #[JoinColumn(name: 'layer_id', referencedColumnName: 'id')]
58
    protected ?LayerInterface $layer;
59
60
    #[ManyToOne(targetEntity: 'MapFieldType')]
61
    #[JoinColumn(name: 'field_id', referencedColumnName: 'id')]
62
    private MapFieldTypeInterface $mapFieldType;
63
64
    /**
65
     * @var ArrayCollection<int, SpacecraftInterface>
66
     */
67
    #[OneToMany(targetEntity: 'Spacecraft', mappedBy: 'location', indexBy: 'id', fetch: 'EXTRA_LAZY')]
68
    private Collection $spacecrafts;
69
70
    /**
71
     * @var ArrayCollection<int, TrumfieldInterface>
72
     */
73
    #[OneToMany(targetEntity: 'Trumfield', mappedBy: 'location', indexBy: 'id', fetch: 'EXTRA_LAZY')]
74
    private Collection $trumfields;
75
76
    /**
77
     * @var ArrayCollection<int, FlightSignatureInterface>
78
     */
79
    #[OneToMany(targetEntity: 'FlightSignature', mappedBy: 'location')]
80
    #[OrderBy(['time' => 'DESC'])]
81
    private Collection $signatures;
82
83
    /**
84
     * @var ArrayCollection<int, BuoyInterface>
85
     */
86
    #[OneToMany(targetEntity: 'Buoy', mappedBy: 'location', indexBy: 'id', fetch: 'EXTRA_LAZY')]
87
    private Collection $buoys;
88
89
    /**
90
     * @var ArrayCollection<int, AnomalyInterface>
91
     */
92
    #[OneToMany(targetEntity: 'Anomaly', mappedBy: 'location', indexBy: 'anomaly_type_id', fetch: 'EXTRA_LAZY')]
93
    private Collection $anomalies;
94
95
    /**
96
     * @var ArrayCollection<int, LocationMiningInterface>
97
     */
98
    #[OneToMany(targetEntity: 'LocationMining', mappedBy: 'location')]
99
    private Collection $locationMinings;
100
101
    public function __construct()
102
    {
103
        $this->spacecrafts = new ArrayCollection();
104
        $this->trumfields = new ArrayCollection();
105
        $this->signatures = new ArrayCollection();
106
        $this->buoys = new ArrayCollection();
107
        $this->anomalies = new ArrayCollection();
108
    }
109
110 6
    #[Override]
111
    public function getId(): int
112
    {
113 6
        return $this->id;
114
    }
115
116 19
    #[Override]
117
    public function getCx(): ?int
118
    {
119 19
        return $this->cx;
120
    }
121
122 19
    #[Override]
123
    public function getCy(): ?int
124
    {
125 19
        return $this->cy;
126
    }
127
128 2
    #[Override]
129
    public function getFieldId(): int
130
    {
131 2
        return $this->field_id;
132
    }
133
134 1
    #[Override]
135
    public function getFieldType(): MapFieldTypeInterface
136
    {
137 1
        return $this->mapFieldType;
138
    }
139
140
    #[Override]
141
    public function setFieldType(MapFieldTypeInterface $mapFieldType): LocationInterface
142
    {
143
        $this->mapFieldType = $mapFieldType;
144
145
        return $this;
146
    }
147
148 6
    #[Override]
149
    public function getSpacecrafts(): Collection
150
    {
151 6
        return $this->spacecrafts;
152
    }
153
154 1
    #[Override]
155
    public function getTrumfields(): Collection
156
    {
157 1
        return $this->trumfields;
158
    }
159
160 1
    #[Override]
161
    public function getBuoys(): Collection
162
    {
163 1
        return $this->buoys;
164
    }
165
166 2
    #[Override]
167
    public function getAnomalies(): Collection
168
    {
169 2
        return $this->anomalies;
170
    }
171
172
    public function addAnomaly(AnomalyInterface $anomaly): void
173
    {
174
        $this->anomalies->set($anomaly->getAnomalyType()->getId(), $anomaly);
175
    }
176
177
    #[Override]
178
    public function hasAnomaly(AnomalyTypeEnum $type): bool
179
    {
180
        return $this->anomalies->containsKey($type->value);
181
    }
182
183
    #[Override]
184
    public function getAnomaly(AnomalyTypeEnum $type): ?AnomalyInterface
185
    {
186
        return $this->anomalies->get($type->value);
187
    }
188
189
    #[Override]
190
    public function getSignatures(): Collection
191
    {
192
        return $this->signatures;
193
    }
194
195
    /**
196
     * @return Collection<int, WormholeEntryInterface>
197
     */
198
    protected abstract function getWormholeEntries(): Collection;
199
200 2
    #[Override]
201
    public function getRandomWormholeEntry(): ?WormholeEntryInterface
202
    {
203 2
        $wormholeEntries = $this->getWormholeEntries();
204 2
        if ($wormholeEntries->isEmpty()) {
205 2
            return null;
206
        }
207
208
        $usableEntries = $wormholeEntries
209
            ->filter(fn(WormholeEntryInterface $entry): bool => $entry->isUsable($this))
210
            ->toArray();
211
212
        return $usableEntries === [] ? null : $usableEntries[array_rand($usableEntries)];
213
    }
214
215
    #[Override]
216
    public function isMap(): bool
217
    {
218
        return $this instanceof MapInterface;
219
    }
220
221
    #[Override]
222
    public function isOverWormhole(): bool
223
    {
224
        return $this->isMap() && $this->getRandomWormholeEntry() !== null;
225
    }
226
227
    #[Override]
228
    public function getLocationMinings(): Collection
229
    {
230
        return $this->locationMinings;
231
    }
232
}
233