Passed
Pull Request — master (#1846)
by Nico
32:16
created

Location::hasAnomaly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
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\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_field_type_idx', columns: ['field_id'])]
29
#[Entity(repositoryClass: LocationRepository::class)]
30
#[InheritanceType('JOINED')]
31
#[DiscriminatorColumn(name: 'discr', type: 'string')]
32
#[DiscriminatorMap(['map' => Map::class, 'systemMap' => StarSystemMap::class])]
33
abstract class Location implements LocationInterface
34
{
35
    #[Id]
36
    #[Column(type: 'integer')]
37
    #[GeneratedValue(strategy: 'IDENTITY')]
38
    private int $id;
39
40
    #[Column(type: 'integer', nullable: true)]
41
    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...
42
43
    #[Column(type: 'integer', nullable: true)]
44
    private ?int $cx = null;
45
46
    #[Column(type: 'integer', nullable: true)]
47
    private ?int $cy = null;
48
49
    #[Column(type: 'integer')]
50
    private int $field_id = 0;
51
52
    #[Column(type: 'integer', nullable: true)]
53
    private ?int $map_id = null;
0 ignored issues
show
introduced by
The private property $map_id is not used, and could be removed.
Loading history...
54
55
    #[Column(type: 'integer', nullable: true)]
56
    private ?int $starsystem_map_id = null;
0 ignored issues
show
introduced by
The private property $starsystem_map_id is not used, and could be removed.
Loading history...
57
58
    #[ManyToOne(targetEntity: 'Layer')]
59
    #[JoinColumn(name: 'layer_id', referencedColumnName: 'id')]
60
    protected ?LayerInterface $layer;
61
62
    #[ManyToOne(targetEntity: 'MapFieldType')]
63
    #[JoinColumn(name: 'field_id', referencedColumnName: 'id')]
64
    private MapFieldTypeInterface $mapFieldType;
65
66
    /**
67
     * @var ArrayCollection<int, ShipInterface>
68
     */
69
    #[OneToMany(targetEntity: 'Ship', mappedBy: 'location', fetch: 'EXTRA_LAZY')]
70
    private Collection $ships;
71
72
    /**
73
     * @var ArrayCollection<int, FlightSignatureInterface>
74
     */
75
    #[OneToMany(targetEntity: 'FlightSignature', mappedBy: 'location')]
76
    #[OrderBy(['time' => 'DESC'])]
77
    private Collection $signatures;
78
79
    /**
80
     * @var ArrayCollection<int, BuoyInterface>
81
     */
82
    #[OneToMany(targetEntity: 'Buoy', mappedBy: 'location')]
83
    private Collection $buoys;
84
85
    /**
86
     * @var ArrayCollection<int, AnomalyInterface>
87
     */
88
    #[OneToMany(targetEntity: 'Anomaly', mappedBy: 'location', fetch: 'EXTRA_LAZY')]
89
    private Collection $anomalies;
90
91
    public function __construct()
92
    {
93
        $this->ships = new ArrayCollection();
94
        $this->signatures = new ArrayCollection();
95
        $this->buoys = new ArrayCollection();
96
        $this->anomalies = new ArrayCollection();
97
    }
98
99
    #[Override]
100
    public function getId(): int
101
    {
102
        return $this->id;
103
    }
104
105
    #[Override]
106
    public function getCx(): ?int
107
    {
108
        return $this->cx;
109
    }
110
111
    #[Override]
112
    public function getCy(): ?int
113
    {
114
        return $this->cy;
115
    }
116
117
    #[Override]
118
    public function getFieldId(): int
119
    {
120
        return $this->field_id;
121
    }
122
123
    #[Override]
124
    public function getFieldType(): MapFieldTypeInterface
125
    {
126
        return $this->mapFieldType;
127
    }
128
129
    #[Override]
130
    public function setFieldType(MapFieldTypeInterface $mapFieldType): LocationInterface
131
    {
132
        $this->mapFieldType = $mapFieldType;
133
134
        return $this;
135
    }
136
137
    #[Override]
138
    public function getShips(): Collection
139
    {
140
        return $this->ships;
141
    }
142
143
    #[Override]
144
    public function getBuoys(): Collection
145
    {
146
        return $this->buoys;
147
    }
148
149
    #[Override]
150
    public function getAnomalies(bool $onlyActive = true): Collection
151
    {
152
        return $this->anomalies
153
            ->filter(fn (AnomalyInterface $anomaly): bool => !$onlyActive || $anomaly->isActive());
154
    }
155
156
    #[Override]
157
    public function hasAnomaly(AnomalyTypeEnum $type): bool
158
    {
159
        return $this->getAnomalies()
160
            ->exists(fn (int $key, AnomalyInterface $anomaly): bool => $anomaly->getAnomalyType()->getId() === $type->value);
161
    }
162
163
    #[Override]
164
    public function getSignatures(): Collection
165
    {
166
        return $this->signatures;
167
    }
168
169
    /**
170
     * @return Collection<int, WormholeEntryInterface>
171
     */
172
    protected abstract function getWormholeEntries(): Collection;
173
174
    #[Override]
175
    public function getRandomWormholeEntry(): ?WormholeEntryInterface
176
    {
177
        $wormholeEntries = $this->getWormholeEntries();
178
        if ($wormholeEntries->isEmpty()) {
179
            return null;
180
        }
181
182
        $usableEntries = $wormholeEntries
183
            ->filter(fn (WormholeEntryInterface $entry): bool => $entry->isUsable($this))
184
            ->toArray();
185
186
        return $usableEntries === [] ? null : $usableEntries[array_rand($usableEntries)];
187
    }
188
189
    #[Override]
190
    public function isMap(): bool
191
    {
192
        return $this instanceof MapInterface;
193
    }
194
195
    #[Override]
196
    public function isOverWormhole(): bool
197
    {
198
        return $this->isMap() && $this->getRandomWormholeEntry() !== null;
199
    }
200
}
201