Passed
Push — dev ( 80158b...47bee4 )
by Nico
25:55
created

ShipBuildplanRepository::getShuttleBuildplan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 17
ccs 0
cts 14
cp 0
crap 2
rs 9.8666
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 Stu\Component\Ship\ShipRumpEnum;
9
use Stu\Module\PlayerSetting\Lib\UserEnum;
10
use Stu\Orm\Entity\ShipBuildplan;
11
use Stu\Orm\Entity\ShipBuildplanInterface;
12
use Stu\Orm\Entity\ShipRump;
13
use Stu\Orm\Entity\ShipRumpBuildingFunction;
14
use Stu\Orm\Entity\ShipRumpUser;
15
16
/**
17
 * @extends EntityRepository<ShipBuildplan>
18
 */
19
final class ShipBuildplanRepository extends EntityRepository implements ShipBuildplanRepositoryInterface
20
{
21
    public function getByUserAndBuildingFunction(int $userId, int $buildingFunction): array
22
    {
23
        return $this->getEntityManager()
24
            ->createQuery(
25
                sprintf(
26
                    'SELECT b FROM %s b WHERE b.user_id = :userId AND b.rump_id IN (
27
                        SELECT bf.rump_id FROM %s bf WHERE bf.building_function = :buildingFunction
28
                    )',
29
                    ShipBuildplan::class,
30
                    ShipRumpBuildingFunction::class
31
                )
32
            )
33
            ->setParameters([
34
                'userId' => $userId,
35
                'buildingFunction' => $buildingFunction
36
            ])
37
            ->getResult();
38
    }
39
40
    public function getCountByRumpAndUser(int $rumpId, int $userId): int
41
    {
42
        return $this->count([
43
            'rump_id' => $rumpId,
44
            'user_id' => $userId,
45
        ]);
46
    }
47
48
    public function getByUserShipRumpAndSignature(
49
        int $userId,
50
        int $shipRumpId,
51
        string $signature
52
    ): ?ShipBuildplanInterface {
53
        return $this->findOneBy([
54
            'user_id' => $userId,
55
            'rump_id' => $shipRumpId,
56
            'signature' => $signature
57
        ]);
58
    }
59
60
    public function getShuttleBuildplan(int $commodityId): ?ShipBuildplanInterface
61
    {
62
        return $this->getEntityManager()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getEntityM...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return Stu\Orm\Entity\ShipBuildplanInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
63
            ->createQuery(
64
                sprintf(
65
                    'SELECT sb FROM %s sb
66
                    JOIN %s sr
67
                    WITH sb.rump_id = sr.id
68
                    WHERE sr.commodity_id = :commodityId',
69
                    ShipBuildplan::class,
70
                    ShipRump::class
71
                )
72
            )
73
            ->setParameters([
74
                'commodityId' => $commodityId
75
            ])
76
            ->getOneOrNullResult();
77
    }
78
79
    public function getStationBuildplansByUser(int $userId): array
80
    {
81
        return $this->getEntityManager()
82
            ->createQuery(
83
                sprintf(
84
                    'SELECT bp FROM %s bp
85
                    JOIN %s r
86
                    WITH bp.rump_id = r.id
87
                    WHERE r.category_id = :category
88
                    AND r.id IN (
89
                        SELECT ru.rump_id FROM %s ru WHERE ru.user_id = :userId
90
                    )
91
                    ORDER BY r.id ASC',
92
                    ShipBuildplan::class,
93
                    ShipRump::class,
94
                    ShipRumpUser::class
95
                )
96
            )
97
            ->setParameters([
98
                'category' => ShipRumpEnum::SHIP_CATEGORY_STATION,
99
                'userId' => $userId
100
            ])
101
            ->getResult();
102
    }
103
104
    public function getShipyardBuildplansByUser(int $userId): array
105
    {
106
        return $this->getEntityManager()
107
            ->createQuery(
108
                sprintf(
109
                    'SELECT bp FROM %s bp
110
                    JOIN %s r
111
                    WITH bp.rump_id = r.id
112
                    WHERE r.category_id != :category
113
                    AND bp.user_id = :userId
114
                    ORDER BY r.id ASC',
115
                    ShipBuildplan::class,
116
                    ShipRump::class
117
                )
118
            )
119
            ->setParameters([
120
                'category' => ShipRumpEnum::SHIP_CATEGORY_STATION,
121
                'userId' => $userId
122
            ])
123
            ->getResult();
124
    }
125
126
    public function prototype(): ShipBuildplanInterface
127
    {
128
        return new ShipBuildplan();
129
    }
130
131
    public function save(ShipBuildplanInterface $shipBuildplan): void
132
    {
133
        $em = $this->getEntityManager();
134
135
        $em->persist($shipBuildplan);
136
        $em->flush();
137
    }
138
139
    public function delete(ShipBuildplanInterface $shipBuildplan): void
140
    {
141
        $em = $this->getEntityManager();
142
143
        $em->remove($shipBuildplan);
144
    }
145
146
    public function getByUser(int $userId): array
147
    {
148
        return $this->findBy([
149
            'user_id' => $userId
150
        ]);
151
    }
152
153
    public function findByUserAndName(int $userId, string $name): ?ShipBuildplanInterface
154
    {
155
        return $this->findOneBy([
156
            'user_id' => $userId,
157
            'name' => $name
158
        ]);
159
    }
160
161
    public function truncateAllBuildplansExceptNoOne(): void
162
    {
163
        $this->getEntityManager()
164
            ->createQuery(
165
                sprintf(
166
                    'DELETE FROM %s bp
167
                    WHERE bp.user_id != :noOne',
168
                    ShipBuildplan::class
169
                )
170
            )
171
            ->setParameters([
172
                'noOne' => UserEnum::USER_NOONE
173
            ])
174
            ->execute();
175
    }
176
177
    public function getByUserAndRump(int $userId, int $rumpId): array
178
    {
179
        return $this->findBy([
180
            'user_id' => $userId,
181
            'rump_id' => $rumpId,
182
        ]);
183
    }
184
}
185