Test Failed
Pull Request — dev (#1952)
by Janko
02:59
created

FlightSignatureRepository::getSignaturesInSensorRange()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 60
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 39
c 1
b 1
f 0
nc 4
nop 6
dl 0
loc 60
ccs 0
cts 35
cp 0
crap 20
rs 9.296

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\StarSystemMap;
15
use Stu\Orm\Entity\User;
16
17
/**
18
 * @extends EntityRepository<FlightSignature>
19
 */
20
final class FlightSignatureRepository extends EntityRepository implements FlightSignatureRepositoryInterface
21
{
22
    #[Override]
23
    public function prototype(): FlightSignature
24
    {
25 4
        return new FlightSignature();
26
    }
27
28 4
    #[Override]
29
    public function saveAll(array $array): void
30
    {
31
        $em = $this->getEntityManager();
32
33
        foreach ($array as $obj) {
34
            $em->persist($obj);
35
        }
36
    }
37
38
    #[Override]
39
    public function save(FlightSignature $item): void
40
    {
41 4
        $em = $this->getEntityManager();
42
        $em->persist($item);
43
    }
44 4
45 4
    #[Override]
46
    public function getVisibleSignatureCount(Colony $colony): int
47
    {
48 1
        return (int) $this->getEntityManager()
49
            ->createQuery(
50
                sprintf(
51 1
                    'SELECT count(DISTINCT CONCAT(fs.ship_id, fs.ship_name)) as count
52 1
                    FROM %s fs
53 1
                    JOIN %s ssm
54 1
                    WITH fs.location_id = ssm.id
55
                    WHERE (fs.is_cloaked = :false AND fs.time > :maxAgeUncloaked
56
                      OR fs.is_cloaked = :true AND fs.time > :maxAgeCloaked)
57
                    AND ssm.sx = :sx
58
                    AND ssm.sy = :sy
59
                    AND ssm.systems_id = :systemsId
60
                    AND fs.user_id != :ignoreId',
61
                    FlightSignature::class,
62
                    StarSystemMap::class
63 1
                )
64 1
            )
65 1
            ->setParameters([
66 1
                'maxAgeUncloaked' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED,
67 1
                'maxAgeCloaked' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_CLOAKED,
68 1
                'sx' => $colony->getSx(),
69 1
                'sy' => $colony->getSy(),
70 1
                'systemsId' => $colony->getSystem()->getId(),
71 1
                'ignoreId' => $colony->getUserId(),
72 1
                'false' => false,
73 1
                'true' => true
74 1
            ])
75 1
            ->getSingleScalarResult();
76 1
    }
77 1
78 1
79
80
81
    #[Override]
82
    public function getVisibleSignatures(int $locationId, int $ignoreId): array
83
    {
84 2
        return $this->getEntityManager()
85
            ->createQuery(
86
                sprintf(
87 2
                    'SELECT fs FROM %s fs
88 2
                    WHERE fs.time > :maxAge
89 2
                    AND fs.location_id = :locationId
90 2
                    AND fs.user_id != :ignoreId
91
                    ORDER BY fs.time desc',
92
                    FlightSignature::class
93
                )
94 2
            )
95 2
            ->setParameters([
96 2
                'maxAge' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED,
97 2
                'locationId' => $locationId,
98 2
                'ignoreId' => $ignoreId
99 2
            ])
100 2
            ->getResult();
101 2
    }
102 2
103 2
    public function createSignatureRangeResultMapping(): ResultSetMapping
104
    {
105
        $rsm = new ResultSetMapping();
106
        $rsm->addScalarResult('minx', 'minx', 'integer');
107
        $rsm->addScalarResult('maxx', 'maxx', 'integer');
108
        $rsm->addScalarResult('miny', 'miny', 'integer');
109
        $rsm->addScalarResult('maxy', 'maxy', 'integer');
110
111
        return $rsm;
112
    }
113
114
    #[Override]
115
    public function getSignatureRange(): array
116
    {
117
        return $this->getEntityManager()
118
            ->createNativeQuery(
119
                'SELECT COALESCE(min(l.cx),0) as minx, COALESCE(max(l.cx),0) as maxx,
120
                    COALESCE(min(l.cy),0) as miny, COALESCE(max(l.cy),0) as maxy
121
                FROM stu_flight_sig fs
122
                JOIN stu_location l ON l.id = fs.location_id',
123
                $this->createSignatureRangeResultMapping()
124
            )
125
            ->getResult();
126
    }
127
128
    #[Override]
129
    public function getSignatureRangeForShip(int $shipId): array
130
    {
131
        return $this->getEntityManager()
132
            ->createNativeQuery(
133
                '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
134
                FROM stu_flight_sig fs
135
                JOIN stu_location l ON l.id = fs.location_id
136
                WHERE fs.ship_id = :shipId',
137
                $this->createSignatureRangeResultMapping()
138
            )
139
            ->setParameter('shipId', $shipId)
140
            ->getResult();
141
    }
142
143
    #[Override]
144
    public function getSignatureRangeForUser(int $userId): array
145
    {
146
        return $this->getEntityManager()
147
            ->createNativeQuery(
148
                '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
149
                FROM stu_flight_sig fs
150
                JOIN stu_location l ON l.id = fs.location_id
151
                WHERE fs.user_id = :userId',
152
                $this->createSignatureRangeResultMapping()
153
            )
154
            ->setParameter('userId', $userId)
155
            ->getResult();
156
    }
157
158
    #[Override]
159
    public function getSignatureRangeForAlly(int $allyId): array
160
    {
161
        return $this->getEntityManager()
162
            ->createNativeQuery(
163
                '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
164
                FROM stu_flight_sig fs
165
                JOIN stu_location l ON l.id = fs.location_id
166
                JOIN stu_user u	ON fs.user_id = u.id
167
                WHERE u.allys_id = :allyId',
168
                $this->createSignatureRangeResultMapping()
169
            )
170
            ->setParameter('allyId', $allyId)
171
            ->getResult();
172
    }
173
174
    #[Override]
175
    public function deleteOldSignatures(int $threshold): void
176
    {
177 1
        $q = $this->getEntityManager()
178
            ->createQuery(
179
                sprintf(
180 1
                    'DELETE FROM %s fs WHERE fs.time < :maxAge',
181 1
                    FlightSignature::class
182 1
                )
183 1
            );
184 1
        $q->setParameter('maxAge', time() - $threshold);
185 1
        $q->execute();
186 1
    }
187 1
188 1
    #[Override]
189
    public function getFlightsTop10(): array
190
    {
191 2
        $rsm = new ResultSetMapping();
192
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
193
        $rsm->addScalarResult('sc', 'sc', 'integer');
194 2
        $rsm->addScalarResult('faction_id', 'factionid', 'integer');
195 2
        $rsm->addScalarResult('shipc', 'shipc', 'integer');
196 2
197 2
        return $this
198 2
            ->getEntityManager()
199
            ->createNativeQuery(
200 2
                'SELECT fs.user_id, count(*) as sc,
201 2
                (SELECT faction_id
202 2
                FROM stu_user u
203 2
                WHERE fs.user_id = u.id),
204
                count(distinct ship_id) as shipc
205
                FROM stu_flight_sig fs
206
                WHERE fs.to_direction != 0
207
                AND fs.user_id > :firstUserId
208
                AND fs.time > :maxAge
209
                GROUP BY fs.user_id
210
                ORDER BY 2 DESC
211
                LIMIT 10',
212
                $rsm
213
            )
214 2
            ->setParameters([
215 2
                'maxAge' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED,
216 2
                'firstUserId' => UserConstants::USER_FIRST_ID
217 2
            ])
218 2
            ->getResult();
219 2
    }
220 2
221 2
    #[Override]
222
    public function getSignaturesForUser(User $user): int
223
    {
224 1
        return (int)$this
225
            ->getEntityManager()
226
            ->createQuery(
227 1
                sprintf(
228 1
                    'SELECT count(fs.id)
229 1
                    FROM %s fs
230 1
                    WHERE fs.to_direction != 0
231 1
                    AND fs.user_id  = :userId
232
                    AND fs.time > :maxAge',
233
                    FlightSignature::class
234
                )
235 1
            )
236 1
            ->setParameters([
237 1
                'maxAge' => time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_UNCLOAKED,
238 1
                'userId' => $user->getId()
239 1
            ])
240 1
            ->getSingleScalarResult();
241 1
    }
242
}
243