Passed
Push — dev ( ad4f42...691af8 )
by Nico
14:11
created

RpgPlotArchivRepository::getByVersion()   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
use Stu\Orm\Entity\RpgPlotMemberArchiv;
12
13
/**
14
 * @extends EntityRepository<RpgPlotArchiv>
15
 */
16
final class RpgPlotArchivRepository extends EntityRepository implements RpgPlotArchivRepositoryInterface
17
{
18
    #[Override]
19
    public function getByFoundingUser(int $userId): array
20
    {
21
        return $this->findBy([
22
            'user_id' => $userId,
23
        ]);
24
    }
25
26
    #[Override]
27
    public function prototype(): RpgPlotArchiv
28
    {
29
        return new RpgPlotArchiv();
30
    }
31
32
    #[Override]
33
    public function save(RpgPlotArchiv $rpgPlot): void
34
    {
35
        $em = $this->getEntityManager();
36
37
        $em->persist($rpgPlot);
38
    }
39
40
    #[Override]
41
    public function delete(RpgPlotArchiv $rpgPlot): void
42
    {
43
        $em = $this->getEntityManager();
44
45
        $em->remove($rpgPlot);
46
    }
47
48
    #[Override]
49
    public function getActiveByUser(int $userId): array
50
    {
51
        return $this->getEntityManager()
52
            ->createQuery(
53
                sprintf(
54
                    'SELECT p FROM %s p WHERE p.end_date IS NULL AND p.id IN (
55
                        SELECT pm.plot_id FROM %s pm WHERE pm.user_id = :userId
56
                    ) ORDER BY p.start_date DESC',
57
                    RpgPlotArchiv::class,
58
                    RpgPlotMemberArchiv::class
59
                )
60
            )
61
            ->setParameters(['userId' => $userId])
62
            ->getResult();
63
    }
64
65
    #[Override]
66
    public function getEmptyOldPlots(int $maxAge): array
67
    {
68
        return $this->getEntityManager()
69
            ->createQuery(
70
                sprintf(
71
                    'SELECT pl
72
                    FROM %s pl
73
                    WHERE NOT EXISTS (SELECT kn.id
74
                                    FROM %s kn
75
                                    WHERE kn.plot_id = pl.id)
76
                    AND pl.start_date < :deletionThreshold',
77
                    RpgPlotArchiv::class,
78
                    KnPostArchiv::class
79
                )
80
            )
81
            ->setParameters(['deletionThreshold' => time() - $maxAge])
82
            ->getResult();
83
    }
84
85
    #[Override]
86
    public function getOrderedList(): array
87
    {
88
        return $this->findBy([], ['start_date' => 'asc']);
89
    }
90
91
    #[Override]
92
    public function truncateAllEntities(): void
93
    {
94
        $this->getEntityManager()->createQuery(
95
            sprintf(
96
                'DELETE FROM %s rp',
97
                RpgPlotArchiv::class
98
            )
99
        )->execute();
100
    }
101
102
    #[Override]
103
    public function getAvailableVersions(): array
104
    {
105
        $result = $this->createQueryBuilder('rpa')
106
            ->select('DISTINCT rpa.version')
107
            ->orderBy('rpa.version', 'DESC')
108
            ->getQuery()
109
            ->getScalarResult();
110
111
        return array_column($result, 'version');
112
    }
113
114
    #[Override]
115
    public function getByVersion(string $version): array
116
    {
117
        return $this->findBy(
118
            ['version' => $version],
119
            ['start_date' => 'desc']
120
        );
121
    }
122
123
    #[Override]
124
    public function getOrderedListByVersion(string $version): array
125
    {
126
        return $this->createQueryBuilder('rpa')
127
            ->where('rpa.version = :version')
128
            ->setParameter('version', $version)
129
            ->orderBy('rpa.start_date', 'DESC')
130
            ->getQuery()
131
            ->getResult();
132
    }
133
134
    #[Override]
135
    public function findByFormerId(int $formerId): ?RpgPlotArchiv
136
    {
137
        return $this->findOneBy(['former_id' => $formerId]);
138
    }
139
}
140