Passed
Push — dev ( b2109c...60db09 )
by Janko
10:33
created

StationRepository::getStationOnLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 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...
10
use Stu\Component\Game\TimeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\TimeConstants 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\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...
12
use Stu\Component\Spacecraft\System\SpacecraftSystemModeEnum;
13
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
14
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...
15
use Stu\Module\Station\Lib\TStationItem;
16
use Stu\Orm\Entity\Crew;
17
use Stu\Orm\Entity\Location;
18
use Stu\Orm\Entity\Map;
19
use Stu\Orm\Entity\MapInterface;
20
use Stu\Orm\Entity\CrewAssignment;
21
use Stu\Orm\Entity\LocationInterface;
22
use Stu\Orm\Entity\PirateWrath;
23
use Stu\Orm\Entity\ShipInterface;
24
use Stu\Orm\Entity\SpacecraftRump;
25
use Stu\Orm\Entity\SpacecraftRumpInterface;
26
use Stu\Orm\Entity\Spacecraft;
27
use Stu\Orm\Entity\SpacecraftInterface;
28
use Stu\Orm\Entity\SpacecraftSystem;
29
use Stu\Orm\Entity\StarSystemMap;
30
use Stu\Orm\Entity\StarSystemMapInterface;
31
use Stu\Orm\Entity\Station;
32
use Stu\Orm\Entity\StationInterface;
33
use Stu\Orm\Entity\TradePost;
34
use Stu\Orm\Entity\User;
35
use Stu\Orm\Entity\UserInterface;
36
use Stu\Orm\Proxy\__CG__\Stu\Orm\Entity\ShipRump;
0 ignored issues
show
Bug introduced by
The type Stu\Orm\Proxy\__CG__\Stu\Orm\Entity\ShipRump 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...
37
38
/**
39
 * @extends EntityRepository<Station>
40
 */
41
final class StationRepository extends EntityRepository implements StationRepositoryInterface
42
{
43
    #[Override]
44
    public function prototype(): StationInterface
45
    {
46
        return new Station();
47
    }
48
49
    #[Override]
50
    public function save(StationInterface $station): void
51
    {
52
        $em = $this->getEntityManager();
53
54
        $em->persist($station);
55
    }
56
57
    #[Override]
58
    public function delete(StationInterface $station): void
59
    {
60
        $em = $this->getEntityManager();
61
62
        $em->remove($station);
63
    }
64
65 1
    #[Override]
66
    public function getByUser(int $userId): array
67
    {
68 1
        return $this->findBy(
69 1
            [
70 1
                'user_id' => $userId
71 1
            ],
72 1
            ['max_huelle' => 'desc', 'id' => 'asc']
73 1
        );
74
    }
75
76
    #[Override]
77
    public function getForeignStationsInBroadcastRange(SpacecraftInterface $spacecraft): array
78
    {
79
        $layer = $spacecraft->getLayer();
80
        $systemMap = $spacecraft->getStarsystemMap();
81
        $map = $spacecraft->getMap();
82
83
        return $this->getEntityManager()
84
            ->createQuery(
85
                sprintf(
86
                    'SELECT st FROM %s st
87
                     JOIN %s s
88
                     WITH st.id = s.id
89
                     LEFT JOIN %s m
90
                     WITH s.location_id = m.id
91
                     LEFT JOIN %s l
92
                     WITH m.id = l.id
93
                     LEFT JOIN %s sm
94
                     WITH s.location_id = sm.id
95
                     WHERE s.user_id NOT IN (:ignoreIds)
96
                     AND (:layerId = 0 OR (l.layer_id = :layerId
97
                        AND l.cx BETWEEN (:cx - 1) AND (:cx + 1)
98
                        AND l.cy BETWEEN (:cy - 1) AND (:cy + 1)))
99
                     AND (:systemId = 0 OR (sm.systems_id = :systemId
100
                        AND sm.sx BETWEEN (:sx - 1) AND (:sx + 1)
101
                        AND sm.sy BETWEEN (:sy - 1) AND (:sy + 1)))',
102
                    Station::class,
103
                    Spacecraft::class,
104
                    Map::class,
105
                    Location::class,
106
                    StarSystemMap::class
107
                )
108
            )
109
            ->setParameters([
110
                'ignoreIds' => [$spacecraft->getUser()->getId(), UserEnum::USER_NOONE],
111
                'systemId' => $systemMap === null ? 0 : $systemMap->getSystem()->getId(),
112
                'sx' => $systemMap === null ? 0 : $systemMap->getSx(),
113
                'sy' => $systemMap === null ? 0 : $systemMap->getSy(),
114
                'layerId' => ($systemMap !== null || $layer === null) ? 0 : $layer->getId(),
115
                'cx' => ($systemMap !== null || $map === null) ? 0 : $map->getCx(),
116
                'cy' => ($systemMap !== null || $map === null) ? 0 : $map->getCy()
117
            ])
118
            ->getResult();
119
    }
120
121
    #[Override]
122
    public function getTradePostsWithoutDatabaseEntry(): array
123
    {
124
        return $this->getEntityManager()->createQuery(
125
            sprintf(
126
                'SELECT s FROM %s s
127
                JOIN %s tp
128
                WITH s.tradePost = tp
129
                WHERE s.database_id is null',
130
                Station::class,
131
                TradePost::class
132
            )
133
        )->getResult();
134
    }
135
136 1
    #[Override]
137
    public function getByUplink(int $userId): array
138
    {
139 1
        return $this->getEntityManager()->createQuery(
140 1
            sprintf(
141 1
                'SELECT s FROM %s s
142
                JOIN %s sp
143
                WITH s.id = sp.id
144
                JOIN %s sc
145
                WITH s.id = sc.spacecraft_id
146
                JOIN %s c
147
                WITH sc.crew_id = c.id
148
                JOIN %s ss
149
                WITH ss.spacecraft_id = s.id
150
                JOIN %s u
151
                WITH sp.user_id = u.id
152
                WHERE sp.user_id != :userId
153
                AND c.user_id = :userId
154
                AND ss.system_type = :systemType
155
                AND ss.mode >= :mode
156 1
                AND (u.vac_active = :false OR u.vac_request_date > :vacationThreshold)',
157 1
                Station::class,
158 1
                Spacecraft::class,
159 1
                CrewAssignment::class,
160 1
                Crew::class,
161 1
                SpacecraftSystem::class,
162 1
                User::class
163 1
            )
164 1
        )->setParameters([
165 1
            'userId' => $userId,
166 1
            'systemType' => SpacecraftSystemTypeEnum::UPLINK->value,
167 1
            'mode' => SpacecraftSystemModeEnum::MODE_ON->value,
168 1
            'vacationThreshold' => time() - UserEnum::VACATION_DELAY_IN_SECONDS,
169 1
            'false' => false
170 1
        ])
171 1
            ->getResult();
172
    }
173
174
    #[Override]
175
    public function getStationConstructions(): array
176
    {
177
        return $this->getEntityManager()->createQuery(
178
            sprintf(
179
                'SELECT s FROM %s s
180
                JOIN %s r
181
                WITH s.rump_id = r.id
182
                WHERE s.user_id > :firstUserId
183
                AND r.category_id = :catId',
184
                Spacecraft::class,
185
                SpacecraftRump::class
186
            )
187
        )->setParameters([
188
            'catId' => SpacecraftRumpEnum::SHIP_CATEGORY_CONSTRUCTION,
189
            'firstUserId' => UserEnum::USER_FIRST_ID
190
        ])
191
            ->getResult();
192
    }
193
194 1
    #[Override]
195
    public function getStationScannerResults(
196
        SpacecraftInterface $spacecraft,
197
        bool $showCloaked = false,
198
        MapInterface|StarSystemMapInterface|null $field = null
199
    ): array {
200
201 1
        $rsm = new ResultSetMapping();
202 1
        $rsm->addEntityResult(TStationItem::class, 's');
203 1
        TStationItem::addTSpacecraftItemFields($rsm);
204
205 1
        $location = $field ?? $spacecraft->getLocation();
206
207 1
        $query = $this->getEntityManager()->createNativeQuery(
208 1
            sprintf(
209 1
                'SELECT s.id as shipid, s.rump_id as rumpid , ss.mode as warpstate,
210
                    COALESCE(ss2.mode,0) as cloakstate, ss3.mode as shieldstate, COALESCE(ss4.status,0) as uplinkstate,
211
                    s.type as spacecrafttype, s.name as shipname, s.huelle as hull, s.max_huelle as maxhull,
212
                    s.schilde as shield, s.holding_web_id as webid, tw.finished_time as webfinishtime, u.id as userid, u.username,
213
                    r.category_id as rumpcategoryid, r.name as rumpname, r.role_id as rumproleid,
214
                    (SELECT count(*) > 0 FROM stu_ship_log sl WHERE sl.spacecraft_id = s.id AND sl.is_private = :false) as haslogbook,
215
                    (SELECT count(*) > 0 FROM stu_crew_assign ca WHERE ca.spacecraft_id = s.id) as hascrew
216
                FROM stu_spacecraft s
217
                JOIN stu_station st
218
                ON s.id = st.id
219
                LEFT JOIN stu_spacecraft_system ss
220
                ON s.id = ss.spacecraft_id
221
                AND ss.system_type = :warpdriveType
222
                LEFT JOIN stu_spacecraft_system ss2
223
                ON s.id = ss2.spacecraft_id
224
                AND ss2.system_type = :cloakType
225
                LEFT JOIN stu_spacecraft_system ss3
226
                ON s.id = ss3.spacecraft_id
227
                AND ss3.system_type = :shieldType
228
                LEFT JOIN stu_spacecraft_system ss4
229
                ON s.id = ss4.spacecraft_id
230
                AND ss4.system_type = :uplinkType
231
                JOIN stu_rump r
232
                ON s.rump_id = r.id
233
                LEFT OUTER JOIN stu_tholian_web tw
234
                ON s.holding_web_id = tw.id
235
                JOIN stu_user u
236
                ON s.user_id = u.id
237
                WHERE s.location_id = :locationId
238
                AND s.id != :ignoreId
239
                %s
240 1
                ORDER BY r.category_id ASC, r.role_id ASC, r.id ASC, s.name ASC',
241 1
                $showCloaked ? '' : sprintf(' AND (s.user_id = %d OR COALESCE(ss2.mode,0) < %d) ', $spacecraft->getUser()->getId(), SpacecraftSystemModeEnum::MODE_ON->value)
242 1
            ),
243 1
            $rsm
244 1
        )->setParameters([
245 1
            'locationId' => $location->getId(),
0 ignored issues
show
Bug introduced by
The method getId() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

245
            'locationId' => $location->/** @scrutinizer ignore-call */ getId(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
246 1
            'ignoreId' => $spacecraft->getId(),
247 1
            'cloakType' => SpacecraftSystemTypeEnum::CLOAK->value,
248 1
            'warpdriveType' => SpacecraftSystemTypeEnum::WARPDRIVE->value,
249 1
            'shieldType' => SpacecraftSystemTypeEnum::SHIELDS->value,
250 1
            'uplinkType' => SpacecraftSystemTypeEnum::UPLINK->value,
251 1
            'false' => false
252 1
        ]);
253
254 1
        return $query->getResult();
255
    }
256
257 3
    #[Override]
258
    public function getStationOnLocation(LocationInterface $location): ?StationInterface
259
    {
260 3
        return $this->findOneBy(['location' => $location]);
261
    }
262
263
    #[Override]
264
    public function getStationsByUser(int $userId): array
265
    {
266
        return $this->getEntityManager()
267
            ->createQuery(
268
                sprintf(
269
                    'SELECT s
270
                    FROM %s s
271
                    JOIN %s r
272
                    WITH s.rump_id = r.id
273
                    WHERE s.user_id = :userId
274
                    AND r.category_id = :categoryId',
275
                    Station::class,
276
                    SpacecraftRump::class
277
                )
278
            )
279
            ->setParameters([
280
                'userId' => $userId,
281
                'categoryId' => SpacecraftRumpEnum::SHIP_CATEGORY_STATION
282
            ])
283
            ->getResult();
284
    }
285
286
    #[Override]
287
    public function getPiratePhalanxTargets(ShipInterface $pirateShip): array
288
    {
289
        $layer = $pirateShip->getLayer();
290
        if ($layer === null) {
291
            return [];
292
        }
293
294
        $location = $pirateShip->getLocation();
295
        $range = $pirateShip->getSensorRange() * 2;
296
297
        return $this->getEntityManager()->createQuery(
298
            sprintf(
299
                'SELECT s FROM %s s
300
                JOIN %s r WITH s.rump = r
301
                JOIN %s l WITH s.location = l
302
                JOIN %s u WITH s.user = u
303
                LEFT JOIN %s w WITH u = w.user
304
                WHERE r.role_id = :phalanxRoleId
305
                AND l.layer_id = :layerId
306
                AND l.cx BETWEEN :minX AND :maxX
307
                AND l.cy BETWEEN :minY AND :maxY
308
                AND u.id >= :firstUserId
309
                AND u.state >= :stateActive
310
                AND u.creation < :eightWeeksEarlier
311
                AND (u.vac_active = :false OR u.vac_request_date > :vacationThreshold)
312
                AND COALESCE(w.protection_timeout, 0) < :currentTime',
313
                Station::class,
314
                SpacecraftRump::class,
315
                Location::class,
316
                User::class,
317
                PirateWrath::class
318
            )
319
        )
320
            ->setParameters([
321
                'phalanxRoleId' => SpacecraftRumpEnum::SHIP_ROLE_SENSOR,
322
                'minX' => $location->getCx() - $range,
323
                'maxX' => $location->getCx() + $range,
324
                'minY' => $location->getCy() - $range,
325
                'maxY' => $location->getCy() + $range,
326
                'layerId' => $layer->getId(),
327
                'firstUserId' => UserEnum::USER_FIRST_ID,
328
                'stateActive' => UserEnum::USER_STATE_ACTIVE,
329
                'eightWeeksEarlier' => time() - TimeConstants::EIGHT_WEEKS_IN_SECONDS,
330
                'vacationThreshold' => time() - UserEnum::VACATION_DELAY_IN_SECONDS,
331
                'currentTime' => time(),
332
                'false' => false
333
            ])
334
            ->getResult();
335
    }
336
337 1
    #[Override]
338
    public function getByUserAndRump(UserInterface $user, SpacecraftRumpInterface $rump): array
339
    {
340 1
        return $this->findBy([
341 1
            'user_id' => $user->getId(),
342 1
            'rump_id' => $rump->getId()
343 1
        ], [
344 1
            'location_id' => 'asc'
345 1
        ]);
346
    }
347
}
348