Test Failed
Push — master ( 87706c...9d01da )
by Nico
24:23 queued 16:28
created

getForeignColoniesInBroadcastRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 2
dl 0
loc 25
ccs 0
cts 17
cp 0
crap 2
rs 9.6666
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 Stu\Module\PlayerSetting\Lib\UserEnum;
10
use Stu\Orm\Entity\Colony;
11
use Stu\Orm\Entity\ColonyClass;
12
use Stu\Orm\Entity\ColonyInterface;
13
use Stu\Orm\Entity\Map;
14
use Stu\Orm\Entity\MapRegionSettlement;
15
use Stu\Orm\Entity\StarSystemMap;
16
use Stu\Orm\Entity\StarSystemMapInterface;
17
use Stu\Orm\Entity\UserInterface;
18
19
/**
20
 * @extends EntityRepository<Colony>
21
 */
22
final class ColonyRepository extends EntityRepository implements ColonyRepositoryInterface
23
{
24
    public function prototype(): ColonyInterface
25
    {
26
        return new Colony();
27
    }
28
29
    public function save(ColonyInterface $colony): void
30
    {
31
        $em = $this->getEntityManager();
32
33
        $em->persist($colony);
34
    }
35
36
    public function delete(ColonyInterface $colony): void
37
    {
38
        $em = $this->getEntityManager();
39
40
        $em->remove($colony);
41
        $em->flush();
42
    }
43
44
    public function getAmountByUser(UserInterface $user, int $colonyType): int
45
    {
46
        return (int) $this->getEntityManager()
47
            ->createQuery(
48
                sprintf(
49
                    'SELECT count(c.id) from %s c WHERE c.user_id = :userId AND c.colonies_classes_id IN (
50
                        SELECT cc.id FROM %s cc WHERE cc.type = :type
51
                    )',
52
                    Colony::class,
53
                    ColonyClass::class
54
                )
55
            )
56
            ->setParameters([
57
                'userId' => $user,
58
                'type' => $colonyType
59
            ])
60
            ->getSingleScalarResult();
61
    }
62
63
    public function getStartingByFaction(int $factionId): array
64
    {
65
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...actionId))->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...
66
            ->createQuery(
67
                sprintf(
68
                    'SELECT c FROM %s c INDEX BY c.id
69
                     JOIN %s sm
70
                     WITH c.starsystem_map_id = sm.id
71
                     WHERE c.user_id = :userId AND c.colonies_classes_id IN (
72
                        SELECT pt.id FROM %s pt WHERE pt.allow_start = :allowStart
73
                    ) AND sm.systems_id IN (
74
                        SELECT m.systems_id FROM %s m WHERE m.systems_id > 0 AND m.admin_region_id IN (
75
                            SELECT mrs.region_id from %s mrs WHERE mrs.faction_id = :factionId
76
                        )
77
                    )',
78
                    Colony::class,
79
                    StarSystemMap::class,
80
                    ColonyClass::class,
81
                    Map::class,
82
                    MapRegionSettlement::class
83
                )
84
            )
85
            ->setParameters([
86
                'allowStart' => 1,
87
                'userId' => UserEnum::USER_NOONE,
88
                'factionId' => $factionId
89
            ])
90
            ->getResult();
91
    }
92
93
    public function getByPosition(StarSystemMapInterface $sysmap): ?ColonyInterface
94
    {
95
        return $this->findOneBy([
96
            'starsystem_map_id' => $sysmap->getId()
97
        ]);
98
    }
99
100
    public function getForeignColoniesInBroadcastRange(
101
        StarSystemMapInterface $systemMap,
102
        UserInterface $user
103
    ): array {
104
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...>getSy()))->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...
105
            ->createQuery(
106
                sprintf(
107
                    'SELECT c FROM %s c
108
                     JOIN %s sm
109
                     WITH c.starsystem_map_id = sm.id
110
                     WHERE c.user_id NOT IN (:ignoreIds)
111
                     AND sm.systems_id = :systemId
112
                     AND sm.sx BETWEEN (:sx - 1) AND (:sx + 1)
113
                     AND sm.sy BETWEEN (:sy - 1) AND (:sy + 1)',
114
                    Colony::class,
115
                    StarSystemMap::class
116
                )
117
            )
118
            ->setParameters([
119
                'ignoreIds' => [$user->getId(), UserEnum::USER_NOONE],
120
                'systemId' => $systemMap->getSystem()->getId(),
121
                'sx' => $systemMap->getSx(),
122
                'sy' => $systemMap->getSy()
123
            ])
124
            ->getResult();
125
    }
126
127
    public function getByBatchGroup(int $batchGroup, int $batchGroupCount): iterable
128
    {
129
        return $this->getEntityManager()
130
            ->createQuery(
131
                sprintf(
132
                    'SELECT c FROM %s c
133
                    WHERE MOD(c.user_id, :groupCount) + 1 = :groupId
134
                    AND c.user_id != :userId',
135
                    Colony::class
136
                )
137
            )
138
            ->setParameters([
139
                'groupId' => $batchGroup,
140
                'groupCount' => $batchGroupCount,
141
                'userId' => UserEnum::USER_NOONE
142
            ])
143
            ->getResult();
144
    }
145
146
    public function getColonized(): iterable
147
    {
148
        return $this->getEntityManager()
149
            ->createQuery(
150
                sprintf(
151
                    'SELECT c FROM %s c WHERE c.user_id != :userId',
152
                    Colony::class
153
                )
154
            )
155
            ->setParameters([
156
                'userId' => UserEnum::USER_NOONE,
157
            ])
158
            ->getResult();
159
    }
160
161
    public function getColonyListForRenderFragment(UserInterface $user): array
162
    {
163
        $rsm = new ResultSetMapping();
164
        $rsm->addScalarResult('colonyid', 'colonyid', 'integer');
165
        $rsm->addScalarResult('classid', 'classid', 'integer');
166
        $rsm->addScalarResult('type', 'type', 'integer');
167
        $rsm->addScalarResult('nameandsector', 'nameandsector', 'string');
168
169
        return $this->getEntityManager()
170
            ->createNativeQuery(
171
                'SELECT c.id AS colonyid, cc.id AS classid, cc.type AS type,
172
                            concat(c.name, \' \', sm.sx, \'|\', sm.sy, \' (\', s.name, \'-\',
173
                                    CASE WHEN s.is_wormhole
174
                                        THEN \'Wurmloch\'
175
                                        ELSE \'System\'
176
                                    END, \')\') as nameandsector
177
                        FROM stu_colonies c
178
                        JOIN stu_colonies_classes cc
179
                            ON c.colonies_classes_id = cc.id
180
                        JOIN stu_sys_map sm
181
                            ON c.starsystem_map_id = sm.id
182
                        JOIN stu_systems s
183
                            ON sm.systems_id = s.id
184
                        WHERE c.user_id = :userId
185
                        ORDER BY cc.id ASC, c.id ASC',
186
                $rsm
187
            )
188
            ->setParameter('userId', $user->getId())
189
            ->getResult();
190
    }
191
192
    public function getColoniesNetWorth(): array
193
    {
194
        $rsm = new ResultSetMapping();
195
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
196
        $rsm->addScalarResult('commodity_id', 'commodity_id', 'integer');
197
        $rsm->addScalarResult('sum', 'sum', 'integer');
198
199
        return $this->getEntityManager()
200
            ->createNativeQuery(
201
                'SELECT u.id as user_id, bc.commodity_id as commodity_id, sum(bc.count) as sum
202
                FROM stu_user u
203
                JOIN stu_colonies c
204
                ON u.id = c.user_id 
205
                JOIN stu_colonies_fielddata cf
206
                ON cf.colonies_id = c.id
207
                JOIN stu_buildings_cost bc 
208
                ON cf.buildings_id = bc.buildings_id 
209
                WHERE u.id >= :firstUserId
210
                AND cf.buildings_id IS NOT NULL
211
                AND cf.aktiv = 1
212
                GROUP BY u.id, bc.commodity_id',
213
                $rsm
214
            )
215
            ->setParameters(['firstUserId' => UserEnum::USER_FIRST_ID])
216
            ->getResult();
217
    }
218
}
219