Passed
Push — dev ( 03e2b8...98e531 )
by Janko
15:04
created

CrewAssignmentRepository::getAmountBySpacecraft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
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\Spacecraft\SpacecraftRumpEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft\SpacecraftRumpEnum 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\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...
12
use Stu\Orm\Entity\CrewAssignment;
13
use Stu\Orm\Entity\CrewAssignmentInterface;
14
use Stu\Orm\Entity\Spacecraft;
15
use Stu\Orm\Entity\SpacecraftRump;
16
use Stu\Orm\Entity\UserInterface;
17
use Stu\Orm\Entity\Crew;
18
use Stu\Orm\Entity\SpacecraftInterface;
19
use Stu\Orm\Entity\Station;
20
21
/**
22
 * @extends EntityRepository<CrewAssignment>
23
 */
24
final class CrewAssignmentRepository extends EntityRepository implements CrewAssignmentRepositoryInterface
25
{
26
    #[Override]
27
    public function prototype(): CrewAssignmentInterface
28
    {
29
        return new CrewAssignment();
30
    }
31
32
    #[Override]
33
    public function save(CrewAssignmentInterface $post): void
34
    {
35
        $em = $this->getEntityManager();
36
37
        $em->persist($post);
38
    }
39
40
    #[Override]
41
    public function delete(CrewAssignmentInterface $post): void
42
    {
43
        $em = $this->getEntityManager();
44
45
        $em->remove($post);
46
    }
47
48
    #[Override]
49
    public function getAmountBySpacecraft(SpacecraftInterface $spacecraft): int
50
    {
51
        return $this->count([
52
            'spacecraft' => $spacecraft
53
        ]);
54
    }
55
56
    #[Override]
57
    public function getByShip(int $shipId): array
58
    {
59
        return $this->findBy(
60
            ['spacecraft_id' => $shipId],
61
            ['slot' => 'asc']
62
        );
63
    }
64
65
    #[Override]
66
    public function getByShipAndSlot(int $shipId, int $slotId): array
67
    {
68
        return $this->findBy([
69
            'spacecraft_id' => $shipId,
70
            'slot' => $slotId
71
        ]);
72
    }
73
74
    /**
75
     * @return array<array{id: int, name: string, sector: string, amount: int}>
76
     */
77
    #[Override]
78
    public function getOrphanedSummaryByUserAtTradeposts(int $userId): array
79
    {
80
        $rsm = new ResultSetMapping();
81
        $rsm->addScalarResult('id', 'id', 'integer');
82
        $rsm->addScalarResult('name', 'name');
83
        $rsm->addScalarResult('sector', 'sector');
84
        $rsm->addScalarResult('amount', 'amount', 'integer');
85
86
        return $this->getEntityManager()->createNativeQuery(
87
            'SELECT tp.id as id, tp.name as name, concat(l.cx, \'|\', l.cy) as sector, count(*) as amount
88
            FROM stu_crew_assign ca
89
            JOIN stu_trade_posts tp
90
            ON ca.tradepost_id = tp.id
91
            JOIN stu_station s
92
            ON tp.station_id = s.id
93
            JOIN stu_spacecraft sp
94
            ON s.id = sp.id
95
            JOIN stu_map m
96
            ON sp.location_id = m.id
97
            JOIN stu_location l
98
            ON m.id = l.id
99
            WHERE ca.user_id = :userId
100
            GROUP BY tp.id, tp.name, l.cx, l.cy',
101
            $rsm
102
        )->setParameter('userId', $userId)
103
            ->getResult();
104
    }
105
106 2
    #[Override]
107
    public function getAmountByUser(UserInterface $user): int
108
    {
109 2
        return $this->count([
110 2
            'user' => $user
111 2
        ]);
112
    }
113
114
    #[Override]
115
    public function getByUserAtColonies(int $userId): array
116
    {
117
        return $this->getEntityManager()
118
            ->createQuery(
119
                sprintf(
120
                    'SELECT ca
121
                    FROM %s ca
122
                    WHERE ca.user_id = :userId
123
                    AND ca.colony_id IS NOT NULL',
124
                    CrewAssignment::class
125
                )
126
            )
127
            ->setParameter('userId', $userId)
128
            ->getResult();
129
    }
130
131
    #[Override]
132
    public function getByUserOnEscapePods(int $userId): array
133
    {
134
        return $this->getEntityManager()
135
            ->createQuery(
136
                sprintf(
137
                    'SELECT ca
138
                    FROM %s ca
139
                    JOIN %s s
140
                    WITH ca.spacecraft_id = s.id
141
                    JOIN %s r
142
                    WITH s.rump_id = r.id
143
                    WHERE ca.user_id = :userId
144
                    AND r.category_id = :categoryId',
145
                    CrewAssignment::class,
146
                    Spacecraft::class,
147
                    SpacecraftRump::class
148
                )
149
            )
150
            ->setParameters([
151
                'userId' => $userId,
152
                'categoryId' => SpacecraftRumpEnum::SHIP_CATEGORY_ESCAPE_PODS
153
            ])
154
            ->getResult();
155
    }
156
157
    #[Override]
158
    public function getByUserAtTradeposts(int $userId): array
159
    {
160
        return $this->getEntityManager()
161
            ->createQuery(
162
                sprintf(
163
                    'SELECT ca
164
                    FROM %s ca
165
                    WHERE ca.user_id = :userId
166
                    AND ca.tradepost_id IS NOT NULL',
167
                    CrewAssignment::class
168
                )
169
            )
170
            ->setParameter('userId', $userId)
171
            ->getResult();
172
    }
173
174
    #[Override]
175
    public function getAmountByUserOnColonies(int $userId): int
176
    {
177
        return (int)$this->getEntityManager()->createQuery(
178
            sprintf(
179
                'SELECT count(ca.id)
180
                FROM %s ca
181
                WHERE ca.user_id = :userId
182
                AND ca.colony_id IS NOT NULL',
183
                CrewAssignment::class
184
            )
185
        )->setParameter('userId', $userId)->getSingleScalarResult();
186
    }
187
188 4
    #[Override]
189
    public function getAmountByUserOnShips(UserInterface $user): int
190
    {
191 4
        return (int)$this->getEntityManager()
192 4
            ->createQuery(
193 4
                sprintf(
194 4
                    'SELECT count(ca.id)
195
                    FROM %s ca
196
                    WHERE ca.user = :user
197 4
                    AND ca.spacecraft_id IS NOT NULL',
198 4
                    CrewAssignment::class
199 4
                )
200 4
            )
201 4
            ->setParameter('user', $user)
202 4
            ->getSingleScalarResult();
203
    }
204
205 1
    #[Override]
206
    public function getAmountByUserAtTradeposts(UserInterface $user): int
207
    {
208 1
        return (int)$this->getEntityManager()
209 1
            ->createQuery(
210 1
                sprintf(
211 1
                    'SELECT count(ca.id)
212
                    FROM %s ca
213
                    WHERE ca.user = :user
214 1
                    AND ca.tradepost_id IS NOT NULL',
215 1
                    CrewAssignment::class
216 1
                )
217 1
            )
218 1
            ->setParameter('user', $user)
219 1
            ->getSingleScalarResult();
220
    }
221
222 1
    #[Override]
223
    public function getCrewsTop10(): array
224
    {
225 1
        $rsm = new ResultSetMapping();
226 1
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
227 1
        $rsm->addScalarResult('factionid', 'factionid', 'integer');
228 1
        $rsm->addScalarResult('crewc', 'crewc', 'integer');
229
230 1
        return $this->getEntityManager()->createNativeQuery(
231 1
            'SELECT ca.user_id, count(*) as crewc,
232
                (SELECT race as factionid
233
                FROM stu_user u
234
                WHERE ca.user_id = u.id) as factionid
235
            FROM stu_crew_assign ca
236
            JOIN stu_spacecraft s
237
            ON ca.spacecraft_id = s.id
238
            WHERE ca.user_id >= :firstUserId
239
            GROUP BY ca.user_id
240
            ORDER BY 2 DESC
241 1
            LIMIT 10',
242 1
            $rsm
243 1
        )->setParameter('firstUserId', UserEnum::USER_FIRST_ID)
244 1
            ->getResult();
245
    }
246
247
    #[Override]
248
    public function truncateByShip(int $shipId): void
249
    {
250
        $this->getEntityManager()
251
            ->createQuery(
252
                sprintf(
253
                    'DELETE FROM %s sc WHERE sc.spacecraft_id = :shipId',
254
                    CrewAssignment::class
255
                )
256
            )
257
            ->setParameter('shipId', $shipId)
258
            ->execute();
259
    }
260
261
    #[Override]
262
    public function truncateByUser(int $userId): void
263
    {
264
        $this->getEntityManager()
265
            ->createQuery(
266
                sprintf(
267
                    'DELETE FROM %s sc WHERE sc.user_id = :userId',
268
                    CrewAssignment::class
269
                )
270
            )
271
            ->setParameter('userId', $userId)
272
            ->execute();
273
    }
274
275 199
    #[Override]
276
    public function hasCrewOnForeignStation(UserInterface $user): bool
277
    {
278 199
        return (int) $this->getEntityManager()
279 199
            ->createQuery(
280 199
                sprintf(
281 199
                    'SELECT COUNT(ca.id)
282
                 FROM %s ca
283
                 JOIN %s c WITH ca.crew_id = c.id
284
                 JOIN %s st WITH ca.spacecraft_id = st.id
285
                 WHERE c.user_id = :user
286 199
                 AND st.user_id != :user',
287 199
                    CrewAssignment::class,
288 199
                    Crew::class,
289 199
                    Station::class
290 199
                )
291 199
            )
292 199
            ->setParameter('user', $user->getId())
293 199
            ->getSingleScalarResult() > 0;
294
    }
295
}
296