|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Exam; |
|
6
|
|
|
use App\Entity\Notification; |
|
7
|
|
|
use App\Entity\User; |
|
8
|
|
|
use DateTime; |
|
9
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
|
10
|
|
|
|
|
11
|
|
|
class NotificationRepository extends AbstractRepository implements NotificationRepositoryInterface { |
|
12
|
|
|
|
|
13
|
|
|
public function countUnreadForUser(User $user): int { |
|
14
|
|
|
$qb = $this->em->createQueryBuilder() |
|
15
|
|
|
->select('COUNT(n.id)') |
|
16
|
|
|
->from(Notification::class, 'n') |
|
17
|
|
|
->where('n.recipient = :user') |
|
18
|
|
|
->andWhere('n.isRead = false') |
|
19
|
|
|
->setParameter('user', $user->getId()); |
|
20
|
|
|
|
|
21
|
|
|
return $qb->getQuery()->getSingleScalarResult(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function findUnreadForUser(User $user): array { |
|
25
|
|
|
$qb = $this->em->createQueryBuilder() |
|
26
|
|
|
->select('n') |
|
27
|
|
|
->from(Notification::class, 'n') |
|
28
|
|
|
->where('n.recipient = :user') |
|
29
|
|
|
->andWhere('n.isRead = false') |
|
30
|
|
|
->setParameter('user', $user->getId()) |
|
31
|
|
|
->orderBy('n.createdAt', 'desc'); |
|
32
|
|
|
|
|
33
|
|
|
return $qb->getQuery()->getResult(); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getUserPaginator(User $user, int $itemsPerPage, int &$page): Paginator { |
|
37
|
|
|
$qb = $this->em->createQueryBuilder() |
|
38
|
|
|
->select('n') |
|
39
|
|
|
->from(Notification::class, 'n') |
|
40
|
|
|
->where('n.recipient = :user') |
|
41
|
|
|
->setParameter('user', $user->getId()) |
|
42
|
|
|
->orderBy('n.createdAt', 'desc'); |
|
43
|
|
|
|
|
44
|
|
|
if($page < 1) { |
|
45
|
|
|
$page = 1; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$offset = ($page - 1) * $itemsPerPage; |
|
49
|
|
|
$paginator = new Paginator($qb); |
|
50
|
|
|
$paginator->getQuery() |
|
51
|
|
|
->setMaxResults($itemsPerPage) |
|
52
|
|
|
->setFirstResult($offset); |
|
53
|
|
|
|
|
54
|
|
|
return $paginator; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function markAllReadForUser(User $user): int { |
|
58
|
|
|
return $this->em->createQueryBuilder() |
|
59
|
|
|
->update(Notification::class, 'n') |
|
60
|
|
|
->set('n.isRead', true) |
|
61
|
|
|
->where('n.recipient = :user') |
|
62
|
|
|
->setParameter('user', $user->getId()) |
|
63
|
|
|
->getQuery() |
|
64
|
|
|
->getSingleScalarResult(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function persist(Notification $notification): void { |
|
68
|
|
|
$this->em->persist($notification); |
|
69
|
|
|
$this->em->flush(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function remove(Notification $notification): void { |
|
73
|
|
|
$this->em->remove($notification); |
|
74
|
|
|
$this->em->flush(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function removeBetween(DateTime $start, DateTime $end): int { |
|
78
|
|
|
return $this->em->createQueryBuilder() |
|
|
|
|
|
|
79
|
|
|
->delete(Notification::class, 'n') |
|
80
|
|
|
->where('n.createdAt >= :start') |
|
81
|
|
|
->andWhere('n.createdAt <= :end') |
|
82
|
|
|
->setParameter('start', $start) |
|
83
|
|
|
->setParameter('end', $end) |
|
84
|
|
|
->getQuery() |
|
85
|
|
|
->execute(); |
|
86
|
|
|
} |
|
87
|
|
|
} |