Passed
Push — dev ( 00bc98...ea1fa5 )
by Janko
14:47
created

CrewAssignmentRepository::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 hasEnoughCrew(SpacecraftInterface $spacecraft): bool
58
    {
59
        return $this->getAmountBySpacecraft($spacecraft) >= $spacecraft->getNeededCrewCount();
60
    }
61
62
    #[Override]
63
    public function hasCrewmanOfUser(SpacecraftInterface $spacecraft, int $userId): bool
64
    {
65
        return (int)$this->getEntityManager()
66
            ->createQuery(
67
                sprintf(
68
                    'SELECT count(ca)
69
                    FROM %s ca
70
                    JOIN %s c
71
                    WITH ca.crew = c
72
                    WHERE c.user_id = :userId
73
                    AND ca.spacecraft = :spacecraft',
74
                    CrewAssignment::class,
75
                    Crew::class
76
                )
77
            )
78
            ->setParameters([
79
                'userId' => $userId,
80
                'spacecraft' => $spacecraft
81
            ])
82
            ->getSingleScalarResult() > 0;
83
    }
84
85
    #[Override]
86
    public function getByShip(int $shipId): array
87
    {
88
        return $this->findBy(
89
            ['spacecraft_id' => $shipId],
90
            ['slot' => 'asc']
91
        );
92
    }
93
94
    #[Override]
95
    public function getByShipAndSlot(int $shipId, int $slotId): array
96
    {
97
        return $this->findBy([
98
            'spacecraft_id' => $shipId,
99
            'slot' => $slotId
100
        ]);
101
    }
102
103
    /**
104
     * @return array<array{id: int, name: string, sector: string, amount: int}>
105
     */
106
    #[Override]
107
    public function getOrphanedSummaryByUserAtTradeposts(int $userId): array
108
    {
109
        $rsm = new ResultSetMapping();
110
        $rsm->addScalarResult('id', 'id', 'integer');
111
        $rsm->addScalarResult('name', 'name');
112
        $rsm->addScalarResult('sector', 'sector');
113
        $rsm->addScalarResult('amount', 'amount', 'integer');
114
115
        return $this->getEntityManager()->createNativeQuery(
116
            'SELECT tp.id as id, tp.name as name, concat(l.cx, \'|\', l.cy) as sector, count(*) as amount
117
            FROM stu_crew_assign ca
118
            JOIN stu_trade_posts tp
119
            ON ca.tradepost_id = tp.id
120
            JOIN stu_station s
121
            ON tp.station_id = s.id
122
            JOIN stu_spacecraft sp
123
            ON s.id = sp.id
124
            JOIN stu_map m
125
            ON sp.location_id = m.id
126
            JOIN stu_location l
127
            ON m.id = l.id
128
            WHERE ca.user_id = :userId
129
            GROUP BY tp.id, tp.name, l.cx, l.cy',
130
            $rsm
131
        )->setParameter('userId', $userId)
132
            ->getResult();
133
    }
134
135 2
    #[Override]
136
    public function getAmountByUser(UserInterface $user): int
137
    {
138 2
        return $this->count([
139 2
            'user' => $user
140 2
        ]);
141
    }
142
143
    #[Override]
144
    public function getByUserAtColonies(int $userId): array
145
    {
146
        return $this->getEntityManager()
147
            ->createQuery(
148
                sprintf(
149
                    'SELECT ca
150
                    FROM %s ca
151
                    WHERE ca.user_id = :userId
152
                    AND ca.colony_id IS NOT NULL',
153
                    CrewAssignment::class
154
                )
155
            )
156
            ->setParameter('userId', $userId)
157
            ->getResult();
158
    }
159
160
    #[Override]
161
    public function getByUserOnEscapePods(int $userId): array
162
    {
163
        return $this->getEntityManager()
164
            ->createQuery(
165
                sprintf(
166
                    'SELECT ca
167
                    FROM %s ca
168
                    JOIN %s s
169
                    WITH ca.spacecraft_id = s.id
170
                    JOIN %s r
171
                    WITH s.rump_id = r.id
172
                    WHERE ca.user_id = :userId
173
                    AND r.category_id = :categoryId',
174
                    CrewAssignment::class,
175
                    Spacecraft::class,
176
                    SpacecraftRump::class
177
                )
178
            )
179
            ->setParameters([
180
                'userId' => $userId,
181
                'categoryId' => SpacecraftRumpEnum::SHIP_CATEGORY_ESCAPE_PODS
182
            ])
183
            ->getResult();
184
    }
185
186
    #[Override]
187
    public function getByUserAtTradeposts(int $userId): array
188
    {
189
        return $this->getEntityManager()
190
            ->createQuery(
191
                sprintf(
192
                    'SELECT ca
193
                    FROM %s ca
194
                    WHERE ca.user_id = :userId
195
                    AND ca.tradepost_id IS NOT NULL',
196
                    CrewAssignment::class
197
                )
198
            )
199
            ->setParameter('userId', $userId)
200
            ->getResult();
201
    }
202
203
    #[Override]
204
    public function getAmountByUserOnColonies(int $userId): int
205
    {
206
        return (int)$this->getEntityManager()->createQuery(
207
            sprintf(
208
                'SELECT count(ca.id)
209
                FROM %s ca
210
                WHERE ca.user_id = :userId
211
                AND ca.colony_id IS NOT NULL',
212
                CrewAssignment::class
213
            )
214
        )->setParameter('userId', $userId)->getSingleScalarResult();
215
    }
216
217 4
    #[Override]
218
    public function getAmountByUserOnShips(UserInterface $user): int
219
    {
220 4
        return (int)$this->getEntityManager()
221 4
            ->createQuery(
222 4
                sprintf(
223 4
                    'SELECT count(ca.id)
224
                    FROM %s ca
225
                    WHERE ca.user = :user
226 4
                    AND ca.spacecraft_id IS NOT NULL',
227 4
                    CrewAssignment::class
228 4
                )
229 4
            )
230 4
            ->setParameter('user', $user)
231 4
            ->getSingleScalarResult();
232
    }
233
234 1
    #[Override]
235
    public function getAmountByUserAtTradeposts(UserInterface $user): int
236
    {
237 1
        return (int)$this->getEntityManager()
238 1
            ->createQuery(
239 1
                sprintf(
240 1
                    'SELECT count(ca.id)
241
                    FROM %s ca
242
                    WHERE ca.user = :user
243 1
                    AND ca.tradepost_id IS NOT NULL',
244 1
                    CrewAssignment::class
245 1
                )
246 1
            )
247 1
            ->setParameter('user', $user)
248 1
            ->getSingleScalarResult();
249
    }
250
251 1
    #[Override]
252
    public function getCrewsTop10(): array
253
    {
254 1
        $rsm = new ResultSetMapping();
255 1
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
256 1
        $rsm->addScalarResult('factionid', 'factionid', 'integer');
257 1
        $rsm->addScalarResult('crewc', 'crewc', 'integer');
258
259 1
        return $this->getEntityManager()->createNativeQuery(
260 1
            'SELECT ca.user_id, count(*) as crewc,
261
                (SELECT race as factionid
262
                FROM stu_user u
263
                WHERE ca.user_id = u.id) as factionid
264
            FROM stu_crew_assign ca
265
            JOIN stu_spacecraft s
266
            ON ca.spacecraft_id = s.id
267
            WHERE ca.user_id >= :firstUserId
268
            GROUP BY ca.user_id
269
            ORDER BY 2 DESC
270 1
            LIMIT 10',
271 1
            $rsm
272 1
        )->setParameter('firstUserId', UserEnum::USER_FIRST_ID)
273 1
            ->getResult();
274
    }
275
276
    #[Override]
277
    public function truncateByShip(int $shipId): void
278
    {
279
        $this->getEntityManager()
280
            ->createQuery(
281
                sprintf(
282
                    'DELETE FROM %s sc WHERE sc.spacecraft_id = :shipId',
283
                    CrewAssignment::class
284
                )
285
            )
286
            ->setParameter('shipId', $shipId)
287
            ->execute();
288
    }
289
290
    #[Override]
291
    public function truncateByUser(int $userId): void
292
    {
293
        $this->getEntityManager()
294
            ->createQuery(
295
                sprintf(
296
                    'DELETE FROM %s sc WHERE sc.user_id = :userId',
297
                    CrewAssignment::class
298
                )
299
            )
300
            ->setParameter('userId', $userId)
301
            ->execute();
302
    }
303
304 199
    #[Override]
305
    public function hasCrewOnForeignStation(UserInterface $user): bool
306
    {
307 199
        return (int) $this->getEntityManager()
308 199
            ->createQuery(
309 199
                sprintf(
310 199
                    'SELECT COUNT(ca.id)
311
                 FROM %s ca
312
                 JOIN %s c WITH ca.crew_id = c.id
313
                 JOIN %s st WITH ca.spacecraft_id = st.id
314
                 WHERE c.user_id = :user
315 199
                 AND st.user_id != :user',
316 199
                    CrewAssignment::class,
317 199
                    Crew::class,
318 199
                    Station::class
319 199
                )
320 199
            )
321 199
            ->setParameter('user', $user->getId())
322 199
            ->getSingleScalarResult() > 0;
323
    }
324
}
325