Passed
Pull Request — dev (#2038)
by Janko
09:06
created

CrewAssignmentRepository::getByShipAndSlot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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