|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\RegistrationCode; |
|
6
|
|
|
use App\Entity\User; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
9
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
|
10
|
|
|
|
|
11
|
|
|
class RegistrationCodeRepository implements RegistrationCodeRepositoryInterface { |
|
12
|
|
|
|
|
13
|
|
|
private $em; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(EntityManagerInterface $em) { |
|
16
|
|
|
$this->em = $em; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
private function createDefaultQueryBuilder(): QueryBuilder { |
|
20
|
|
|
return $this->em->createQueryBuilder() |
|
21
|
|
|
->select(['c', 'u']) |
|
22
|
|
|
->from(RegistrationCode::class, 'c') |
|
23
|
|
|
->leftJoin('c.student', 'u'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function findOneByCode(string $code): ?RegistrationCode { |
|
27
|
|
|
return $this->createDefaultQueryBuilder() |
|
|
|
|
|
|
28
|
|
|
->where('c.code = :code') |
|
29
|
|
|
->setParameter('code', $code) |
|
30
|
|
|
->setMaxResults(1) |
|
31
|
|
|
->getQuery() |
|
32
|
|
|
->getOneOrNullResult(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function findAll() { |
|
36
|
|
|
return $this->createDefaultQueryBuilder() |
|
37
|
|
|
->getQuery() |
|
38
|
|
|
->getResult(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function persist(RegistrationCode $code): void { |
|
42
|
|
|
$this->em->persist($code); |
|
43
|
|
|
$this->em->flush(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function remove(RegistrationCode $code): void { |
|
47
|
|
|
$this->em->remove($code); |
|
48
|
|
|
$this->em->flush();; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function beginTransaction() { |
|
52
|
|
|
$this->em->beginTransaction(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function commit() { |
|
56
|
|
|
$this->em->commit(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function rollBack() { |
|
60
|
|
|
$this->em->rollback();; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getPaginatedUsers(int $itemsPerPage, int &$page, ?string $query = null, ?string $grade = null): Paginator { |
|
64
|
|
|
$qb = $this->createDefaultQueryBuilder(); |
|
65
|
|
|
|
|
66
|
|
|
if($query !== null) { |
|
67
|
|
|
$qb->andWhere($qb->expr()->like('c.code', ':query')) |
|
68
|
|
|
->setParameter('query', '%' . $query . '%'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if($grade !== null) { |
|
72
|
|
|
$qb->andWhere('u.grade = :grade') |
|
73
|
|
|
->setParameter('grade', $grade); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if(!is_numeric($page) || $page < 1) { |
|
|
|
|
|
|
77
|
|
|
$page = 1; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$offset = ($page - 1) * $itemsPerPage; |
|
81
|
|
|
|
|
82
|
|
|
$paginator = new Paginator($qb); |
|
83
|
|
|
$paginator->getQuery() |
|
84
|
|
|
->setMaxResults($itemsPerPage) |
|
85
|
|
|
->setFirstResult($offset); |
|
86
|
|
|
|
|
87
|
|
|
return $paginator; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritDoc |
|
92
|
|
|
*/ |
|
93
|
|
|
public function findAllUuids(): array { |
|
94
|
|
|
$qb = $this->em |
|
95
|
|
|
->createQueryBuilder() |
|
96
|
|
|
->select('u.uuid') |
|
97
|
|
|
->from(RegistrationCode::class, 'u'); |
|
98
|
|
|
|
|
99
|
|
|
return array_map(function(array $item) { |
|
100
|
|
|
return $item['uuid']; |
|
101
|
|
|
}, $qb->getQuery()->getScalarResult()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function removeRedeemed(): void { |
|
105
|
|
|
$qb = $this->em->createQueryBuilder(); |
|
106
|
|
|
$qb |
|
107
|
|
|
->delete(RegistrationCode::class, 'r') |
|
108
|
|
|
->where($qb->expr()->isNotNull('r.redeemingUser')) |
|
109
|
|
|
->getQuery() |
|
110
|
|
|
->execute(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function findByGrade(string $grade): array { |
|
114
|
|
|
return $this->em->createQueryBuilder() |
|
|
|
|
|
|
115
|
|
|
->select(['c', 's']) |
|
116
|
|
|
->from(RegistrationCode::class, 'c') |
|
117
|
|
|
->leftJoin('c.student', 's') |
|
118
|
|
|
->where('s.grade = :grade') |
|
119
|
|
|
->setParameter('grade', $grade) |
|
120
|
|
|
->getQuery() |
|
121
|
|
|
->getResult(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritDoc |
|
126
|
|
|
*/ |
|
127
|
|
|
public function findAllByStudent(User $user): array { |
|
128
|
|
|
return $this->em->createQueryBuilder() |
|
|
|
|
|
|
129
|
|
|
->select(['c', 's']) |
|
130
|
|
|
->from(RegistrationCode::class, 'c') |
|
131
|
|
|
->leftJoin('c.student', 's') |
|
132
|
|
|
->where('s.id = :student') |
|
133
|
|
|
->setParameter('student', $user->getId()) |
|
134
|
|
|
->getQuery() |
|
135
|
|
|
->getResult(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @inheritDoc |
|
141
|
|
|
*/ |
|
142
|
|
|
public function codeForStudentExists(User $user): bool { |
|
143
|
|
|
return $this->em->createQueryBuilder() |
|
144
|
|
|
->select('COUNT(c.id)') |
|
145
|
|
|
->from(RegistrationCode::class, 'c') |
|
146
|
|
|
->leftJoin('c.student', 's') |
|
147
|
|
|
->where('s.id = :student') |
|
148
|
|
|
->setParameter('student', $user->getId()) |
|
149
|
|
|
->getQuery() |
|
150
|
|
|
->getSingleScalarResult() > 0; |
|
151
|
|
|
} |
|
152
|
|
|
} |