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

getRandomSystemMapIdsForAstroMeasurement()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 82
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 65
nc 2
nop 1
dl 0
loc 82
ccs 0
cts 43
cp 0
crap 12
rs 8.7636
c 0
b 0
f 0

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\BuildingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Building\BuildingEnum 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...
11
use Stu\Component\Ship\AstronomicalMappingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\AstronomicalMappingEnum 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\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...
13
use Stu\Component\Ship\System\ShipSystemTypeEnum;
14
use Stu\Lib\Map\VisualPanel\PanelBoundaries;
15
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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\StarSystemInterface;
17
use Stu\Orm\Entity\StarSystemMap;
18
use Stu\Orm\Entity\StarSystemMapInterface;
19
20
/**
21
 * @extends EntityRepository<StarSystemMap>
22
 */
23
final class StarSystemMapRepository extends EntityRepository implements StarSystemMapRepositoryInterface
24
{
25
    #[Override]
26
    public function getBySystemOrdered(int $starSystemId): array
27
    {
28
        return $this->findBy(
29
            ['systems_id' => $starSystemId],
30
            ['sy' => 'asc', 'sx' => 'asc']
31
        );
32
    }
33
34
    #[Override]
35
    public function getByCoordinates(
36
        int $starSystemId,
37
        int $sx,
38
        int $sy
39
    ): ?StarSystemMapInterface {
40
        return $this->findOneBy([
41
            'systems_id' => $starSystemId,
42
            'sx' => $sx,
43
            'sy' => $sy
44
        ]);
45
    }
46
47
    #[Override]
48
    public function getByCoordinateRange(
49
        StarSystemInterface $starSystem,
50
        int $startSx,
51
        int $endSx,
52
        int $startSy,
53
        int $endSy,
54
        bool $sortAscending = true
55
    ): array {
56
        return $this->getEntityManager()
57
            ->createQuery(
58
                sprintf(
59
                    'SELECT m FROM %1$s m
60
                    WHERE m.systems_id = :starSystemId AND
61
                        m.sx BETWEEN :startSx AND :endSx AND
62
                        m.sy BETWEEN :startSy AND :endSy
63
                    ORDER BY m.sy %2$s, m.sx %2$s',
64
                    StarSystemMap::class,
65
                    $sortAscending ? 'ASC' : 'DESC'
66
                )
67
            )
68
            ->setParameters([
69
                'starSystemId' => $starSystem,
70
                'startSx' => $startSx,
71
                'endSx' => $endSx,
72
                'startSy' => $startSy,
73
                'endSy' => $endSy
74
            ])
75
            ->getResult();
76
    }
77
78
    #[Override]
79
    public function getMapLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
80
    {
81
        return $this->getEntityManager()->createNativeQuery(
82
            'SELECT sm.sx as x, sm.sy AS y, ft.type
83
                FROM stu_sys_map sm
84
                JOIN stu_location l
85
                ON sm.id = l.id
86
                JOIN stu_map_ftypes ft ON ft.id = l.field_id
87
                WHERE sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd
88
                AND sm.systems_id = :systemId',
89
            $rsm
90
        )->setParameters([
91
            'xStart' => $boundaries->getMinX(),
92
            'xEnd' => $boundaries->getMaxX(),
93
            'yStart' => $boundaries->getMinY(),
94
            'yEnd' => $boundaries->getMaxY(),
95
            'systemId' => $boundaries->getParentId()
96
        ])->getResult();
97
    }
98
99
    #[Override]
100
    public function getShipCountLayerData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
101
    {
102
        return $this->getEntityManager()->createNativeQuery(
103
            'SELECT sm.id, sm.sx as x, sm.sy AS y,
104
                (SELECT count(DISTINCT b.id) FROM stu_ships b
105
                    WHERE sm.id = b.location_id
106
                    AND NOT EXISTS (SELECT ss.id
107
                                        FROM stu_ship_system ss
108
                                        WHERE b.id = ss.ship_id
109
                                        AND ss.system_type = :cloakSystemId
110
                                        AND ss.mode > 1)) AS shipcount,
111
                (SELECT count(DISTINCT c.id) FROM stu_ships c
112
                    WHERE sm.id = c.location_id
113
                    AND EXISTS (SELECT ss2.id
114
                                        FROM stu_ship_system ss2
115
                                        WHERE c.id = ss2.ship_id
116
                                        AND ss2.system_type = :cloakSystemId
117
                                        AND ss2.mode > 1)) AS cloakcount
118
            FROM stu_sys_map sm
119
            WHERE sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd
120
            AND sm.systems_id = :systemId
121
            GROUP BY sm.id, sm.sy, sm.sx',
122
            $rsm
123
        )->setParameters([
124
            'xStart' => $boundaries->getMinX(),
125
            'xEnd' => $boundaries->getMaxX(),
126
            'yStart' => $boundaries->getMinY(),
127
            'yEnd' => $boundaries->getMaxY(),
128
            'systemId' => $boundaries->getParentId(),
129
            'cloakSystemId' => ShipSystemTypeEnum::SYSTEM_CLOAK->value
130
        ])->getResult();
131
    }
132
133
    #[Override]
134
    public function getColonyShieldData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
135
    {
136
        return $this->getEntityManager()->createNativeQuery(
137
            'SELECT sm.sx as x, sm.sy AS y,
138
            (SELECT COUNT(cfd) > 0
139
                FROM stu_colonies col
140
                JOIN stu_colonies_fielddata cfd
141
                ON col.id = cfd.colonies_id
142
                WHERE sm.id = col.starsystem_map_id
143
                AND cfd.aktiv = :active
144
                AND cfd.buildings_id IN (
145
                    SELECT bf.buildings_id
146
                    FROM stu_buildings_functions bf
147
                    WHERE bf.function = :shieldBuilding)) AS shieldstate
148
            FROM stu_sys_map sm
149
            WHERE sm.systems_id = :systemId
150
            AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
151
            $rsm
152
        )->setParameters([
153
            'xStart' => $boundaries->getMinX(),
154
            'xEnd' => $boundaries->getMaxX(),
155
            'yStart' => $boundaries->getMinY(),
156
            'yEnd' => $boundaries->getMaxY(),
157
            'systemId' => $boundaries->getParentId(),
158
            'active' => 1,
159
            'shieldBuilding' => BuildingEnum::BUILDING_FUNCTION_SHIELD_GENERATOR
160
        ])->getResult();
161
    }
162
163
164
    #[Override]
165
    public function getBorderData(PanelBoundaries $boundaries, ResultSetMapping $rsm): array
166
    {
167
        return $this->getEntityManager()->createNativeQuery(
168
            'SELECT sm.sx AS x, sm.sy AS y
169
            FROM stu_sys_map sm
170
            WHERE sm.systems_id = :systemId
171
            AND sm.sx BETWEEN :xStart AND :xEnd
172
            AND sm.sy BETWEEN :yStart AND :yEnd',
173
            $rsm
174
        )->setParameters([
175
            'xStart' => $boundaries->getMinX(),
176
            'xEnd' => $boundaries->getMaxX(),
177
            'yStart' => $boundaries->getMinY(),
178
            'yEnd' => $boundaries->getMaxY(),
179
            'systemId' => $boundaries->getParentId()
180
        ])->getResult();
181
    }
182
183
    #[Override]
184
    public function getIgnoringSubspaceLayerData(PanelBoundaries $boundaries, int $ignoreId, ResultSetMapping $rsm): array
185
    {
186
        $maxAge = time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED;
187
188
        return $this->getEntityManager()->createNativeQuery(
189
            sprintf(
190
                'SELECT sm.sx as x, sm.sy AS y,
191
                (select count(distinct fs1.ship_id) from stu_flight_sig fs1
192
                where fs1.starsystem_map_id = sm.id
193
                AND fs1.user_id != %1$d
194
                AND (fs1.from_direction = 1 OR fs1.to_direction = 1)
195
                AND fs1.time > %2$d) as d1c,
196
                (select count(distinct fs2.ship_id) from stu_flight_sig fs2
197
                where fs2.starsystem_map_id = sm.id
198
                AND fs2.user_id != %1$d
199
                AND (fs2.from_direction = 2 OR fs2.to_direction = 2)
200
                AND fs2.time > %2$d) as d2c,
201
                (select count(distinct fs3.ship_id) from stu_flight_sig fs3
202
                where fs3.starsystem_map_id = sm.id
203
                AND fs3.user_id != %1$d
204
                AND (fs3.from_direction = 3 OR fs3.to_direction = 3)
205
                AND fs3.time > %2$d) as d3c,
206
                (select count(distinct fs4.ship_id) from stu_flight_sig fs4
207
                where fs4.starsystem_map_id = sm.id
208
                AND fs4.user_id != %1$d
209
                AND (fs4.from_direction = 4 OR fs4.to_direction = 4)
210
                AND fs4.time > %2$d) as d4c 
211
                FROM stu_sys_map sm
212
                WHERE sm.systems_id = :systemId
213
                AND sm.sx BETWEEN :xStart AND :xEnd AND sm.sy BETWEEN :yStart AND :yEnd',
214
                $ignoreId,
215
                $maxAge
216
            ),
217
            $rsm
218
        )->setParameters([
219
            'xStart' => $boundaries->getMinX(),
220
            'xEnd' => $boundaries->getMaxX(),
221
            'yStart' => $boundaries->getMinY(),
222
            'yEnd' => $boundaries->getMaxY(),
223
            'systemId' => $boundaries->getParentId()
224
        ])->getResult();
225
    }
226
227
    #[Override]
228
    public function getRandomSystemMapIdsForAstroMeasurement(int $starSystemId): array
229
    {
230
        $result = [];
231
232
        $rsm = new ResultSetMapping();
233
        $rsm->addScalarResult('id', 'id', 'integer');
234
235
        $userColonyFields = $this->getEntityManager()
236
            ->createNativeQuery(
237
                'SELECT sm.id as id
238
                FROM stu_sys_map sm
239
                WHERE sm.systems_id = :systemId
240
                AND EXISTS (SELECT c.id
241
                            FROM stu_colonies c
242
                            WHERE c.starsystem_map_id = sm.id
243
                            AND c.user_id != :noOne)
244
                ORDER BY RANDOM()
245
                LIMIT 2',
246
                $rsm
247
            )
248
            ->setParameters([
249
                'systemId' => $starSystemId,
250
                'noOne' => UserEnum::USER_NOONE
251
            ])
252
            ->getResult();
253
254
        $result = array_merge($result, $userColonyFields);
255
256
        $otherColonyFields = $this->getEntityManager()
257
            ->createNativeQuery(
258
                'SELECT sm.id as id
259
                FROM stu_sys_map sm
260
                JOIN stu_location l
261
                ON sm.id = l.id
262
                JOIN stu_map_ftypes ft
263
                ON l.field_id = ft.id
264
                JOIN stu_colonies_classes cc
265
                ON ft.colonies_classes_id = cc.id
266
                WHERE sm.systems_id = :systemId
267
                AND ft.colonies_classes_id IS NOT NULL
268
                AND cc.type < 3
269
                AND sm.id NOT IN (:ids)
270
                ORDER BY RANDOM()
271
                LIMIT :theLimit',
272
                $rsm
273
            )
274
            ->setParameters([
275
                'systemId' => $starSystemId,
276
                'ids' => $result !== [] ? $result : [0],
277
                'theLimit' => AstronomicalMappingEnum::MEASUREMENT_COUNT - count($result)
278
            ])
279
            ->getResult();
280
281
        $result = array_merge($result, $otherColonyFields);
282
283
        if (count($result) < AstronomicalMappingEnum::MEASUREMENT_COUNT) {
284
            $otherFields = $this->getEntityManager()
285
                ->createNativeQuery(
286
                    'SELECT sm.id as id
287
                    FROM stu_sys_map sm
288
                    JOIN stu_location l
289
                    ON sm.id = l.id
290
                    JOIN stu_map_ftypes ft
291
                    ON l.field_id = ft.id
292
                    WHERE sm.systems_id = :systemId
293
                    AND ft.x_damage_system <= 10
294
                    AND ft.x_damage <= 10
295
                    ORDER BY RANDOM()
296
                    LIMIT :theLimit',
297
                    $rsm
298
                )
299
                ->setParameters([
300
                    'systemId' => $starSystemId,
301
                    'theLimit' => AstronomicalMappingEnum::MEASUREMENT_COUNT - count($result)
302
                ])
303
                ->getResult();
304
305
            $result = array_merge($result, $otherFields);
306
        }
307
308
        return array_map(fn (array $data) => $data['id'], $result);
309
    }
310
311
    #[Override]
312
    public function prototype(): StarSystemMapInterface
313
    {
314
        return new StarSystemMap();
315
    }
316
317
    #[Override]
318
    public function save(StarSystemMapInterface $starSystemMap): void
319
    {
320
        $em = $this->getEntityManager();
321
322
        $em->persist($starSystemMap);
323
    }
324
325
    #[Override]
326
    public function truncateByStarSystem(StarSystemInterface $starSystem): void
327
    {
328
        $this->getEntityManager()->createQuery(
329
            sprintf(
330
                'DELETE FROM %s sm WHERE sm.systems_id = :systemId',
331
                StarSystemMap::class
332
            )
333
        )
334
            ->setParameters(['systemId' => $starSystem->getId()])
335
            ->execute();
336
    }
337
}
338