Passed
Pull Request — master (#2178)
by Nico
24:52 queued 14:43
created

KnPostArchivRepository::getByUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
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 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...
9
use Stu\Orm\Entity\KnPostArchiv;
10
use Stu\Orm\Entity\RpgPlotArchiv;
11
12
/**
13
 * @extends EntityRepository<KnPostArchiv>
14
 */
15
final class KnPostArchivRepository extends EntityRepository implements KnPostArchivRepositoryInterface
16
{
17
    #[Override]
18
    public function prototype(): KnPostArchiv
19
    {
20
        return new KnPostArchiv();
21
    }
22
23
    #[Override]
24
    public function save(KnPostArchiv $post): void
25
    {
26
        $em = $this->getEntityManager();
27
28
        $em->persist($post);
29
        $em->flush();
30
    }
31
32
    #[Override]
33
    public function delete(KnPostArchiv $post): void
34
    {
35
        $em = $this->getEntityManager();
36
37
        $em->remove($post);
38
    }
39
40
    #[Override]
41
    public function getBy(int $offset, int $limit): array
42
    {
43
        return $this->findBy(
44
            [],
45
            ['date' => 'desc'],
46
            $limit,
47
            $offset
48
        );
49
    }
50
51
    #[Override]
52
    public function getByUser(int $userId): array
53
    {
54
        return $this->findBy(
55
            ['user_id' => $userId],
56
            ['id' => 'desc']
57
        );
58
    }
59
60
    #[Override]
61
    public function getByPlot(RpgPlotArchiv $plot, ?int $offset, ?int $limit): array
62
    {
63
        return $this->findBy(
64
            ['plot_id' => $plot],
65
            ['date' => 'desc'],
66
            $limit,
67
            $offset
68
        );
69
    }
70
71 1
    #[Override]
72
    public function getByVersion(string $version, int $offset, int $limit): array
73
    {
74 1
        return $this->findBy(
75 1
            ['version' => $version],
76 1
            ['date' => 'desc'],
77 1
            $limit,
78 1
            $offset
79 1
        );
80
    }
81
82
    #[Override]
83
    public function getByVersionWithPlots(string $version, int $offset, int $limit): array
84
    {
85
        return $this->createQueryBuilder('kpa')
86
            ->where('kpa.version = :version')
87
            ->setParameter('version', $version)
88
            ->orderBy('kpa.date', 'DESC')
89
            ->setFirstResult($offset)
90
            ->setMaxResults($limit)
91
            ->getQuery()
92
            ->getResult();
93
    }
94
95
    /**
96
     * @param array<int> $plotIds
97
     * @return array<int, RpgPlotArchiv>
98
     */
99
    #[Override]
100
    public function getPlotsByIds(array $plotIds): array
101
    {
102
        if (empty($plotIds)) {
103
            return [];
104
        }
105
106
        $plots = $this->getEntityManager()
107
            ->getRepository(RpgPlotArchiv::class)
108
            ->createQueryBuilder('rpa')
109
            ->where('rpa.former_id IN (:plotIds)')
110
            ->setParameter('plotIds', $plotIds)
111
            ->getQuery()
112
            ->getResult();
113
114
        $result = [];
115
        foreach ($plots as $plot) {
116
            $result[$plot->getFormerId()] = $plot;
117
        }
118
119
        return $result;
120
    }
121
122
    #[Override]
123
    public function getAvailableVersions(): array
124
    {
125
        $result = $this->createQueryBuilder('kpa')
126
            ->select('DISTINCT kpa.version')
127
            ->orderBy('kpa.version', 'DESC')
128
            ->getQuery()
129
            ->getScalarResult();
130
131
        return array_column($result, 'version');
132
    }
133
134
    #[Override]
135
    public function getAmount(): int
136
    {
137
        return $this->count([]);
138
    }
139
140
    #[Override]
141
    public function getAmountByPlot(int $plotId): int
142
    {
143
        return $this->count([
144
            'plot_id' => $plotId
145
        ]);
146
    }
147
148 1
    #[Override]
149
    public function getAmountByVersion(string $version): int
150
    {
151 1
        return $this->count(['version' => $version]);
152
    }
153
154
    #[Override]
155
    public function getAmountSince(int $postId): int
156
    {
157
        return (int) $this->getEntityManager()
158
            ->createQuery(
159
                sprintf(
160
                    'SELECT COUNT(p.id) FROM %s p WHERE p.id > :postId',
161
                    KnPostArchiv::class
162
                )
163
            )
164
            ->setParameters(['postId' => $postId])
165
            ->getSingleScalarResult();
166
    }
167
168
    #[Override]
169
    public function getNewerThenMark(int $mark): array
170
    {
171
        return $this->getEntityManager()
172
            ->createQuery(
173
                sprintf(
174
                    'SELECT p FROM %s p WHERE p.id > :postId ORDER BY p.id ASC',
175
                    KnPostArchiv::class
176
                )
177
            )
178
            ->setMaxResults(3)
179
            ->setParameters(['postId' => $mark])
180
            ->getResult();
181
    }
182
183
    #[Override]
184
    public function searchByContent(string $content): array
185
    {
186
        return $this->getEntityManager()
187
            ->createQuery(
188
                sprintf(
189
                    'SELECT p FROM %s p
190
                    WHERE UPPER(p.text) like UPPER(:content) OR UPPER(p.titel) like UPPER(:content)
191
                    ORDER BY p.id DESC',
192
                    KnPostArchiv::class
193
                )
194
            )
195
            ->setParameters(['content' => sprintf('%%%s%%', $content)])
196
            ->getResult();
197
    }
198
199
    #[Override]
200
    public function truncateAllEntities(): void
201
    {
202
        $this->getEntityManager()->createQuery(
203
            sprintf(
204
                'DELETE FROM %s kp',
205
                KnPostArchiv::class
206
            )
207
        )->execute();
208
    }
209
210 1
    #[Override]
211
    public function findByFormerId(int $formerId): ?KnPostArchiv
212
    {
213 1
        return $this->findOneBy(['former_id' => $formerId]);
214
    }
215
216
    #[Override]
217
    public function getByPlotFormerId(int $plotFormerId, ?int $offset, ?int $limit): array
218
    {
219
        return $this->findBy(
220
            ['plot_id' => $plotFormerId],
221
            ['date' => 'desc'],
222
            $limit,
223
            $offset
224
        );
225
    }
226
227
    #[Override]
228
    public function getByUserAndVersion(int $userId, string $version): array
229
    {
230
        return $this->findBy(
231
            [
232
                'user_id' => $userId,
233
                'version' => $version
234
            ],
235
            ['date' => 'desc']
236
        );
237
    }
238
239
    #[Override]
240
    public function findByFormerIdAndVersion(int $formerId, string $version): ?KnPostArchiv
241
    {
242
        return $this->findOneBy([
243
            'former_id' => $formerId,
244
            'version' => $version
245
        ]);
246
    }
247
248
    #[Override]
249
    public function searchByContentAndVersion(string $content, string $version): array
250
    {
251
        return $this->getEntityManager()
252
            ->createQuery(
253
                sprintf(
254
                    'SELECT p FROM %s p
255
                    WHERE (UPPER(p.text) like UPPER(:content) OR UPPER(p.titel) like UPPER(:content))
256
                    AND p.version = :version
257
                    ORDER BY p.date DESC',
258
                    KnPostArchiv::class
259
                )
260
            )
261
            ->setParameters([
262
                'content' => sprintf('%%%s%%', $content),
263
                'version' => $version
264
            ])
265
            ->getResult();
266
    }
267
}
268