Passed
Push — master ( 5c1ca2...a370b2 )
by Nico
11:43
created

getShipSubspaceLayerData()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 63
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 53
c 2
b 1
f 0
nc 32
nop 4
dl 0
loc 63
ccs 0
cts 30
cp 0
crap 42
rs 8.4032

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\Query\ResultSetMapping;
9
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...
10
use Stu\Component\Building\BuildingFunctionEnum;
11
use Stu\Component\Ship\FlightSignatureVisibilityEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\FlightSignatureVisibilityEnum 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...
12
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
13
use Stu\Lib\Map\VisualPanel\PanelBoundaries;
14
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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...
15
use Stu\Orm\Entity\AstronomicalEntry;
0 ignored issues
show
Bug introduced by
The type Stu\Orm\Entity\AstronomicalEntry 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...
16
use Stu\Orm\Entity\StarSystem;
17
use Stu\Orm\Entity\StarSystemMap;
18
19
/**
20
 * @extends EntityRepository<StarSystemMap>
21
 */
22
final class StarSystemMapRepository extends EntityRepository implements StarSystemMapRepositoryInterface
23
{
24
    public function getBySystemOrdered(int $starSystemId): array
25
    {
26
        return $this->findBy(
27
            ['systems_id' => $starSystemId],
28
            ['sy' => 'asc', 'sx' => 'asc']
29
        );
30
    }
31
32 2
    #[Override]
33
    public function getByCoordinates(
34
        int $starSystemId,
35
        int $sx,
36
        int $sy
37
    ): ?StarSystemMap {
38 2
        return $this->findOneBy([
39 2
            'systems_id' => $starSystemId,
40 2
            'sx' => $sx,
41 2
            'sy' => $sy
42 2
        ]);
43
    }
44
45
    #[Override]
46
    public function getByBoundaries(PanelBoundaries $boundaries): array
47
    {
48
        return $this->getByCoordinateRange(
49
            $boundaries->getParentId(),
50
            $boundaries->getMinX(),
51
            $boundaries->getMaxX(),
52
            $boundaries->getMinY(),
53
            $boundaries->getMaxY()
54
        );
55
    }
56
57
    #[Override]
58
    public function getByCoordinateRange(
59
        int $starSystemId,
60
        int $startSx,
61
        int $endSx,
62
        int $startSy,
63
        int $endSy,
64
        bool $sortAscending = true
65
    ): array {
66
        return $this->getEntityManager()
67
            ->createQuery(
68
                sprintf(
69
                    'SELECT m FROM %1$s m
70
                    WHERE m.systems_id = :starSystemId AND
71
                        m.sx BETWEEN :startSx AND :endSx AND
72
                        m.sy BETWEEN :startSy AND :endSy
73
                    ORDER BY m.sy %2$s, m.sx %2$s',
74
                    StarSystemMap::class,
75
                    $sortAscending ? 'ASC' : 'DESC'
76
                )
77
            )
78
            ->setParameters([
79
                'starSystemId' => $starSystemId,
80
                'startSx' => $startSx,
81
                'endSx' => $endSx,
82
                'startSy' => $startSy,
83
                'endSy' => $endSy
84
            ])
85
            ->getResult();
86
    }
87
88 4
    #[Override]
89
    public function getMapLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
90
    {
91 4
        return $this->getEntityManager()->createNativeQuery(
92 4
            'SELECT sm.sx as x, sm.sy AS y, ft.type
93
                FROM stu_sys_map sm
94
                JOIN stu_location l
95
                ON sm.id = l.id
96
                JOIN stu_map_ftypes ft ON ft.id = l.field_id
97
                WHERE sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd
98 4
                AND sm.systems_id = :systemId',
99 4
            $rsm
100 4
        )->setParameters([
101 4
            'xStart' => $boundaries->getMinX(),
102 4
            'xEnd' => $boundaries->getMaxX(),
103 4
            'yStart' => $boundaries->getMinY(),
104 4
            'yEnd' => $boundaries->getMaxY(),
105 4
            'systemId' => $boundaries->getParentId()
106 4
        ])->getResult();
107
    }
108
109 4
    #[Override]
110
    public function getSpacecraftCountLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
111
    {
112 4
        return $this->getEntityManager()->createNativeQuery(
113 4
            'SELECT sm.id, sm.sx as x, sm.sy AS y,
114
                (SELECT count(DISTINCT b.id) FROM stu_spacecraft b
115
                    WHERE sm.id = b.location_id
116
                    AND NOT EXISTS (SELECT ss.id
117
                                        FROM stu_spacecraft_system ss
118
                                        WHERE b.id = ss.spacecraft_id
119
                                        AND ss.system_type = :cloakSystemId
120
                                        AND ss.mode > 1)) AS spacecraftcount,
121
                (SELECT count(DISTINCT c.id) FROM stu_spacecraft c
122
                    WHERE sm.id = c.location_id
123
                    AND EXISTS (SELECT ss2.id
124
                                        FROM stu_spacecraft_system ss2
125
                                        WHERE c.id = ss2.spacecraft_id
126
                                        AND ss2.system_type = :cloakSystemId
127
                                        AND ss2.mode > 1)) AS cloakcount,
128
                (SELECT mft.effects FROM stu_map_ftypes mft
129
                WHERE l.field_id = mft.id) as effects
130
            FROM stu_sys_map sm
131
            JOIN stu_location l
132
            ON sm.id = l.id
133
            WHERE sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd
134
            AND sm.systems_id = :systemId
135 4
            GROUP BY sm.id, sm.sy, sm.sx, l.field_id',
136 4
            $rsm
137 4
        )->setParameters([
138 4
            'xStart' => $boundaries->getMinX(),
139 4
            'xEnd' => $boundaries->getMaxX(),
140 4
            'yStart' => $boundaries->getMinY(),
141 4
            'yEnd' => $boundaries->getMaxY(),
142 4
            'systemId' => $boundaries->getParentId(),
143 4
            'cloakSystemId' => SpacecraftSystemTypeEnum::CLOAK->value
144 4
        ])->getResult();
145
    }
146
147 4
    #[Override]
148
    public function getColonyShieldData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
149
    {
150 4
        return $this->getEntityManager()->createNativeQuery(
151 4
            'SELECT sm.sx as x, sm.sy AS y,
152
            (SELECT COUNT(*) > 0
153
                FROM stu_colony col
154
                JOIN stu_colonies_fielddata cfd
155
                ON col.id = cfd.colonies_id
156
                WHERE sm.id = col.starsystem_map_id
157
                AND cfd.aktiv = :active
158
                AND cfd.buildings_id IN (
159
                    SELECT bf.buildings_id
160
                    FROM stu_buildings_functions bf
161
                    WHERE bf.function = :shieldBuilding)) AS shieldstate
162
            FROM stu_sys_map sm
163
            WHERE sm.systems_id = :systemId
164 4
            AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
165 4
            $rsm
166 4
        )->setParameters([
167 4
            'xStart' => $boundaries->getMinX(),
168 4
            'xEnd' => $boundaries->getMaxX(),
169 4
            'yStart' => $boundaries->getMinY(),
170 4
            'yEnd' => $boundaries->getMaxY(),
171 4
            'systemId' => $boundaries->getParentId(),
172 4
            'active' => 1,
173 4
            'shieldBuilding' => BuildingFunctionEnum::SHIELD_GENERATOR
174 4
        ])->getResult();
175
    }
176
177
    #[Override]
178
    public function getNormalBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
179
    {
180
        return $this->getEntityManager()->createNativeQuery(
181
            'SELECT sm.sx AS x, sm.sy AS y
182
            FROM stu_sys_map sm
183
            WHERE sm.systems_id = :systemId
184
            AND sm.sx BETWEEN :xStart AND :xEnd
185
            AND sm.sy BETWEEN :yStart AND :yEnd',
186
            $rsm
187
        )->setParameters([
188
            'xStart' => $boundaries->getMinX(),
189
            'xEnd' => $boundaries->getMaxX(),
190
            'yStart' => $boundaries->getMinY(),
191
            'yEnd' => $boundaries->getMaxY(),
192
            'systemId' => $boundaries->getParentId()
193
        ])->getResult();
194
    }
195
196
    #[Override]
197
    public function getRegionBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
198
    {
199
        return $this->getEntityManager()->createNativeQuery(
200
            'SELECT sm.sx AS x, sm.sy AS y
201
            FROM stu_sys_map sm
202
            WHERE sm.systems_id = :systemId
203
            AND sm.sx BETWEEN :xStart AND :xEnd
204
            AND sm.sy BETWEEN :yStart AND :yEnd',
205
            $rsm
206
        )->setParameters([
207
            'xStart' => $boundaries->getMinX(),
208
            'xEnd' => $boundaries->getMaxX(),
209
            'yStart' => $boundaries->getMinY(),
210
            'yEnd' => $boundaries->getMaxY(),
211
            'systemId' => $boundaries->getParentId()
212
        ])->getResult();
213
    }
214
215
    // TODO: Show impassable only for cartographed systems. 
216
    // Currently, it does not display any impassable areas.
217
    #[Override]
218
    public function getImpassableBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
219
    {
220
        return $this->getEntityManager()->createNativeQuery(
221
            'SELECT sm.sx AS x, sm.sy AS y, TRUE AS impassable,
222
                    (SELECT mft.complementary_color FROM stu_map_ftypes mft JOIN stu_location l on l.id = sm.id where mft.id = l.field_id) AS complementary_color
223
            FROM stu_sys_map sm
224
            WHERE sm.systems_id = :systemId
225
            AND sm.sx BETWEEN :xStart AND :xEnd
226
            AND sm.sy BETWEEN :yStart AND :yEnd',
227
            $rsm
228
        )->setParameters([
229
            'xStart' => $boundaries->getMinX(),
230
            'xEnd' => $boundaries->getMaxX(),
231
            'yStart' => $boundaries->getMinY(),
232
            'yEnd' => $boundaries->getMaxY(),
233
            'systemId' => $boundaries->getParentId()
234
        ])->getResult();
235
    }
236
237
    #[Override]
238
    public function getCartographingData(PanelBoundaries $boundaries, ResultSetMapping $rsm, array $locations): array
239
    {
240
        return $this->getEntityManager()->createNativeQuery(
241
            'SELECT DISTINCT 
242
                sm.sx AS x, 
243
                sm.sy AS y, 
244
                CASE 
245
                    WHEN sm.id IN (:fieldIds) THEN TRUE ELSE FALSE
246
                END AS cartographing,
247
                    (SELECT mft.complementary_color FROM stu_map_ftypes mft JOIN stu_location l on l.id = sm.id where mft.id = l.field_id) AS complementary_color
248
            FROM stu_sys_map sm
249
            WHERE sm.sx BETWEEN :xStart AND :xEnd
250
            AND sm.sy BETWEEN :yStart AND :yEnd
251
            AND sm.systems_id = :systemId
252
            ORDER BY cartographing DESC',
253
            $rsm
254
        )->setParameters([
255
            'xStart' => $boundaries->getMinX(),
256
            'xEnd' => $boundaries->getMaxX(),
257
            'yStart' => $boundaries->getMinY(),
258
            'yEnd' => $boundaries->getMaxY(),
259
            'systemId' => $boundaries->getParentId(),
260
            'fieldIds' => $locations
261
        ])->getResult();
262
    }
263
264
265
    #[Override]
266
    public function getAnomalyData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
267
    {
268
        return $this->getEntityManager()->createNativeQuery(
269
            'SELECT sm.sx AS x, sm.sy AS y,
270
                (SELECT array_to_string(array(SELECT a.anomaly_type_id FROM stu_anomaly a WHERE a.location_id = sm.id), \',\')) as anomalytypes
271
            FROM stu_sys_map sm
272
            WHERE sm.systems_id = :systemId
273
            AND sm.sx BETWEEN :xStart AND :xEnd
274
            AND sm.sy BETWEEN :yStart AND :yEnd',
275
            $rsm
276
        )->setParameters([
277
            'xStart' => $boundaries->getMinX(),
278
            'xEnd' => $boundaries->getMaxX(),
279
            'yStart' => $boundaries->getMinY(),
280
            'yEnd' => $boundaries->getMaxY(),
281
            'systemId' => $boundaries->getParentId()
282
        ])->getResult();
283
    }
284
285 4
    #[Override]
286
    public function getLssBlockadeLocations(PanelBoundaries $boundaries): array
287
    {
288 4
        $rsm = new ResultSetMapping();
289 4
        $rsm->addScalarResult('sx', 'x', 'integer');
290 4
        $rsm->addScalarResult('sy', 'y', 'integer');
291 4
        $rsm->addScalarResult('effects', 'effects', 'string');
292
293 4
        return $this->getEntityManager()->createNativeQuery(
294 4
            'WITH bbox AS (
295
                SELECT id, sx, sy
296
                FROM stu_sys_map
297
                WHERE systems_id = :systemId
298
                AND sx BETWEEN :xStart AND :xEnd
299
                AND sy BETWEEN :yStart AND :yEnd
300
            )
301
            SELECT sm.sx, sm.sy, mft.effects
302
            FROM bbox sm
303
            JOIN stu_location l ON sm.id = l.id
304 4
            JOIN stu_map_ftypes mft ON mft.id   = l.field_id',
305 4
            $rsm
306 4
        )->setParameters([
307 4
            'xStart' => $boundaries->getMinX(),
308 4
            'xEnd' => $boundaries->getMaxX(),
309 4
            'yStart' => $boundaries->getMinY(),
310 4
            'yEnd' => $boundaries->getMaxY(),
311 4
            'systemId' => $boundaries->getParentId()
312 4
        ])->getResult();
313
    }
314
315
    #[Override]
316
    public function getIgnoringSubspaceLayerData(PanelBoundaries $boundaries, int $ignoreUserId, ResultSetMapping $rsm): array
317
    {
318
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
319
320
        return $this->getEntityManager()->createNativeQuery(
321
            'SELECT sm.sx as x, sm.sy AS y, mft.effects as effects,
322
                (select count(distinct fs1.ship_id) from stu_flight_sig fs1
323
                where fs1.location_id = sm.id
324
                AND fs1.user_id != :ignoreUserId
325
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
326
                AND fs1.time > :timeThreshold) as d1c,
327
                (select count(distinct fs2.ship_id) from stu_flight_sig fs2
328
                where fs2.location_id = sm.id
329
                AND fs2.user_id != :ignoreUserId
330
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
331
                AND fs2.time > :timeThreshold) as d2c,
332
                (select count(distinct fs3.ship_id) from stu_flight_sig fs3
333
                where fs3.location_id = sm.id
334
                AND fs3.user_id != :ignoreUserId
335
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
336
                AND fs3.time > :timeThreshold) as d3c,
337
                (select count(distinct fs4.ship_id) from stu_flight_sig fs4
338
                where fs4.location_id = sm.id
339
                AND fs4.user_id != :ignoreUserId
340
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
341
                AND fs4.time > :timeThreshold) as d4c 
342
                FROM stu_sys_map sm
343
                JOIN stu_location l
344
                ON sm.id = l.id
345
                JOIN stu_map_ftypes mft
346
                ON l.field_id = mft.id
347
                WHERE sm.systems_id = :systemId
348
                AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
349
            $rsm
350
        )->setParameters([
351
            'xStart' => $boundaries->getMinX(),
352
            'xEnd' => $boundaries->getMaxX(),
353
            'yStart' => $boundaries->getMinY(),
354
            'yEnd' => $boundaries->getMaxY(),
355
            'systemId' => $boundaries->getParentId(),
356
            'ignoreUserId' => $ignoreUserId,
357
            'timeThreshold' => $maxAge
358
        ])->getResult();
359
    }
360
    #[Override]
361
    public function getShipSubspaceLayerData(PanelBoundaries $boundaries, int $shipId, ResultSetMapping $rsm, ?int $rumpId = null): array
362
    {
363
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
364
365
        $rumpId_condition = $rumpId !== null ? 'AND fs1.rump_id = :rumpId' : '';
366
        $rumpId_condition2 = $rumpId !== null ? 'AND fs2.rump_id = :rumpId' : '';
367
        $rumpId_condition3 = $rumpId !== null ? 'AND fs3.rump_id = :rumpId' : '';
368
        $rumpId_condition4 = $rumpId !== null ? 'AND fs4.rump_id = :rumpId' : '';
369
370
        $query = $this->getEntityManager()->createNativeQuery(
371
            sprintf(
372
                'SELECT sm.sx as x, sm.sy as y, mft.effects as effects,
373
            (SELECT count(distinct fs1.ship_id) from stu_flight_sig fs1
374
                WHERE fs1.location_id = l.id
375
                AND fs1.ship_id = :shipId %s
376
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
377
                AND fs1.time > :timeThreshold AND fs1.is_cloaked = :false) as d1c,
378
            (SELECT count(distinct fs2.ship_id) from stu_flight_sig fs2
379
                WHERE fs2.location_id = l.id
380
                AND fs2.ship_id = :shipId %s
381
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
382
                AND fs2.time > :timeThreshold AND fs2.is_cloaked = :false) as d2c,
383
            (SELECT count(distinct fs3.ship_id) from stu_flight_sig fs3
384
                WHERE fs3.location_id = l.id
385
                AND fs3.ship_id = :shipId %s
386
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
387
                AND fs3.time > :timeThreshold AND fs3.is_cloaked = :false) as d3c,
388
            (SELECT count(distinct fs4.ship_id) from stu_flight_sig fs4
389
                WHERE fs4.location_id = l.id
390
                AND fs4.ship_id = :shipId %s
391
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
392
                AND fs4.time > :timeThreshold AND fs4.is_cloaked = :false) as d4c 
393
            FROM stu_sys_map sm
394
                JOIN stu_location l
395
                ON sm.id = l.id
396
                JOIN stu_map_ftypes mft
397
                ON l.field_id = mft.id
398
                WHERE sm.systems_id = :systemId
399
                AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
400
                $rumpId_condition,
401
                $rumpId_condition2,
402
                $rumpId_condition3,
403
                $rumpId_condition4
404
            ),
405
            $rsm
406
        );
407
        $parameters = [
408
            'xStart' => $boundaries->getMinX(),
409
            'xEnd' => $boundaries->getMaxX(),
410
            'yStart' => $boundaries->getMinY(),
411
            'yEnd' => $boundaries->getMaxY(),
412
            'systemId' => $boundaries->getParentId(),
413
            'shipId' => $shipId,
414
            'timeThreshold' => $maxAge,
415
            'false' => false
416
        ];
417
418
        if ($rumpId !== null) {
419
            $parameters['rumpId'] = $rumpId;
420
        }
421
422
        return $query->setParameters($parameters)->getResult();
423
    }
424
425
    #[Override]
426
    public function getRandomSystemMapIdsForAstroMeasurement(int $starSystemId, int $location): array
427
    {
428
        $result = [];
429
430
        $rsm = new ResultSetMapping();
431
        $rsm->addScalarResult('id', 'id', 'integer');
432
433
        $userColonyFields = $this->getEntityManager()
434
            ->createNativeQuery(
435
                'SELECT sm.id as id
436
                FROM stu_sys_map sm
437
                WHERE sm.systems_id = :systemId
438
                AND sm.id != :location
439
                AND EXISTS (SELECT c.id
440
                            FROM stu_colony c
441
                            WHERE c.starsystem_map_id = sm.id
442
                            AND c.user_id != :noOne)
443
                ORDER BY RANDOM()
444
                LIMIT 2',
445
                $rsm
446
            )
447
            ->setParameters([
448
                'systemId' => $starSystemId,
449
                'location'  => $location,
450
                'noOne' => UserConstants::USER_NOONE
451
            ])
452
            ->getResult();
453
454
        $result = array_merge($result, $userColonyFields);
455
456
        $otherColonyFields = $this->getEntityManager()
457
            ->createNativeQuery(
458
                'SELECT sm.id as id
459
                FROM stu_sys_map sm
460
                JOIN stu_location l
461
                ON sm.id = l.id
462
                JOIN stu_map_ftypes ft
463
                ON l.field_id = ft.id
464
                JOIN stu_colonies_classes cc
465
                ON ft.colonies_classes_id = cc.id
466
                WHERE sm.systems_id = :systemId
467
                AND ft.colonies_classes_id IS NOT NULL
468
                AND cc.type < 3
469
                AND sm.id NOT IN (:ids)
470
                ORDER BY RANDOM()
471
                LIMIT :theLimit',
472
                $rsm
473
            )
474
            ->setParameters([
475
                'systemId' => $starSystemId,
476
                'ids' => $result !== [] ? $result : [0],
477
                'theLimit' => AstronomicalEntry::MEASUREMENT_COUNT - count($result)
478
            ])
479
            ->getResult();
480
481
        $result = array_merge($result, $otherColonyFields);
482
483
        if (count($result) < AstronomicalEntry::MEASUREMENT_COUNT) {
484
            $otherFields = $this->getEntityManager()
485
                ->createNativeQuery(
486
                    'SELECT sm.id as id
487
                    FROM stu_sys_map sm
488
                    JOIN stu_location l
489
                    ON sm.id = l.id
490
                    JOIN stu_map_ftypes ft
491
                    ON l.field_id = ft.id
492
                    WHERE sm.systems_id = :systemId
493
                    AND ft.x_damage_system <= 10
494
                    AND ft.x_damage <= 10
495
                    ORDER BY RANDOM()
496
                    LIMIT :theLimit',
497
                    $rsm
498
                )
499
                ->setParameters([
500
                    'systemId' => $starSystemId,
501
                    'theLimit' => AstronomicalEntry::MEASUREMENT_COUNT - count($result)
502
                ])
503
                ->getResult();
504
505
            $result = array_merge($result, $otherFields);
506
        }
507
508
        return array_map(fn(array $data) => $data['id'], $result);
509
    }
510
511
    #[Override]
512
    public function prototype(): StarSystemMap
513
    {
514
        return new StarSystemMap();
515
    }
516
517
    #[Override]
518
    public function save(StarSystemMap $starSystemMap): void
519
    {
520
        $em = $this->getEntityManager();
521
522
        $em->persist($starSystemMap);
523
    }
524
525
    #[Override]
526
    public function truncateByStarSystem(StarSystem $starSystem): void
527
    {
528
        $this->getEntityManager()->createQuery(
529
            sprintf(
530
                'DELETE FROM %s sm WHERE sm.systems_id = :systemId',
531
                StarSystemMap::class
532
            )
533
        )
534
            ->setParameters(['systemId' => $starSystem->getId()])
535
            ->execute();
536
    }
537
}
538