Passed
Push — master ( 41c1e2...7e39ad )
by Nico
17:44 queued 08:39
created

getSignaturesInSensorRange()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 35
nc 4
nop 6
dl 0
loc 54
ccs 0
cts 30
cp 0
crap 20
rs 9.36
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\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...
11
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...
12
use Stu\Orm\Entity\Colony;
13
use Stu\Orm\Entity\FlightSignature;
14
use Stu\Orm\Entity\Location;
15
use Stu\Orm\Entity\Map;
16
use Stu\Orm\Entity\StarSystemMap;
17
use Stu\Orm\Entity\Spacecraft;
18
use Stu\Orm\Entity\User;
19
20
/**
21
 * @extends EntityRepository<FlightSignature>
22
 */
23
final class FlightSignatureRepository extends EntityRepository implements FlightSignatureRepositoryInterface
24
{
25 1
    #[Override]
26
    public function prototype(): FlightSignature
27
    {
28 1
        return new FlightSignature();
29
    }
30
31
    #[Override]
32
    public function saveAll(array $array): void
33
    {
34
        $em = $this->getEntityManager();
35
36
        foreach ($array as $obj) {
37
            $em->persist($obj);
38
        }
39
    }
40
41 1
    #[Override]
42
    public function save(FlightSignature $item): void
43
    {
44 1
        $em = $this->getEntityManager();
45 1
        $em->persist($item);
46
    }
47
48 1
    #[Override]
49
    public function getVisibleSignatureCount(Colony $colony): int
50
    {
51 1
        return (int) $this->getEntityManager()
52 1
            ->createQuery(
53 1
                sprintf(
54 1
                    'SELECT count(DISTINCT CONCAT(fs.ship_id, fs.ship_name)) as count
55
                    FROM %s fs
56
                    JOIN %s ssm
57
                    WITH fs.location_id = ssm.id
58
                    WHERE (fs.is_cloaked = :false AND fs.time > :maxAgeUncloaked
59
                      OR fs.is_cloaked = :true AND fs.time > :maxAgeCloaked)
60
                    AND ssm.sx = :sx
61
                    AND ssm.sy = :sy
62
                    AND ssm.systems_id = :systemsId
63 1
                    AND fs.user_id != :ignoreId',
64 1
                    FlightSignature::class,
65 1
                    StarSystemMap::class
66 1
                )
67 1
            )
68 1
            ->setParameters([
69 1
                'maxAgeUncloaked' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED,
70 1
                'maxAgeCloaked' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_CLOAKED,
71 1
                'sx' => $colony->getSx(),
72 1
                'sy' => $colony->getSy(),
73 1
                'systemsId' => $colony->getSystem()->getId(),
74 1
                'ignoreId' => $colony->getUserId(),
75 1
                'false' => false,
76 1
                'true' => true
77 1
            ])
78 1
            ->getSingleScalarResult();
79
    }
80
81
82
83
84 2
    #[Override]
85
    public function getVisibleSignatures(int $locationId, int $ignoreId): array
86
    {
87 2
        return $this->getEntityManager()
88 2
            ->createQuery(
89 2
                sprintf(
90 2
                    'SELECT fs FROM %s fs
91
                    WHERE fs.time > :maxAge
92
                    AND fs.location_id = :locationId
93
                    AND fs.user_id != :ignoreId
94 2
                    ORDER BY fs.time desc',
95 2
                    FlightSignature::class
96 2
                )
97 2
            )
98 2
            ->setParameters([
99 2
                'maxAge' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED,
100 2
                'locationId' => $locationId,
101 2
                'ignoreId' => $ignoreId
102 2
            ])
103 2
            ->getResult();
104
    }
105
106
    public function createSignatureRangeResultMapping(): ResultSetMapping
107
    {
108
        $rsm = new ResultSetMapping();
109
        $rsm->addScalarResult('minx', 'minx', 'integer');
110
        $rsm->addScalarResult('maxx', 'maxx', 'integer');
111
        $rsm->addScalarResult('miny', 'miny', 'integer');
112
        $rsm->addScalarResult('maxy', 'maxy', 'integer');
113
114
        return $rsm;
115
    }
116
117
    #[Override]
118
    public function getSignatureRange(): array
119
    {
120
        return $this->getEntityManager()
121
            ->createNativeQuery(
122
                'SELECT COALESCE(min(l.cx),0) as minx, COALESCE(max(l.cx),0) as maxx,
123
                    COALESCE(min(l.cy),0) as miny, COALESCE(max(l.cy),0) as maxy
124
                FROM stu_flight_sig fs
125
                JOIN stu_location l ON l.id = fs.location_id',
126
                $this->createSignatureRangeResultMapping()
127
            )
128
            ->getResult();
129
    }
130
131
    #[Override]
132
    public function getSignatureRangeForShip(int $shipId): array
133
    {
134
        return $this->getEntityManager()
135
            ->createNativeQuery(
136
                'SELECT COALESCE(min(l.cx),0) as minx, COALESCE(max(l.cx),0) as maxx, COALESCE(min(l.cy),0) as miny, COALESCE(max(l.cy),0) as maxy
137
                FROM stu_flight_sig fs
138
                JOIN stu_location l ON l.id = fs.location_id
139
                WHERE fs.ship_id = :shipId',
140
                $this->createSignatureRangeResultMapping()
141
            )
142
            ->setParameter('shipId', $shipId)
143
            ->getResult();
144
    }
145
146
    #[Override]
147
    public function getSignatureRangeForUser(int $userId): array
148
    {
149
        return $this->getEntityManager()
150
            ->createNativeQuery(
151
                'SELECT COALESCE(min(l.cx),0) as minx, COALESCE(max(l.cx),0) as maxx, COALESCE(min(l.cy),0) as miny, COALESCE(max(l.cy),0) as maxy
152
                FROM stu_flight_sig fs
153
                JOIN stu_location l ON l.id = fs.location_id
154
                WHERE fs.user_id = :userId',
155
                $this->createSignatureRangeResultMapping()
156
            )
157
            ->setParameter('userId', $userId)
158
            ->getResult();
159
    }
160
161
    #[Override]
162
    public function getSignatureRangeForAlly(int $allyId): array
163
    {
164
        return $this->getEntityManager()
165
            ->createNativeQuery(
166
                'SELECT COALESCE(min(l.cx),0) as minx, COALESCE(max(l.cx),0) as maxx, COALESCE(min(l.cy),0) as miny, COALESCE(max(l.cy),0) as maxy
167
                FROM stu_flight_sig fs
168
                JOIN stu_location l ON l.id = fs.location_id
169
                JOIN stu_user u	ON fs.user_id = u.id
170
                WHERE u.allys_id = :allyId',
171
                $this->createSignatureRangeResultMapping()
172
            )
173
            ->setParameter('allyId', $allyId)
174
            ->getResult();
175
    }
176
177
    #[Override]
178
    public function deleteOldSignatures(int $threshold): void
179
    {
180
        $q = $this->getEntityManager()
181
            ->createQuery(
182
                sprintf(
183
                    'DELETE FROM %s fs WHERE fs.time < :maxAge',
184
                    FlightSignature::class
185
                )
186
            );
187
        $q->setParameter('maxAge', time() - $threshold);
188
        $q->execute();
189
    }
190
191 1
    #[Override]
192
    public function getFlightsTop10(): array
193
    {
194 1
        $rsm = new ResultSetMapping();
195 1
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
196 1
        $rsm->addScalarResult('sc', 'sc', 'integer');
197 1
        $rsm->addScalarResult('faction_id', 'factionid', 'integer');
198 1
        $rsm->addScalarResult('shipc', 'shipc', 'integer');
199
200 1
        return $this
201 1
            ->getEntityManager()
202 1
            ->createNativeQuery(
203 1
                'SELECT fs.user_id, count(*) as sc,
204
                (SELECT faction_id
205
                FROM stu_user u
206
                WHERE fs.user_id = u.id),
207
                count(distinct ship_id) as shipc
208
                FROM stu_flight_sig fs
209
                WHERE fs.to_direction != 0
210
                AND fs.user_id > :firstUserId
211
                AND fs.time > :maxAge
212
                GROUP BY fs.user_id
213
                ORDER BY 2 DESC
214 1
                LIMIT 10',
215 1
                $rsm
216 1
            )
217 1
            ->setParameters([
218 1
                'maxAge' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED,
219 1
                'firstUserId' => UserConstants::USER_FIRST_ID
220 1
            ])
221 1
            ->getResult();
222
    }
223
224 1
    #[Override]
225
    public function getSignaturesForUser(User $user): int
226
    {
227 1
        return (int)$this
228 1
            ->getEntityManager()
229 1
            ->createQuery(
230 1
                sprintf(
231 1
                    'SELECT count(fs.id)
232
                    FROM %s fs
233
                    WHERE fs.to_direction != 0
234
                    AND fs.user_id  = :userId
235 1
                    AND fs.time > :maxAge',
236 1
                    FlightSignature::class
237 1
                )
238 1
            )
239 1
            ->setParameters([
240 1
                'maxAge' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED,
241 1
                'userId' => $user->getId()
242 1
            ])
243 1
            ->getSingleScalarResult();
244
    }
245
    /**
246
     * @return array<array{FlightSignature, Spacecraft}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array{FlightSignature, Spacecraft}> at position 4 could not be parsed: Expected ':' at position 4, but found 'FlightSignature'.
Loading history...
247
     */
248
    #[Override]
249
    public function getSignaturesInSensorRange(
250
        int $user_id,
251
        int $cx,
252
        int $cy,
253
        int $layer_id,
254
        int $sensorRange,
255
        int $timeThreshold
256
    ): array {
257
258
        $flightSignatures = $this->getEntityManager()
259
            ->createQuery(
260
                sprintf(
261
                    'SELECT fs
262
                FROM %s fs
263
                JOIN %s m WITH fs.location_id = m.id
264
                WHERE m.cx BETWEEN :minX AND :maxX
265
                  AND m.cy BETWEEN :minY AND :maxY
266
                  AND m.layer_id = :layerId
267
                  AND fs.time >= :minTime
268
                  AND fs.user_id != :userId
269
                  AND fs.is_cloaked = false
270
                ORDER BY fs.ship_id ASC, fs.time DESC',
271
                    FlightSignature::class,
272
                    Map::class
273
                )
274
            )
275
            ->setParameters([
276
                'minX' => $cx - $sensorRange,
277
                'maxX' => $cx + $sensorRange,
278
                'minY' => $cy - $sensorRange,
279
                'maxY' => $cy + $sensorRange,
280
                'layerId' => $layer_id,
281
                'minTime' => $timeThreshold,
282
                'userId' => $user_id
283
            ])
284
            ->getResult();
285
286
        $latestPerShip = [];
287
        foreach ($flightSignatures as $flightSignature) {
288
            $shipId = $flightSignature->getShipId();
289
290
            if (!isset($latestPerShip[$shipId])) {
291
                $spacecraft = $this->getEntityManager()
292
                    ->getRepository(Spacecraft::class)
293
                    ->find($shipId);
294
295
                if ($spacecraft) {
296
                    $latestPerShip[$shipId] = [$flightSignature, $spacecraft];
297
                }
298
            }
299
        }
300
301
        return array_values($latestPerShip);
302
    }
303
}
304