Passed
Push — dev ( 48a242...d91619 )
by Nico
17:39
created

KnPostRepository::findActiveById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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