Passed
Push — master ( b559d8...779d9f )
by Nico
10:31
created

KnPostRepository::searchByContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 2
rs 9.9666
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 Stu\Module\PlayerSetting\Lib\UserEnum;
10
use Stu\Orm\Entity\KnPost;
11
use Stu\Orm\Entity\KnPostInterface;
12
use Stu\Orm\Entity\RpgPlotInterface;
13
use Stu\Orm\Entity\UserInterface;
14
15
/**
16
 * @extends EntityRepository<KnPost>
17
 */
18
final class KnPostRepository extends EntityRepository implements KnPostRepositoryInterface
19
{
20
    public function prototype(): KnPostInterface
21
    {
22
        return new KnPost();
23
    }
24
25
    public function save(KnPostInterface $post): void
26
    {
27
        $em = $this->getEntityManager();
28
29
        $em->persist($post);
30
        $em->flush();
31
    }
32
33
    public function delete(KnPostInterface $post): void
34
    {
35
        $em = $this->getEntityManager();
36
37
        $em->remove($post);
38
    }
39
40
    public function getBy(int $offset, int $limit): array
41
    {
42
        return $this->findBy(
43
            [],
44
            ['date' => 'desc'],
45
            $limit,
46
            $offset
47
        );
48
    }
49
50
    public function getByUser(int $userId): array
51
    {
52
        return $this->findBy(
53
            ['user_id' => $userId],
54
            ['id' => 'desc']
55
        );
56
    }
57
58
    public function getByPlot(RpgPlotInterface $plot, ?int $offset, ?int $limit): array
59
    {
60
        return $this->findBy(
61
            ['plot_id' => $plot],
62
            ['date' => 'desc'],
63
            $limit,
64
            $offset
65
        );
66
    }
67
68
    public function getAmount(): int
69
    {
70
        return $this->count([]);
71
    }
72
73
    public function getAmountByPlot(int $plotId): int
74
    {
75
        return $this->count([
76
            'plot_id' => $plotId
77
        ]);
78
    }
79
80
    public function getAmountSince(int $postId): int
81
    {
82
        return (int) $this->getEntityManager()
83
            ->createQuery(
84
                sprintf(
85
                    'SELECT COUNT(p.id) FROM %s p WHERE p.id > :postId',
86
                    KnPost::class
87
                )
88
            )
89
            ->setParameters(['postId' => $postId])
90
            ->getSingleScalarResult();
91
    }
92
93
    public function getNewerThenMark(int $mark): array
94
    {
95
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...=> $mark))->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
96
            ->createQuery(
97
                sprintf(
98
                    'SELECT p FROM %s p WHERE p.id > :postId ORDER BY p.id ASC',
99
                    KnPost::class
100
                )
101
            )
102
            ->setMaxResults(3)
103
            ->setParameters(['postId' => $mark])
104
            ->getResult();
105
    }
106
107
    public function searchByContent(string $content): array
108
    {
109
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...content)))->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
110
            ->createQuery(
111
                sprintf(
112
                    'SELECT p FROM %s p
113
                    WHERE UPPER(p.text) like UPPER(:content) OR UPPER(p.titel) like UPPER(:content)
114
                    ORDER BY p.id DESC',
115
                    KnPost::class
116
                )
117
            )
118
            ->setParameters(['content' => sprintf('%%%s%%', $content)])
119
            ->getResult();
120
    }
121
122
    public function truncateAllEntities(): void
123
    {
124
        $this->getEntityManager()->createQuery(
125
            sprintf(
126
                'DELETE FROM %s kp',
127
                KnPost::class
128
            )
129
        )->execute();
130
    }
131
132
    public function getRpgVotesTop10(): array
133
    {
134
        $rsm = new ResultSetMapping();
135
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
136
        $rsm->addScalarResult('votes', 'votes', 'integer');
137
138
        return $this->getEntityManager()->createNativeQuery(
139
            'SELECT kn.user_id, SUM(value::float) AS votes
140
            FROM stu_kn kn
141
            CROSS JOIN LATERAL json_each_text(kn.ratings)
142
            WHERE kn.user_id >= :firstUserId
143
            GROUP BY kn.user_id
144
            ORDER BY votes DESC
145
            LIMIT 10',
146
            $rsm
147
        )
148
            ->setParameter('firstUserId', UserEnum::USER_FIRST_ID)
149
            ->getResult();
150
    }
151
152
    public function getRpgVotesOfUser(UserInterface $user): ?int
153
    {
154
        $rsm = new ResultSetMapping();
155
        $rsm->addScalarResult('votes', 'votes', 'integer');
156
157
        $result = $this->getEntityManager()->createNativeQuery(
158
            'SELECT SUM(value::int) as votes
159
            FROM stu_kn kn
160
            CROSS JOIN LATERAL json_each_text(kn.ratings)
161
            WHERE kn.user_id = :userId',
162
            $rsm
163
        )
164
            ->setParameter('userId', $user->getId())
165
            ->getSingleScalarResult();
166
167
        if ($result === null) {
168
            return null;
169
        }
170
171
        return (int) $result;
172
    }
173
}
174