Passed
Pull Request — master (#2113)
by Nico
31:05 queued 21:07
created

hasCrewOnForeignStation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

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