Passed
Push — master ( 6b02b9...a180a6 )
by Nico
26:39 queued 18:43
created

StarSystemMap::getFieldStyle()   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
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
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\OneToOne;
17
use Doctrine\ORM\Mapping\OrderBy;
18
use Doctrine\ORM\Mapping\Table;
19
use Doctrine\ORM\Mapping\UniqueConstraint;
20
use Stu\Component\Map\MapEnum;
21
use Stu\Lib\SectorString;
22
23
/**
24
 * @Entity(repositoryClass="Stu\Orm\Repository\StarSystemMapRepository")
25
 * @Table(
26
 *     name="stu_sys_map",
27
 *     uniqueConstraints={@UniqueConstraint(name="system_coordinates_idx", columns={"sx", "sy", "systems_id"})}
28
 * )
29
 **/
30
class StarSystemMap implements StarSystemMapInterface
31
{
32
    /**
33
     * @Id
34
     * @Column(type="integer")
35
     * @GeneratedValue(strategy="IDENTITY")
36
     */
37
    private int $id;
38
39
    /**
40
     * @Column(type="smallint")
41
     *
42
     */
43
    private int $sx = 0;
44
45
    /**
46
     * @Column(type="smallint")
47
     *
48
     */
49
    private int $sy = 0;
50
51
    /**
52
     * @Column(type="integer")
53
     *
54
     */
55
    private int $systems_id = 0;
56
57
    /**
58
     * @Column(type="integer")
59
     *
60
     */
61
    private int $field_id = 0;
62
63
    /**
64
     *
65
     * @ManyToOne(targetEntity="StarSystem", inversedBy="fields")
66
     * @JoinColumn(name="systems_id", referencedColumnName="id")
67
     */
68
    private StarSystemInterface $starSystem;
69
70
    /**
71
     * @OneToOne(targetEntity="Colony", mappedBy="starsystem_map")
72
     */
73
    private ?ColonyInterface $colony = null;
74
75
    /**
76
     *
77
     * @ManyToOne(targetEntity="MapFieldType")
78
     * @JoinColumn(name="field_id", referencedColumnName="id")
79
     */
80
    private MapFieldTypeInterface $mapFieldType;
81
82
    /**
83
     * @var ArrayCollection<int, ShipInterface>
84
     *
85
     * @OneToMany(targetEntity="Ship", mappedBy="starsystem_map", fetch="EXTRA_LAZY")
86
     */
87
    private Collection $ships;
88
89
    /**
90
     * @var ArrayCollection<int, FlightSignatureInterface>
91
     *
92
     * @OneToMany(targetEntity="FlightSignature", mappedBy="starsystem_map")
93
     * @OrderBy({"time": "DESC"})
94
     */
95
    private Collection $signatures;
96
97
    /**
98
     * @var ArrayCollection<int, AnomalyInterface>
99
     *
100
     * @OneToMany(targetEntity="Anomaly", mappedBy="starsystem_map", fetch="EXTRA_LAZY")
101
     */
102
    private Collection $anomalies;
103
104
    /**
105
     * @var ArrayCollection<int, WormholeEntryInterface>
106
     *
107
     * @OneToMany(targetEntity="WormholeEntry", mappedBy="systemMap")
108
     */
109
    private Collection $wormholeEntries;
110
111
    public function __construct()
112
    {
113
        $this->ships = new ArrayCollection();
114
        $this->signatures = new ArrayCollection();
115
        $this->anomalies = new ArrayCollection();
116
        $this->wormholeEntries = new ArrayCollection();
117
    }
118
119
    public function getId(): int
120
    {
121
        return $this->id;
122
    }
123
124
    public function getSx(): int
125
    {
126
        return $this->sx;
127
    }
128
129
    public function setSx(int $sx): StarSystemMapInterface
130
    {
131
        $this->sx = $sx;
132
133
        return $this;
134
    }
135
136
    public function getX(): int
137
    {
138
        return $this->getSx();
139
    }
140
141
    public function getSy(): int
142
    {
143
        return $this->sy;
144
    }
145
146
    public function setSy(int $sy): StarSystemMapInterface
147
    {
148
        $this->sy = $sy;
149
150
        return $this;
151
    }
152
153
    public function getY(): int
154
    {
155
        return $this->getSy();
156
    }
157
158
    public function getSystemId(): int
159
    {
160
        return $this->systems_id;
161
    }
162
163
    public function getSystem(): StarSystemInterface
164
    {
165
        return $this->starSystem;
166
    }
167
168
    public function setSystem(StarSystemInterface $starSystem): StarSystemMapInterface
169
    {
170
        $this->starSystem = $starSystem;
171
172
        return $this;
173
    }
174
175
    public function getFieldId(): int
176
    {
177
        return $this->field_id;
178
    }
179
180
    public function getFieldType(): MapFieldTypeInterface
181
    {
182
        return $this->mapFieldType;
183
    }
184
185
    public function setFieldType(MapFieldTypeInterface $mapFieldType): StarSystemMapInterface
186
    {
187
        $this->mapFieldType = $mapFieldType;
188
189
        return $this;
190
    }
191
192
    public function getColony(): ?ColonyInterface
193
    {
194
        return $this->colony;
195
    }
196
197
    public function getMapRegion(): ?MapRegionInterface
198
    {
199
        return null;
200
    }
201
202
    public function getAdministratedRegion(): ?MapRegionInterface
203
    {
204
        return null;
205
    }
206
207
    public function getInfluenceArea(): ?StarSystemInterface
208
    {
209
        return null;
210
    }
211
212
    public function getFieldStyle(): string
213
    {
214
        return "background-image: url('/assets/map/" . $this->getFieldId() . ".png'); opacity:1;";
215
    }
216
217
    public function getShips(): Collection
218
    {
219
        return $this->ships;
220
    }
221
222
    public function getSignatures(): Collection
223
    {
224
        return $this->signatures;
225
    }
226
227
    public function getAnomalies(): Collection
228
    {
229
        return $this->anomalies;
230
    }
231
232
    public function getRandomWormholeEntry(): ?WormholeEntryInterface
233
    {
234
        if ($this->wormholeEntries->isEmpty()) {
235
            return null;
236
        }
237
238
        $usableEntries =  array_filter(
239
            $this->wormholeEntries->toArray(),
240
            function (WormholeEntryInterface $entry): bool {
241
                $type = $entry->getType();
242
243
                return $entry->isUsable() && ($type === MapEnum::WORMHOLE_ENTRY_TYPE_BOTH ||
244
                    $type === MapEnum::WORMHOLE_ENTRY_TYPE_OUT);
245
            }
246
        );
247
248
        return $usableEntries === [] ? null : $usableEntries[array_rand($usableEntries)];
249
    }
250
251
    public function getSectorString(): string
252
    {
253
        return SectorString::getForStarSystemMap($this);
254
    }
255
}
256