Passed
Push — master ( 0956f0...2da600 )
by Nico
15:29 queued 09:54
created

UserRepository   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 427
Duplicated Lines 0 %

Test Coverage

Coverage 71.86%

Importance

Changes 0
Metric Value
eloc 270
dl 0
loc 427
ccs 189
cts 263
cp 0.7186
rs 10
c 0
b 0
f 0
wmc 27

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getByAlliance() 0 9 1
A getByLogin() 0 16 1
A delete() 0 6 1
A getByResetToken() 0 16 1
A getByEmail() 0 16 1
A getNPCAdminList() 0 23 2
A prototype() 0 7 1
A getByMobile() 0 17 1
A getList() 0 23 2
A getActiveAmountRecentlyOnline() 0 12 1
A getNpcList() 0 12 1
A getOrderedByLastaction() 0 31 1
A getActiveAmount() 0 9 1
A getFriendsByUserAndAlliance() 0 17 1
A getNonNpcList() 0 9 1
A getInactiveAmount() 0 15 1
A getIdleRegistrations() 0 19 1
A save() 0 6 1
A getDeleteable() 0 32 1
A getVacationAmount() 0 17 1
A getFallbackUser() 0 10 2
A getNonNpcListbyFaction() 0 15 1
A getUsersWithActiveLicense() 0 42 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
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...
9
use Stu\Exception\FallbackUserDoesNotExistException;
10
use Stu\Module\Message\Lib\ContactListModeEnum;
11
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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\Module\PlayerSetting\Lib\UserSettingEnum;
13
use Stu\Module\PlayerSetting\Lib\UserStateEnum;
14
use Stu\Orm\Entity\Alliance;
15
use Stu\Orm\Entity\Contact;
16
use Stu\Orm\Entity\User;
17
use Stu\Orm\Entity\UserRegistration;
18
use Stu\Orm\Entity\UserSetting;
19
use Stu\Orm\Entity\TradeLicense;
20
21
/**
22
 * @extends EntityRepository<User>
23
 */
24
final class UserRepository extends EntityRepository implements UserRepositoryInterface
25
{
26
    #[\Override]
27
    public function prototype(): User
28
    {
29
        $user = new User();
30
        $user->setRegistration(new UserRegistration($user));
31
32
        return $user;
33
    }
34
35 5
    #[\Override]
36
    public function save(User $post): void
37
    {
38 5
        $em = $this->getEntityManager();
39
40 5
        $em->persist($post);
41
    }
42
43
    #[\Override]
44
    public function delete(User $post): void
45
    {
46
        $em = $this->getEntityManager();
47
48
        $em->remove($post);
49
    }
50
51 1
    #[\Override]
52
    public function getByResetToken(string $resetToken): ?User
53
    {
54 1
        return $this->getEntityManager()
55 1
            ->createQuery(
56 1
                sprintf(
57 1
                    'SELECT u FROM %s u
58
                    JOIN %s ur
59
                    WITH u = ur.user
60 1
                    WHERE ur.password_token = :token',
61 1
                    User::class,
62 1
                    UserRegistration::class
63 1
                )
64 1
            )
65 1
            ->setParameter('token', $resetToken)
66 1
            ->getOneOrNullResult();
67
    }
68
69 1
    #[\Override]
70
    public function getDeleteable(
71
        int $idleTimeThreshold,
72
        int $idleTimeVacationThreshold,
73
        array $ignoreIds
74
    ): array {
75 1
        return $this->getEntityManager()->createQuery(
76 1
            sprintf(
77 1
                'SELECT u FROM %s u INDEX BY u.id
78
                JOIN %s ur
79
                WITH u = ur.user
80
                 WHERE u.id > :firstUserId
81
                 AND u.id NOT IN (:ignoreIds)
82
                 AND ur.delmark != :deletionForbidden
83
                 AND (ur.delmark = :deletionMark
84
                        OR (u.vac_active = :false AND u.lastaction > 0 AND u.lastaction < :idleTimeThreshold)
85
                        OR (u.vac_active = :true AND u.lastaction > 0 AND u.lastaction < :idleTimeVacationThreshold)
86
                    )
87 1
                 ORDER BY u.id ASC',
88 1
                User::class,
89 1
                UserRegistration::class
90 1
            )
91 1
        )->setParameters([
92 1
            'idleTimeThreshold' => $idleTimeThreshold,
93 1
            'idleTimeVacationThreshold' => $idleTimeVacationThreshold,
94 1
            'ignoreIds' => $ignoreIds,
95 1
            'deletionMark' => UserConstants::DELETION_CONFIRMED,
96 1
            'deletionForbidden' => UserConstants::DELETION_FORBIDDEN,
97 1
            'firstUserId' => UserConstants::USER_FIRST_ID,
98 1
            'false' => false,
99 1
            'true' => true
100 1
        ])->getResult();
101
    }
102
103 1
    #[\Override]
104
    public function getIdleRegistrations(
105
        int $idleTimeThreshold
106
    ): array {
107 1
        return $this->getEntityManager()->createQuery(
108 1
            sprintf(
109 1
                'SELECT u FROM %s u INDEX BY u.id
110
                JOIN %s ur
111
                WITH u = ur.user
112
                 WHERE (u.state = :newUser OR u.state = :accountVerification)
113 1
                 AND ur.creation < :idleTimeThreshold',
114 1
                User::class,
115 1
                UserRegistration::class
116 1
            )
117 1
        )->setParameters([
118 1
            'idleTimeThreshold' => $idleTimeThreshold,
119 1
            'newUser' => UserStateEnum::NEW->value,
120 1
            'accountVerification' => UserStateEnum::ACCOUNT_VERIFICATION->value
121 1
        ])->getResult();
122
    }
123
124
    #[\Override]
125
    public function getByEmail(string $email): ?User
126
    {
127
        return $this->getEntityManager()
128
            ->createQuery(
129
                sprintf(
130
                    'SELECT u FROM %s u
131
                    JOIN %s ur
132
                    WITH u = ur.user
133
                    WHERE ur.email = :email',
134
                    User::class,
135
                    UserRegistration::class
136
                )
137
            )
138
            ->setParameter('email', $email)
139
            ->getOneOrNullResult();
140
    }
141
142
    #[\Override]
143
    public function getByMobile(string $mobile, string $mobileHash): ?User
144
    {
145
        return $this->getEntityManager()->createQuery(
146
            sprintf(
147
                'SELECT u FROM %s u
148
                    JOIN %s ur
149
                    WITH u = ur.user
150
                    WHERE ur.mobile = :mobile
151
                    OR ur.mobile = :mobileHash',
152
                User::class,
153
                UserRegistration::class
154
            )
155
        )->setParameters([
156
            'mobile' => $mobile,
157
            'mobileHash' => $mobileHash
158
        ])->getOneOrNullResult();
159
    }
160
161
    #[\Override]
162
    public function getByLogin(string $loginName): ?User
163
    {
164
        return $this->getEntityManager()
165
            ->createQuery(
166
                sprintf(
167
                    'SELECT u FROM %s u
168
                        JOIN %s ur
169
                        WITH u = ur.user
170
                        WHERE ur.login = :login',
171
                    User::class,
172
                    UserRegistration::class
173
                )
174
            )
175
            ->setParameter('login', $loginName)
176
            ->getOneOrNullResult();
177
    }
178
179 1
    #[\Override]
180
    public function getByAlliance(Alliance $alliance): array
181
    {
182 1
        return $this->findBy(
183 1
            [
184 1
                'alliance' => $alliance
185 1
            ],
186 1
            [
187 1
                'id' => 'ASC'
188 1
            ]
189 1
        );
190
    }
191
192 1
    #[\Override]
193
    public function getList(
194
        string $sortField,
195
        string $sortOrder,
196
        ?int $limit,
197
        int $offset
198
    ): array {
199 1
        $query = $this->getEntityManager()
200 1
            ->createQuery(
201 1
                sprintf(
202 1
                    'SELECT u FROM %s u WHERE u.id >= :firstUserId ORDER BY u.%s %s',
203 1
                    User::class,
204 1
                    $sortField,
205 1
                    $sortOrder
206 1
                )
207 1
            )
208 1
            ->setFirstResult($offset);
209
210 1
        if ($limit !== null) {
211 1
            $query->setMaxResults($limit);
212
        }
213
214 1
        return $query->setParameter('firstUserId', UserConstants::USER_FIRST_ID)->getResult();
215
    }
216
217 1
    #[\Override]
218
    public function getNPCAdminList(
219
        string $sortField,
220
        string $sortOrder,
221
        ?int $limit,
222
        int $offset
223
    ): array {
224 1
        $query = $this->getEntityManager()
225 1
            ->createQuery(
226 1
                sprintf(
227 1
                    'SELECT u FROM %s u WHERE u.id IN (10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 101, 102) ORDER BY u.%s %s',
228 1
                    User::class,
229 1
                    $sortField,
230 1
                    $sortOrder
231 1
                )
232 1
            )
233 1
            ->setFirstResult($offset);
234
235 1
        if ($limit !== null) {
236 1
            $query->setMaxResults($limit);
237
        }
238
239 1
        return $query->getResult();
240
    }
241
242 1
    #[\Override]
243
    public function getFriendsByUserAndAlliance(User $user, ?Alliance $alliance): array
244
    {
245 1
        return $this->getEntityManager()->createQuery(
246 1
            sprintf(
247 1
                'SELECT u FROM %s u WHERE u.id IN (
248
                    SELECT cl.user_id FROM %s cl WHERE cl.mode = :mode AND cl.recipient = :userId
249
                ) OR (u.alliance IS NOT NULL AND u.alliance = :alliance) AND u.id != :userId
250 1
                ORDER BY u.id',
251 1
                User::class,
252 1
                Contact::class
253 1
            )
254 1
        )->setParameters([
255 1
            'mode' => ContactListModeEnum::FRIEND->value,
256 1
            'userId' => $user->getId(),
257 1
            'alliance' => $alliance
258 1
        ])->getResult();
259
    }
260
261 2
    #[\Override]
262
    public function getOrderedByLastaction(int $limit, int $ignoreUserId, int $lastActionThreshold): array
263
    {
264 2
        return $this->getEntityManager()->createQuery(
265 2
            sprintf(
266 2
                'SELECT u FROM %s u
267
                WHERE u.id != :ignoreUserId
268
                AND u.id > :firstUserId
269
                AND (EXISTS (SELECT us
270
                            FROM %s us
271
                            WHERE us.user = u
272
                            AND us.setting = :showOnlineStateSetting
273
                            AND us.value = :showOnlineState)
274
                    OR u.id IN (
275
                        SELECT cl.user_id FROM %s cl WHERE cl.mode = :contactListModeFriend AND cl.recipient = :ignoreUserId
276
                    )
277 2
                ) AND u.lastaction > :lastActionThreshold',
278 2
                User::class,
279 2
                UserSetting::class,
280 2
                Contact::class
281 2
            )
282 2
        )->setParameters([
283 2
            'ignoreUserId' => $ignoreUserId,
284 2
            'contactListModeFriend' => ContactListModeEnum::FRIEND->value,
285 2
            'lastActionThreshold' => $lastActionThreshold,
286 2
            'showOnlineStateSetting' => UserSettingEnum::SHOW_ONLINE_STATUS->value,
287 2
            'showOnlineState' => 1,
288 2
            'firstUserId' => UserConstants::USER_FIRST_ID
289 2
        ])
290 2
            ->setMaxResults($limit)
291 2
            ->getResult();
292
    }
293
294 223
    #[\Override]
295
    public function getActiveAmount(): int
296
    {
297 223
        return (int) $this->getEntityManager()->createQuery(
298 223
            sprintf(
299 223
                'SELECT COUNT(u.id) FROM %s u WHERE u.id >= :firstUserId',
300 223
                User::class
301 223
            )
302 223
        )->setParameter('firstUserId', UserConstants::USER_FIRST_ID)->getSingleScalarResult();
303
    }
304
305 1
    #[\Override]
306
    public function getInactiveAmount(int $days): int
307
    {
308 1
        return (int) $this->getEntityManager()->createQuery(
309 1
            sprintf(
310 1
                'SELECT COUNT(u.id) FROM %s u
311
                WHERE u.id >= :firstUserId
312 1
                AND u.lastaction < :threshold',
313 1
                User::class
314 1
            )
315 1
        )->setParameters([
316 1
            'threshold' => time() - $days * TimeConstants::ONE_DAY_IN_SECONDS,
317 1
            'firstUserId' => UserConstants::USER_FIRST_ID
318 1
        ])
319 1
            ->getSingleScalarResult();
320
    }
321
322 1
    #[\Override]
323
    public function getVacationAmount(): int
324
    {
325 1
        return (int) $this->getEntityManager()->createQuery(
326 1
            sprintf(
327 1
                'SELECT COUNT(u.id) FROM %s u
328
                WHERE u.id >= :firstUserId
329
                AND u.vac_active = :true
330 1
                AND u.vac_request_date < :vacThreshold',
331 1
                User::class
332 1
            )
333 1
        )->setParameters([
334 1
            'vacThreshold' => time() - UserConstants::VACATION_DELAY_IN_SECONDS,
335 1
            'firstUserId' => UserConstants::USER_FIRST_ID,
336 1
            'true' => true
337 1
        ])
338 1
            ->getSingleScalarResult();
339
    }
340
341 223
    #[\Override]
342
    public function getActiveAmountRecentlyOnline(int $threshold): int
343
    {
344 223
        return (int) $this->getEntityManager()->createQuery(
345 223
            sprintf(
346 223
                'SELECT COUNT(u.id) FROM %s u WHERE u.id >= :firstUserId AND u.lastaction > :threshold',
347 223
                User::class
348 223
            )
349 223
        )->setParameters([
350 223
            'threshold' => $threshold,
351 223
            'firstUserId' => UserConstants::USER_FIRST_ID
352 223
        ])->getSingleScalarResult();
353
    }
354
355 2
    #[\Override]
356
    public function getNpcList(): array
357
    {
358 2
        return $this->getEntityManager()->createQuery(
359 2
            sprintf(
360 2
                'SELECT u FROM %s u
361
                WHERE u.id BETWEEN 10 AND :firstUserId - 1
362 2
                ORDER BY u.id',
363 2
                User::class
364 2
            )
365 2
        )->setParameter('firstUserId', UserConstants::USER_FIRST_ID)
366 2
            ->getResult();
367
    }
368
369 5
    #[\Override]
370
    public function getNonNpcList(): array
371
    {
372 5
        return $this->getEntityManager()->createQuery(
373 5
            sprintf(
374 5
                'SELECT u FROM %s u WHERE u.id >= :firstUserId ORDER BY u.id ASC',
375 5
                User::class
376 5
            )
377 5
        )->setParameter('firstUserId', UserConstants::USER_FIRST_ID)->getResult();
378
    }
379
380 1
    #[\Override]
381
382
    public function getNonNpcListbyFaction(int $factionid): array
383
    {
384 1
        return $this->getEntityManager()->createQuery(
385 1
            sprintf(
386 1
                'SELECT u FROM %s u WHERE u.id >= :firstUserId AND u.faction_id = :factionid ORDER BY u.id ASC',
387 1
                User::class
388 1
            )
389 1
        )->setParameters(
390 1
            [
391 1
                'firstUserId' => UserConstants::USER_FIRST_ID,
392 1
                'factionid' => $factionid
393 1
            ]
394 1
        )->getResult();
395
    }
396
397 4
    #[\Override]
398
    public function getFallbackUser(): User
399
    {
400 4
        $user = $this->find(UserConstants::USER_NOONE);
401
402 4
        if ($user === null) {
403
            throw new FallbackUserDoesNotExistException(sprintf('the user with id %d does not exist', UserConstants::USER_NOONE));
404
        }
405
406 4
        return $user;
407
    }
408
409
    #[\Override]
410
    public function getUsersWithActiveLicense(int $tradePostId, int $currentTime, ?int $factionId = null): array
411
    {
412
        $qb = $this->getEntityManager()->createQuery(
413
            sprintf(
414
                'SELECT u FROM %s u
415
                JOIN %s tl WITH tl.user = u
416
                WHERE tl.posts_id = :tradePostId
417
                AND tl.expired >= :currentTime
418
                AND u.id >= :firstUserId',
419
                User::class,
420
                TradeLicense::class
421
            )
422
        )
423
            ->setParameters([
424
                'tradePostId' => $tradePostId,
425
                'currentTime' => $currentTime,
426
                'firstUserId' => UserConstants::USER_FIRST_ID
427
            ]);
428
429
        if ($factionId !== null) {
430
            $qb = $this->getEntityManager()->createQuery(
431
                sprintf(
432
                    'SELECT u FROM %s u
433
                    JOIN %s tl WITH tl.user = u
434
                    WHERE tl.posts_id = :tradePostId
435
                    AND tl.expired >= :currentTime
436
                    AND u.faction_id = :factionId
437
                    AND u.id >= :firstUserId',
438
                    User::class,
439
                    TradeLicense::class
440
                )
441
            )
442
                ->setParameters([
443
                    'tradePostId' => $tradePostId,
444
                    'currentTime' => $currentTime,
445
                    'factionId' => $factionId,
446
                    'firstUserId' => UserConstants::USER_FIRST_ID
447
                ]);
448
        }
449
450
        return $qb->getResult();
451
    }
452
}
453