Passed
Pull Request — master (#2098)
by Nico
25:05 queued 14:42
created

getStationBuildplanByRump()   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
nc 1
nop 1
dl 0
loc 5
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 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...
9
use Stu\Component\Building\BuildingFunctionEnum;
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\SpacecraftBuildplan;
13
use Stu\Orm\Entity\SpacecraftBuildplanInterface;
14
use Stu\Orm\Entity\ShipRumpBuildingFunction;
15
use Stu\Orm\Entity\ShipRumpUser;
16
use Stu\Orm\Entity\SpacecraftRump;
17
18
/**
19
 * @extends EntityRepository<SpacecraftBuildplan>
20
 */
21
final class SpacecraftBuildplanRepository extends EntityRepository implements SpacecraftBuildplanRepositoryInterface
22
{
23 1
    #[Override]
24
    public function getByUserAndBuildingFunction(int $userId, BuildingFunctionEnum $buildingFunction): array
25
    {
26 1
        return $this->getEntityManager()
27 1
            ->createQuery(
28 1
                sprintf(
29 1
                    'SELECT b FROM %s b WHERE b.user_id = :userId AND b.rump_id IN (
30
                        SELECT bf.rump_id FROM %s bf WHERE bf.building_function = :buildingFunction
31 1
                    )',
32 1
                    SpacecraftBuildplan::class,
33 1
                    ShipRumpBuildingFunction::class
34 1
                )
35 1
            )
36 1
            ->setParameters([
37 1
                'userId' => $userId,
38 1
                'buildingFunction' => $buildingFunction->value
39 1
            ])
40 1
            ->getResult();
41
    }
42
43
    #[Override]
44
    public function getCountByRumpAndUser(int $rumpId, int $userId): int
45
    {
46
        return $this->count([
47
            'rump_id' => $rumpId,
48
            'user_id' => $userId,
49
        ]);
50
    }
51
52
    #[Override]
53
    public function getByUserShipRumpAndSignature(
54
        int $userId,
55
        int $rumpId,
56
        string $signature
57
    ): ?SpacecraftBuildplanInterface {
58
        return $this->findOneBy([
59
            'user_id' => $userId,
60
            'rump_id' => $rumpId,
61
            'signature' => $signature
62
        ]);
63
    }
64
65
    #[Override]
66
    public function getShuttleBuildplan(int $commodityId): ?SpacecraftBuildplanInterface
67
    {
68
        return $this->getEntityManager()
69
            ->createQuery(
70
                sprintf(
71
                    'SELECT sb FROM %s sb
72
                    JOIN %s sr
73
                    WITH sb.rump_id = sr.id
74
                    WHERE sr.commodity_id = :commodityId',
75
                    SpacecraftBuildplan::class,
76
                    SpacecraftRump::class
77
                )
78
            )
79
            ->setParameters([
80
                'commodityId' => $commodityId
81
            ])
82
            ->getOneOrNullResult();
83
    }
84
85 1
    #[Override]
86
    public function getStationBuildplansByUser(int $userId): array
87
    {
88 1
        return $this->getEntityManager()
89 1
            ->createQuery(
90 1
                sprintf(
91 1
                    'SELECT bp FROM %s bp
92
                    JOIN %s r
93
                    WITH bp.rump_id = r.id
94
                    WHERE r.category_id = :category
95
                    AND r.id IN (
96
                        SELECT ru.rump_id FROM %s ru WHERE ru.user_id = :userId
97
                    )
98 1
                    ORDER BY r.id ASC',
99 1
                    SpacecraftBuildplan::class,
100 1
                    SpacecraftRump::class,
101 1
                    ShipRumpUser::class
102 1
                )
103 1
            )
104 1
            ->setParameters([
105 1
                'category' => SpacecraftRumpEnum::SHIP_CATEGORY_STATION,
106 1
                'userId' => $userId
107 1
            ])
108 1
            ->getResult();
109
    }
110
111
    #[Override]
112
    public function getStationBuildplanByRump(int $rumpId): ?SpacecraftBuildplanInterface
113
    {
114
        return $this->findOneBy([
115
            'rump_id' => $rumpId
116
        ]);
117
    }
118
119
    #[Override]
120
    public function getShipyardBuildplansByUser(int $userId): array
121
    {
122
        return $this->getEntityManager()
123
            ->createQuery(
124
                sprintf(
125
                    'SELECT bp FROM %s bp
126
                    JOIN %s r
127
                    WITH bp.rump_id = r.id
128
                    WHERE r.category_id != :category
129
                    AND bp.user_id = :userId
130
                    ORDER BY r.id ASC',
131
                    SpacecraftBuildplan::class,
132
                    SpacecraftRump::class
133
                )
134
            )
135
            ->setParameters([
136
                'category' => SpacecraftRumpEnum::SHIP_CATEGORY_STATION,
137
                'userId' => $userId
138
            ])
139
            ->getResult();
140
    }
141
142
    #[Override]
143
    public function prototype(): SpacecraftBuildplanInterface
144
    {
145
        return new SpacecraftBuildplan();
146
    }
147
148
    #[Override]
149
    public function save(SpacecraftBuildplanInterface $spacecraftBuildplan): void
150
    {
151
        $em = $this->getEntityManager();
152
153
        $em->persist($spacecraftBuildplan);
154
    }
155
156
    #[Override]
157
    public function delete(SpacecraftBuildplanInterface $spacecraftBuildplan): void
158
    {
159
        $em = $this->getEntityManager();
160
161
        $em->remove($spacecraftBuildplan);
162
    }
163
164
    #[Override]
165
    public function getByUser(int $userId): array
166
    {
167
        return $this->findBy([
168
            'user_id' => $userId
169
        ]);
170
    }
171
172
    #[Override]
173
    public function findByUserAndName(int $userId, string $name): ?SpacecraftBuildplanInterface
174
    {
175
        return $this->findOneBy([
176
            'user_id' => $userId,
177
            'name' => $name
178
        ]);
179
    }
180
181
    public function getAllNonNpcBuildplans(): array
182
    {
183
        return $this->getEntityManager()
184
            ->createQuery(
185
                sprintf(
186
                    'SELECT bp FROM %s bp
187
                    WHERE bp.user_id >= :firstUserId',
188
                    SpacecraftBuildplan::class
189
                )
190
            )
191
            ->setParameter('firstUserId', UserEnum::USER_FIRST_ID)
192
            ->getResult();
193
    }
194
195
    #[Override]
196
    public function truncateAllBuildplansExceptNoOne(): void
197
    {
198
        $this->getEntityManager()
199
            ->createQuery(
200
                sprintf(
201
                    'DELETE FROM %s bp
202
                    WHERE bp.user_id != :noOne',
203
                    SpacecraftBuildplan::class
204
                )
205
            )
206
            ->setParameters([
207
                'noOne' => UserEnum::USER_NOONE
208
            ])
209
            ->execute();
210
    }
211
212 1
    #[Override]
213
    public function getByUserAndRump(int $userId, int $rumpId): array
214
    {
215 1
        return $this->findBy([
216 1
            'user_id' => $userId,
217 1
            'rump_id' => $rumpId,
218 1
        ]);
219
    }
220
}