Passed
Push — dev ( 66fe9c...34655c )
by Nico
18:31
created

PlanetFieldRepository   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 319
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 190
dl 0
loc 319
ccs 0
cts 219
cp 0
rs 10
c 0
b 0
f 0
wmc 22

22 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 5 1
A delete() 0 6 1
A getEnergyConsumingByColony() 0 19 1
A getByColonyAndType() 0 5 1
A prototype() 0 3 1
A getByColonyAndFieldId() 0 5 1
A getEnergyProducingByColony() 0 13 1
A getWorkerConsumingByColony() 0 13 1
A getHousingProvidingByColony() 0 13 1
A getInConstructionByUser() 0 13 1
A getByConstructionFinish() 0 10 1
A getCommodityConsumingByColonyAndCommodity() 0 21 1
A getEnergyProductionByColony() 0 13 1
A getMaxShieldsOfColony() 0 24 1
A getCountByBuildingAndUser() 0 14 1
A getCommodityProducingByColonyAndCommodity() 0 14 1
A truncateByColony() 0 10 1
A getByColonyAndBuildingFunction() 0 19 1
A getWorkerConsumingByColonyAndState() 0 19 1
A getByColonyWithBuilding() 0 10 1
A getCountByColonyAndBuilding() 0 5 1
A getCountByColonyAndBuildingFunctionAndState() 0 18 1
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 Stu\Component\Building\BuildingEnum;
10
use Stu\Orm\Entity\Building;
11
use Stu\Orm\Entity\BuildingCommodity;
12
use Stu\Orm\Entity\BuildingFunction;
13
use Stu\Orm\Entity\Colony;
14
use Stu\Orm\Entity\ColonyInterface;
15
use Stu\Orm\Entity\PlanetField;
16
use Stu\Orm\Entity\PlanetFieldInterface;
17
18
/**
19
 * @extends EntityRepository<PlanetField>
20
 */
21
final class PlanetFieldRepository extends EntityRepository implements PlanetFieldRepositoryInterface
22
{
23
    public function prototype(): PlanetFieldInterface
24
    {
25
        return new PlanetField();
26
    }
27
28
    public function save(PlanetFieldInterface $planetField): void
29
    {
30
        $em = $this->getEntityManager();
31
32
        $em->persist($planetField);
33
    }
34
35
    public function delete(PlanetFieldInterface $planetField): void
36
    {
37
        $em = $this->getEntityManager();
38
39
        $em->remove($planetField);
40
        $em->flush();
41
    }
42
43
    public function getByColonyAndFieldId(int $colonyId, int $fieldId): ?PlanetFieldInterface
44
    {
45
        return $this->findOneBy([
46
            'colonies_id' => $colonyId,
47
            'field_id' => $fieldId,
48
        ]);
49
    }
50
51
    public function getByColonyAndType(int $colonyId, int $planetFieldTypeId): array
52
    {
53
        return $this->findBy([
54
            'colonies_id' => $colonyId,
55
            'type_id' => $planetFieldTypeId,
56
        ]);
57
    }
58
59
    public function getEnergyConsumingByColony(
60
        int $colonyId,
61
        array $state = [0, 1],
62
        ?int $limit = null
63
    ): iterable {
64
        return $this->getEntityManager()->createQuery(
65
            sprintf(
66
                'SELECT f FROM %s f WHERE f.colonies_id = :colonyId AND f.buildings_id IN (
67
                    SELECT b.id FROM %s b WHERE b.eps_proc < 0
68
                ) AND f.aktiv IN (:state)',
69
                PlanetField::class,
70
                Building::class
71
            )
72
        )->setParameters([
73
            'colonyId' => $colonyId,
74
            'state' => $state
75
        ])
76
            ->setMaxResults($limit)
77
            ->getResult();
78
    }
79
80
    public function getEnergyProducingByColony(int $colonyId): iterable
81
    {
82
        return $this->getEntityManager()->createQuery(
83
            sprintf(
84
                'SELECT f FROM %s f WHERE f.colonies_id = :colonyId AND f.buildings_id IN (
85
                    SELECT b.id FROM %s b WHERE b.eps_proc > 0
86
                )',
87
                PlanetField::class,
88
                Building::class
89
            )
90
        )->setParameters([
91
            'colonyId' => $colonyId,
92
        ])->getResult();
93
    }
94
95
    public function getHousingProvidingByColony(int $colonyId): iterable
96
    {
97
        return $this->getEntityManager()->createQuery(
98
            sprintf(
99
                'SELECT f FROM %s f WHERE f.colonies_id = :colonyId AND f.buildings_id IN (
100
                    SELECT b.id FROM %s b WHERE b.bev_pro > 0
101
                )',
102
                PlanetField::class,
103
                Building::class
104
            )
105
        )->setParameters([
106
            'colonyId' => $colonyId,
107
        ])->getResult();
108
    }
109
110
    public function getWorkerConsumingByColony(int $colonyId): iterable
111
    {
112
        return $this->getEntityManager()->createQuery(
113
            sprintf(
114
                'SELECT f FROM %s f WHERE f.colonies_id = :colonyId AND f.buildings_id IN (
115
                    SELECT b.id FROM %s b WHERE b.bev_use > 0
116
                )',
117
                PlanetField::class,
118
                Building::class
119
            )
120
        )->setParameters([
121
            'colonyId' => $colonyId,
122
        ])->getResult();
123
    }
124
125
    public function getWorkerConsumingByColonyAndState(
126
        int $colonyId,
127
        array $state = [0, 1],
128
        ?int $limit = null
129
    ): iterable {
130
        return $this->getEntityManager()->createQuery(
131
            sprintf(
132
                'SELECT f FROM %s f WHERE f.colonies_id = :colonyId AND f.buildings_id IN (
133
                    SELECT b.id FROM %s b WHERE b.bev_use > 0 AND f.aktiv IN (:state)
134
                )',
135
                PlanetField::class,
136
                Building::class
137
            )
138
        )->setParameters([
139
            'colonyId' => $colonyId,
140
            'state' => $state
141
        ])
142
            ->setMaxResults($limit)
143
            ->getResult();
144
    }
145
146
    public function getCommodityConsumingByColonyAndCommodity(
147
        int $colonyId,
148
        int $commodityId,
149
        array $state = [0, 1],
150
        ?int $limit = null
151
    ): iterable {
152
        return $this->getEntityManager()->createQuery(
153
            sprintf(
154
                'SELECT f FROM %s f WHERE f.colonies_id = :colonyId AND f.buildings_id IN (
155
                    SELECT bg.buildings_id FROM %s bg WHERE bg.commodity_id = :commodityId AND bg.count < 0
156
                ) AND f.aktiv IN (:state)',
157
                PlanetField::class,
158
                BuildingCommodity::class
159
            )
160
        )->setParameters([
161
            'colonyId' => $colonyId,
162
            'commodityId' => $commodityId,
163
            'state' => $state
164
        ])
165
            ->setMaxResults($limit)
166
            ->getResult();
167
    }
168
169
    public function getCommodityProducingByColonyAndCommodity(int $colonyId, int $commodityId): iterable
170
    {
171
        return $this->getEntityManager()->createQuery(
172
            sprintf(
173
                'SELECT f FROM %s f WHERE f.colonies_id = :colonyId AND f.buildings_id IN (
174
                    SELECT bg.buildings_id FROM %s bg WHERE bg.commodity_id = :commodityId AND bg.count > 0
175
                )',
176
                PlanetField::class,
177
                BuildingCommodity::class
178
            )
179
        )->setParameters([
180
            'colonyId' => $colonyId,
181
            'commodityId' => $commodityId,
182
        ])->getResult();
183
    }
184
185
    public function getCountByColonyAndBuilding(int $colonyId, int $buildingId): int
186
    {
187
        return $this->count([
188
            'colonies_id' => $colonyId,
189
            'buildings_id' => $buildingId,
190
        ]);
191
    }
192
193
    public function getCountByBuildingAndUser(int $buildingId, int $userId): int
194
    {
195
        return (int) $this->getEntityManager()->createQuery(
196
            sprintf(
197
                'SELECT COUNT(f) FROM %s f WHERE f.buildings_id = :buildingId AND f.colonies_id IN (
198
                    SELECT c.id FROM %s c WHERE c.user_id = :userId
199
                )',
200
                PlanetField::class,
201
                Colony::class
202
            )
203
        )->setParameters([
204
            'buildingId' => $buildingId,
205
            'userId' => $userId,
206
        ])->getSingleScalarResult();
207
    }
208
209
    public function getCountByColonyAndBuildingFunctionAndState(
210
        int $colonyId,
211
        array $buildingFunctionIds,
212
        array $state
213
    ): int {
214
        return (int) $this->getEntityManager()->createQuery(
215
            sprintf(
216
                'SELECT COUNT(f) FROM %s f WHERE f.colonies_id = :colonyId AND f.aktiv IN(:state) AND f.buildings_id IN (
217
                    SELECT bf.buildings_id FROM %s bf WHERE bf.function IN (:buildingFunctionId)
218
                )',
219
                PlanetField::class,
220
                BuildingFunction::class
221
            )
222
        )->setParameters([
223
            'colonyId' => $colonyId,
224
            'buildingFunctionId' => $buildingFunctionIds,
225
            'state' => $state,
226
        ])->getSingleScalarResult();
227
    }
228
229
    public function getByColonyAndBuildingFunction(
230
        int $colonyId,
231
        array $buildingFunctionIds
232
    ): array {
233
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...ctionIds))->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
234
            ->createQuery(
235
                sprintf(
236
                    'SELECT f FROM %s f WHERE f.colonies_id = :colonyId AND f.buildings_id IN (
237
                        SELECT bf.buildings_id FROM %s bf WHERE bf.function IN (:buildingFunctionId)
238
                    )',
239
                    PlanetField::class,
240
                    BuildingFunction::class
241
                )
242
            )
243
            ->setParameters([
244
                'colonyId' => $colonyId,
245
                'buildingFunctionId' => $buildingFunctionIds
246
            ])
247
            ->getResult();
248
    }
249
250
    public function getMaxShieldsOfColony(int $colonyId): int
251
    {
252
        $rsm = new ResultSetMapping();
253
        $rsm->addScalarResult('capacity', 'capacity');
254
255
        return (int) $this->getEntityManager()->createNativeQuery(
256
            'SELECT COUNT(distinct f1.id) * :generatorCapacity + COUNT(distinct f2.id) * :batteryCapacity as capacity
257
            FROM stu_colonies_fielddata f1
258
            LEFT JOIN stu_colonies_fielddata f2
259
            ON f1.colonies_id = f2.colonies_id
260
            AND f2.aktiv = 1 AND f2.buildings_id IN (
261
                SELECT bf2.buildings_id FROM stu_buildings_functions bf2 WHERE bf2.function = :shieldBattery
262
            )
263
            WHERE f1.colonies_id = :colonyId AND f1.aktiv = 1 AND f1.buildings_id IN (
264
                SELECT bf1.buildings_id FROM stu_buildings_functions bf1 WHERE bf1.function = :shieldGenerator
265
            )',
266
            $rsm
267
        )->setParameters([
268
            'colonyId' => $colonyId,
269
            'shieldGenerator' => BuildingEnum::BUILDING_FUNCTION_SHIELD_GENERATOR,
270
            'shieldBattery' => BuildingEnum::BUILDING_FUNCTION_SHIELD_BATTERY,
271
            'generatorCapacity' => BuildingEnum::SHIELD_GENERATOR_CAPACITY,
272
            'batteryCapacity' => BuildingEnum::SHIELD_BATTERY_CAPACITY
273
        ])->getSingleScalarResult();
274
    }
275
276
    public function getInConstructionByUser(int $userId): iterable
277
    {
278
        return $this->getEntityManager()->createQuery(
279
            sprintf(
280
                'SELECT f FROM %s f WHERE f.aktiv > 1 AND f.colonies_id IN (
281
                    SELECT c.id FROM %s c WHERE c.user_id = :userId
282
                ) ORDER BY f.aktiv',
283
                PlanetField::class,
284
                Colony::class
285
            )
286
        )->setParameters([
287
            'userId' => $userId,
288
        ])->getResult();
289
    }
290
291
    public function getByConstructionFinish(int $finishTime): iterable
292
    {
293
        return $this->getEntityManager()->createQuery(
294
            sprintf(
295
                'SELECT f FROM %s f WHERE f.aktiv BETWEEN 2 AND :finishTime',
296
                PlanetField::class
297
            )
298
        )->setParameters([
299
            'finishTime' => $finishTime,
300
        ])->getResult();
301
    }
302
303
    public function getByColonyWithBuilding(int $colonyId): array
304
    {
305
        return $this->getEntityManager()->createQuery(
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...colonyId))->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
306
            sprintf(
307
                'SELECT f FROM %s f WHERE f.colonies_id = :colonyId AND f.buildings_id > 0',
308
                PlanetField::class
309
            )
310
        )->setParameters([
311
            'colonyId' => $colonyId,
312
        ])->getResult();
313
    }
314
315
    public function getEnergyProductionByColony(int $colonyId): int
316
    {
317
        return (int) $this->getEntityManager()->createQuery(
318
            sprintf(
319
                'SELECT SUM(b.eps_proc) FROM %s cfd LEFT JOIN %s b WITH b.id = cfd.buildings_id WHERE
320
                cfd.aktiv = :state AND cfd.colonies_id = :colonyId',
321
                PlanetField::class,
322
                Building::class
323
            )
324
        )->setParameters([
325
            'state' => 1,
326
            'colonyId' => $colonyId,
327
        ])->getSingleScalarResult();
328
    }
329
330
    public function truncateByColony(ColonyInterface $colony): void
331
    {
332
        $this->getEntityManager()->createQuery(
333
            sprintf(
334
                'DELETE FROM %s pf WHERE pf.colonies_id = :colonyId',
335
                PlanetField::class
336
            )
337
        )
338
            ->setParameters(['colonyId' => $colony])
339
            ->execute();
340
    }
341
}
342