Passed
Push — master ( 7d944e...416fd2 )
by Janko
10:32 queued 05:08
created

CrewAssignmentRepository::truncateBySpacecraft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

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