Passed
Pull Request — master (#2194)
by Janko
27:48 queued 20:18
created

KnPostRepository::truncateAllEntities()   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\KnPost;
12
use Stu\Orm\Entity\RpgPlot;
13
use Stu\Orm\Entity\User;
14
15
/**
16
 * @extends EntityRepository<KnPost>
17
 */
18
final class KnPostRepository extends EntityRepository implements KnPostRepositoryInterface
19
{
20
    #[Override]
21
    public function prototype(): KnPost
22
    {
23
        return new KnPost();
24
    }
25
26
    #[Override]
27
    public function save(KnPost $post): void
28
    {
29
        $em = $this->getEntityManager();
30
31
        $em->persist($post);
32
        $em->flush();
33
    }
34
35
    #[Override]
36
    public function delete(KnPost $post): void
37
    {
38
        $em = $this->getEntityManager();
39
40
        $em->remove($post);
41
    }
42
43
    #[Override]
44
    public function getBy(int $offset, int $limit): array
45
    {
46
        return $this->findBy(
47
            ['deleted' => null],
48
            ['date' => 'desc'],
49
            $limit,
50
            $offset
51
        );
52
    }
53
54
    #[Override]
55
    public function getByUser(int $userId): array
56
    {
57
        return $this->findBy(
58
            ['user_id' => $userId, 'deleted' => null],
59
            ['date' => 'desc']
60
        );
61
    }
62
63 1
    #[Override]
64
    public function getByPlot(RpgPlot $plot, ?int $offset, ?int $limit): array
65
    {
66 1
        return $this->findBy(
67 1
            ['plot_id' => $plot, 'deleted' => null],
68 1
            ['date' => 'desc'],
69 1
            $limit,
70 1
            $offset
71 1
        );
72
    }
73
74
    #[Override]
75
    public function getAmount(): int
76
    {
77
        return $this->count(['deleted' => null]);
78
    }
79
80 1
    #[Override]
81
    public function getAmountByPlot(int $plotId): int
82
    {
83 1
        return $this->count([
84 1
            'plot_id' => $plotId,
85 1
            'deleted' => null
86 1
        ]);
87
    }
88
89 2
    #[Override]
90
    public function getAmountSince(int $postId): int
91
    {
92 2
        return (int) $this->getEntityManager()
93 2
            ->createQuery(
94 2
                sprintf(
95 2
                    'SELECT COUNT(p.id) FROM %s p WHERE p.id > :postId AND p.deleted IS NULL',
96 2
                    KnPost::class
97 2
                )
98 2
            )
99 2
            ->setParameters(['postId' => $postId])
100 2
            ->getSingleScalarResult();
101
    }
102
103 2
    #[Override]
104
    public function getNewerThenMark(int $mark): array
105
    {
106 2
        return $this->getEntityManager()
107 2
            ->createQuery(
108 2
                sprintf(
109 2
                    'SELECT p FROM %s p WHERE p.id > :postId AND p.deleted IS NULL ORDER BY p.id ASC',
110 2
                    KnPost::class
111 2
                )
112 2
            )
113 2
            ->setMaxResults(3)
114 2
            ->setParameters(['postId' => $mark])
115 2
            ->getResult();
116
    }
117
118
    #[Override]
119
    public function searchByContent(string $content): array
120
    {
121
        return $this->getEntityManager()
122
            ->createQuery(
123
                sprintf(
124
                    'SELECT p FROM %s p
125
                    WHERE UPPER(p.text) like UPPER(:content) OR UPPER(p.titel) like UPPER(:content) 
126
                    AND p.deleted IS NULL
127
                    ORDER BY p.id DESC',
128
                    KnPost::class
129
                )
130
            )
131
            ->setParameters(['content' => sprintf('%%%s%%', $content)])
132
            ->getResult();
133
    }
134
135
    #[Override]
136
    public function getRpgVotesTop10(): array
137
    {
138
        $rsm = new ResultSetMapping();
139
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
140
        $rsm->addScalarResult('votes', 'votes', 'integer');
141
142
        return $this->getEntityManager()->createNativeQuery(
143
            "SELECT kn.user_id, SUM(value::int) AS votes
144
            FROM stu_kn kn
145
            CROSS JOIN LATERAL json_each_text(kn.ratings)
146
            WHERE kn.user_id >= :firstUserId
147
            AND kn.ratings #>> '{}' != '[]'
148
            AND kn.deleted IS NULL
149
            GROUP BY kn.user_id
150
            ORDER BY votes DESC
151
            LIMIT 10",
152
            $rsm
153
        )
154
            ->setParameter('firstUserId', UserEnum::USER_FIRST_ID)
155
            ->getResult();
156
    }
157
158
    #[Override]
159
    public function getRpgVotesOfUser(User $user): ?int
160
    {
161
        $rsm = new ResultSetMapping();
162
        $rsm->addScalarResult('votes', 'votes', 'integer');
163
164
        $result = $this->getEntityManager()->createNativeQuery(
165
            "SELECT SUM(value::int) as votes
166
            FROM stu_kn kn
167
            CROSS JOIN LATERAL json_each_text(kn.ratings)
168
            WHERE kn.user_id = :userId
169
            AND kn.ratings #>> '{}' != '[]'
170
            AND kn.deleted IS NULL",
171
            $rsm
172
        )
173
            ->setParameter('userId', $user->getId())
174
            ->getSingleScalarResult();
175
176
        if ($result === null) {
177
            return null;
178
        }
179
180
        return (int) $result;
181
    }
182
183 1
    #[Override]
184
    public function findActiveById(int $id): ?KnPost
185
    {
186 1
        return $this->findOneBy([
187 1
            'id' => $id,
188 1
            'deleted' => null
189 1
        ]);
190
    }
191
}
192