Test Failed
Push — dev ( d9ea74...46b33e )
by Nico
09:52
created

RpgPlotArchivRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getActiveByUser() 0 14 1
A getOrderedList() 0 3 1
A save() 0 5 1
A delete() 0 5 1
A getByFoundingUser() 0 4 1
A getEmptyOldPlots() 0 17 1
A truncateAllEntities() 0 8 1
A prototype() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Stu\Orm\Entity\KnPostArchiv;
9
use Stu\Orm\Entity\RpgPlotArchiv;
10
use Stu\Orm\Entity\RpgPlotArchivInterface;
11
use Stu\Orm\Entity\RpgPlotMemberArchiv;
12
13
/**
14
 * @extends EntityRepository<RpgPlotArchiv>
15
 */
16
final class RpgPlotArchivRepository extends EntityRepository implements RpgPlotArchivRepositoryInterface
17
{
18
    public function getByFoundingUser(int $userId): array
19
    {
20
        return $this->findBy([
21
            'user_id' => $userId,
22
        ]);
23
    }
24
25
    public function prototype(): RpgPlotArchivInterface
26
    {
27
        return new RpgPlotArchivInterface();
28
    }
29
30
    public function save(RpgPlotArchivInterface $rpgPlot): void
31
    {
32
        $em = $this->getEntityManager();
33
34
        $em->persist($rpgPlot);
35
    }
36
37
    public function delete(RpgPlotArchivInterface $rpgPlot): void
38
    {
39
        $em = $this->getEntityManager();
40
41
        $em->remove($rpgPlot);
42
    }
43
44
    public function getActiveByUser(int $userId): array
45
    {
46
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM... $userId))->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...
47
            ->createQuery(
48
                sprintf(
49
                    'SELECT p FROM %s p WHERE p.end_date IS NULL AND p.id IN (
50
                        SELECT pm.plot_id FROM %s pm WHERE pm.user_id = :userId
51
                    ) ORDER BY p.start_date DESC',
52
                    RpgPlotArchiv::class,
53
                    RpgPlotMemberArchiv::class
54
                )
55
            )
56
            ->setParameters(['userId' => $userId])
57
            ->getResult();
58
    }
59
60
    public function getEmptyOldPlots(int $maxAge): array
61
    {
62
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM... $maxAge))->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...
63
            ->createQuery(
64
                sprintf(
65
                    'SELECT pl
66
                    FROM %s pl
67
                    WHERE NOT EXISTS (SELECT kn.id
68
                                    FROM %s kn
69
                                    WHERE kn.plot_id = pl.id)
70
                    AND pl.start_date < :deletionThreshold',
71
                    RpgPlotArchiv::class,
72
                    KnPostArchiv::class
73
                )
74
            )
75
            ->setParameters(['deletionThreshold' => time() - $maxAge])
76
            ->getResult();
77
    }
78
79
    public function getOrderedList(): array
80
    {
81
        return $this->findBy([], ['start_date' => 'asc']);
82
    }
83
84
    public function truncateAllEntities(): void
85
    {
86
        $this->getEntityManager()->createQuery(
87
            sprintf(
88
                'DELETE FROM %s rp',
89
                RpgPlotArchiv::class
90
            )
91
        )->execute();
92
    }
93
}
94