Passed
Pull Request — master (#2311)
by Nico
07:31 queued 02:09
created

NPCQuestRepository::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 Stu\Orm\Entity\NPCQuest;
9
10
/**
11
 * @extends EntityRepository<NPCQuest>
12
 */
13
final class NPCQuestRepository extends EntityRepository implements NPCQuestRepositoryInterface
14
{
15
    #[\Override]
16
    public function prototype(): NPCQuest
17
    {
18
        return new NPCQuest();
19
    }
20
21
    #[\Override]
22
    public function save(NPCQuest $quest): void
23
    {
24
        $em = $this->getEntityManager();
25
26
        $em->persist($quest);
27
    }
28
29
    #[\Override]
30
    public function delete(NPCQuest $quest): void
31
    {
32
        $em = $this->getEntityManager();
33
34
        $em->remove($quest);
35
        $em->flush();
36
    }
37
38
    #[\Override]
39
    public function getActiveQuests(): array
40
    {
41
        $time = time();
42
43
        return $this->getEntityManager()
44
            ->createQuery(
45
                sprintf(
46
                    'SELECT q FROM %s q
47
                    WHERE q.start <= :now
48
                    AND (q.end IS NULL OR q.end >= :now)
49
                    ORDER BY q.start DESC',
50
                    NPCQuest::class
51
                )
52
            )
53
            ->setParameters(['now' => $time])
54
            ->getResult();
55
    }
56
57
    #[\Override]
58
    public function getOpenForApplications(): array
59
    {
60
        $time = time();
61
62
        return $this->getEntityManager()
63
            ->createQuery(
64
                sprintf(
65
                    'SELECT q FROM %s q
66
                    WHERE q.start <= :now
67
                    AND q.application_end >= :now
68
                    AND (q.end IS NULL OR q.end >= :now)
69
                    ORDER BY q.application_end ASC',
70
                    NPCQuest::class
71
                )
72
            )
73
            ->setParameters(['now' => $time])
74
            ->getResult();
75
    }
76
77
    #[\Override]
78
    public function getByUser(int $userId): array
79
    {
80
        return $this->findBy(
81
            ['user_id' => $userId],
82
            ['id' => 'DESC']
83
        );
84
    }
85
86
    #[\Override]
87
    public function getByPlot(int $plotId): array
88
    {
89
        return $this->findBy(
90
            ['plot_id' => $plotId],
91
            ['id' => 'DESC']
92
        );
93
    }
94
95
    #[\Override]
96
    public function getFinishedQuests(): array
97
    {
98
        $time = time();
99
100
        return $this->getEntityManager()
101
            ->createQuery(
102
                sprintf(
103
                    'SELECT q FROM %s q
104
                    WHERE q.end IS NOT NULL
105
                    AND q.end < :now
106
                    ORDER BY q.end DESC',
107
                    NPCQuest::class
108
                )
109
            )
110
            ->setParameter('now', $time)
111
            ->getResult();
112
    }
113
114 1
    #[\Override]
115
    public function getActiveQuestsByUser(int $userId): array
116
    {
117 1
        return $this->getEntityManager()
118 1
            ->createQuery(
119 1
                sprintf(
120 1
                    'SELECT q FROM %s q
121
                    WHERE q.user_id = :userId
122
                    AND q.end IS NULL
123 1
                    ORDER BY q.id DESC',
124 1
                    NPCQuest::class
125 1
                )
126 1
            )
127 1
            ->setParameter('userId', $userId)
128 1
            ->getResult();
129
    }
130
131 1
    #[\Override]
132
    public function getFinishedQuestsByUser(int $userId): array
133
    {
134 1
        return $this->getEntityManager()
135 1
            ->createQuery(
136 1
                sprintf(
137 1
                    'SELECT q FROM %s q
138
                    WHERE q.user_id = :userId
139
                    AND q.end IS NOT NULL
140 1
                    ORDER BY q.id DESC',
141 1
                    NPCQuest::class
142 1
                )
143 1
            )
144 1
            ->setParameter('userId', $userId)
145 1
            ->getResult();
146
    }
147
148
    #[\Override]
149
    public function truncateByUser(int $userId): void
150
    {
151
        $this->getEntityManager()
152
            ->createQuery(
153
                sprintf(
154
                    'DELETE FROM %s q WHERE q.user_id = :userId',
155
                    NPCQuest::class
156
                )
157
            )
158
            ->setParameter('userId', $userId)
159
            ->execute();
160
    }
161
}
162