Passed
Push — dev ( 0dbbcc...f29cfd )
by Janko
10:15
created

getStationBuildplansByUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 1
dl 0
loc 24
ccs 16
cts 16
cp 1
crap 1
rs 9.6333
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 getShipyardBuildplansByUser(int $userId): array
113
    {
114
        return $this->getEntityManager()
115
            ->createQuery(
116
                sprintf(
117
                    'SELECT bp FROM %s bp
118
                    JOIN %s r
119
                    WITH bp.rump_id = r.id
120
                    WHERE r.category_id != :category
121
                    AND bp.user_id = :userId
122
                    ORDER BY r.id ASC',
123
                    SpacecraftBuildplan::class,
124
                    SpacecraftRump::class
125
                )
126
            )
127
            ->setParameters([
128
                'category' => SpacecraftRumpEnum::SHIP_CATEGORY_STATION,
129
                'userId' => $userId
130
            ])
131
            ->getResult();
132
    }
133
134
    #[Override]
135
    public function prototype(): SpacecraftBuildplanInterface
136
    {
137
        return new SpacecraftBuildplan();
138
    }
139
140
    #[Override]
141
    public function save(SpacecraftBuildplanInterface $spacecraftBuildplan): void
142
    {
143
        $em = $this->getEntityManager();
144
145
        $em->persist($spacecraftBuildplan);
146
    }
147
148
    #[Override]
149
    public function delete(SpacecraftBuildplanInterface $spacecraftBuildplan): void
150
    {
151
        $em = $this->getEntityManager();
152
153
        $em->remove($spacecraftBuildplan);
154
    }
155
156
    #[Override]
157
    public function getByUser(int $userId): array
158
    {
159
        return $this->findBy([
160
            'user_id' => $userId
161
        ]);
162
    }
163
164
    #[Override]
165
    public function findByUserAndName(int $userId, string $name): ?SpacecraftBuildplanInterface
166
    {
167
        return $this->findOneBy([
168
            'user_id' => $userId,
169
            'name' => $name
170
        ]);
171
    }
172
173
    public function getAllNonNpcBuildplans(): array
174
    {
175
        return $this->getEntityManager()
176
            ->createQuery(
177
                sprintf(
178
                    'SELECT bp FROM %s bp
179
                    WHERE bp.user_id >= :firstUserId',
180
                    SpacecraftBuildplan::class
181
                )
182
            )
183
            ->setParameter('firstUserId', UserEnum::USER_FIRST_ID)
184
            ->getResult();
185
    }
186
187
    #[Override]
188
    public function truncateAllBuildplansExceptNoOne(): void
189
    {
190
        $this->getEntityManager()
191
            ->createQuery(
192
                sprintf(
193
                    'DELETE FROM %s bp
194
                    WHERE bp.user_id != :noOne',
195
                    SpacecraftBuildplan::class
196
                )
197
            )
198
            ->setParameters([
199
                'noOne' => UserEnum::USER_NOONE
200
            ])
201
            ->execute();
202
    }
203
204 1
    #[Override]
205
    public function getByUserAndRump(int $userId, int $rumpId): array
206
    {
207 1
        return $this->findBy([
208 1
            'user_id' => $userId,
209 1
            'rump_id' => $rumpId,
210 1
        ]);
211
    }
212
}
213