Passed
Push — master ( 8387eb...5a11dc )
by Janko
07:59
created

DatabaseUserRepository::truncateAllEntries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 10
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 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\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...
11
use Stu\Orm\Entity\DatabaseCategory;
12
use Stu\Orm\Entity\DatabaseEntry;
13
use Stu\Orm\Entity\DatabaseUser;
14
15
/**
16
 * @extends EntityRepository<DatabaseUser>
17
 */
18
final class DatabaseUserRepository extends EntityRepository implements DatabaseUserRepositoryInterface
19
{
20
    #[Override]
21
    public function truncateByUserId(int $userId): void
22
    {
23
        $this->getEntityManager()->createQuery(
24
            sprintf(
25
                'delete from %s d where d.user_id = :userId',
26
                DatabaseUser::class
27
            )
28
        )
29
            ->setParameter('userId', $userId)
30
            ->execute();
31
    }
32
33 7
    #[Override]
34
    public function findFor(int $databaseEntryId, int $userId): ?DatabaseUser
35
    {
36 7
        return $this->findOneBy([
37 7
            'user_id' => $userId,
38 7
            'database_id' => $databaseEntryId,
39 7
        ]);
40
    }
41
42 3
    #[Override]
43
    public function exists(int $userId, int $databaseEntryId): bool
44
    {
45 3
        return $this->count([
46 3
            'user_id' => $userId,
47 3
            'database_id' => $databaseEntryId
48 3
        ]) > 0;
49
    }
50
51
    #[Override]
52
    public function prototype(): DatabaseUser
53
    {
54
        return new DatabaseUser();
55
    }
56
57
    #[Override]
58
    public function save(DatabaseUser $entry): void
59
    {
60
        $em = $this->getEntityManager();
61
        $em->persist($entry);
62
    }
63
64 1
    #[Override]
65
    public function getTopList(): array
66
    {
67 1
        $rsm = new ResultSetMapping();
68 1
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
69 1
        $rsm->addScalarResult('points', 'points', 'integer');
70 1
        $rsm->addScalarResult('timestamp', 'timestamp', 'integer');
71
72 1
        return $this->getEntityManager()
73 1
            ->createNativeQuery(
74 1
                'SELECT dbu.user_id, SUM(dbc.points) AS points,
75
                    (SELECT MAX(du.date)
76
                        FROM stu_database_user du
77
                        WHERE du.user_id = dbu.user_id) AS timestamp
78
                FROM stu_database_user dbu
79
                LEFT JOIN stu_database_entrys dbe
80
                    ON dbe.id = dbu.database_id
81
                LEFT JOIN stu_database_categories dbc
82
                    ON dbc.id = dbe.category_id
83
                WHERE dbu.user_id > :firstUserId
84
                GROUP BY dbu.user_id
85
                ORDER BY points DESC, timestamp ASC
86 1
                LIMIT 10',
87 1
                $rsm
88 1
            )
89 1
            ->setParameter('firstUserId', UserEnum::USER_FIRST_ID)
90 1
            ->getArrayResult();
91
    }
92
93 1
    #[Override]
94
    public function getCountForUser(int $userId): int
95
    {
96 1
        return (int) $this->getEntityManager()
97 1
            ->createQuery(
98 1
                sprintf(
99 1
                    'SELECT SUM(dbc.points) FROM %s dbc JOIN
100
                    %s dbe WITH dbe.category_id = dbc.id JOIN %s dbu WITH
101
                    dbu.database_id = dbe.id
102 1
                    WHERE dbu.user_id = :userId',
103 1
                    DatabaseCategory::class,
104 1
                    DatabaseEntry::class,
105 1
                    DatabaseUser::class
106 1
                )
107 1
            )
108 1
            ->setParameters([
109 1
                'userId' => $userId
110 1
            ])
111 1
            ->getSingleScalarResult();
112
    }
113
114
    #[Override]
115
    public function hasUserCompletedCategoryAndLayer(int $userId, int $categoryId, ?int $ignoredDatabaseEntryId = null, ?int $layerId = null): bool
116
    {
117
        $layerCondition = $layerId === null ? 'de.layer_id IS NULL' : 'de.layer_id = :layerId';
118
119
        $query = $this->getEntityManager()
120
            ->createQuery(
121
                sprintf(
122
                    'SELECT count(de.id)
123
                FROM %s de
124
                WHERE de.category_id = :categoryId
125
                AND de.id != :ignoredDatabaseEntryId
126
                AND %s
127
                AND NOT EXISTS
128
                    (SELECT du.id
129
                    FROM %s du
130
                    WHERE du.database_id = de.id
131
                    AND du.user_id = :userId)',
132
                    DatabaseEntry::class,
133
                    $layerCondition,
134
                    DatabaseUser::class
135
                )
136
            );
137
138
        $parameters = [
139
            'userId' => $userId,
140
            'categoryId' => $categoryId,
141
            'ignoredDatabaseEntryId' => $ignoredDatabaseEntryId ?? 0,
142
        ];
143
144
        if ($layerId !== null) {
145
            $parameters['layerId'] = $layerId;
146
        }
147
148
        return $query->setParameters($parameters)->getSingleScalarResult() == 0;
149
    }
150
}
151