|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
7
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent; |
|
8
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User; |
|
9
|
|
|
|
|
10
|
|
|
class SubscriptionEventRepository extends EntityRepository |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @return int |
|
14
|
|
|
*/ |
|
15
|
4 |
|
public function getLastDayEventsCount(): int |
|
16
|
|
|
{ |
|
17
|
4 |
|
$qb = $this->createQueryBuilder('se'); |
|
18
|
|
|
|
|
19
|
4 |
|
$now = new \DateTime(); |
|
20
|
|
|
|
|
21
|
|
|
return $qb |
|
22
|
4 |
|
->select('COUNT(se)') |
|
23
|
4 |
|
->where('se.date > :time') |
|
24
|
4 |
|
->setParameter('time', $now->sub(new \DateInterval('PT24H'))) |
|
25
|
4 |
|
->getQuery()->getSingleScalarResult() |
|
26
|
|
|
; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Creates QueryBuilder object for pagination of user subscribers events |
|
31
|
|
|
* |
|
32
|
|
|
* @param User $user |
|
33
|
|
|
* |
|
34
|
|
|
* @return QueryBuilder |
|
35
|
|
|
*/ |
|
36
|
3 |
View Code Duplication |
public function createUserLastSubscribersEventsQuery(User $user): QueryBuilder |
|
37
|
|
|
{ |
|
38
|
3 |
|
$qb = $this->createQueryBuilder('se'); |
|
39
|
|
|
|
|
40
|
|
|
return $qb |
|
41
|
3 |
|
->select(['se', 's']) |
|
42
|
3 |
|
->join('se.subscriber', 's') |
|
43
|
3 |
|
->where('se.author = :author') |
|
44
|
3 |
|
->orderBy('se.date', 'desc') |
|
45
|
3 |
|
->setParameter('author', $user->getId()) |
|
46
|
|
|
; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get last user subscriber events |
|
51
|
|
|
* |
|
52
|
|
|
* @param User $user |
|
53
|
|
|
* @param int $limit |
|
54
|
|
|
* |
|
55
|
|
|
* @return SubscriptionEvent[] |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getUserLastSubscribersEvents(User $user, int $limit = 20): array |
|
58
|
|
|
{ |
|
59
|
|
|
$qb = $this->createUserLastSubscribersEventsQuery($user); |
|
60
|
|
|
$qb->setMaxResults($limit); |
|
61
|
|
|
|
|
62
|
|
|
return $qb->getQuery()->getResult(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get last global subscriptions QueryBuilder for pagination |
|
67
|
|
|
* |
|
68
|
|
|
* @return QueryBuilder |
|
69
|
|
|
*/ |
|
70
|
2 |
View Code Duplication |
public function createLastSubscriptionEventsQuery(): QueryBuilder |
|
71
|
|
|
{ |
|
72
|
2 |
|
$qb = $this->createQueryBuilder('se'); |
|
73
|
|
|
|
|
74
|
|
|
return $qb |
|
75
|
2 |
|
->select(['se', 'a', 's']) |
|
76
|
2 |
|
->innerJoin('se.author', 'a') |
|
77
|
2 |
|
->innerJoin('se.subscriber', 's') |
|
78
|
2 |
|
->orderBy('se.date', 'desc') |
|
79
|
|
|
; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get last global subscription events |
|
84
|
|
|
* |
|
85
|
|
|
* @param int $limit |
|
86
|
|
|
* |
|
87
|
|
|
* @return SubscriptionEvent[] |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getLastSubscriptionEvents(int $limit = 20): array |
|
90
|
|
|
{ |
|
91
|
|
|
$qb = $this->createLastSubscriptionEventsQuery(); |
|
92
|
|
|
$qb->setMaxResults($limit); |
|
93
|
|
|
|
|
94
|
|
|
return $qb->getQuery()->getResult(); |
|
95
|
|
|
} |
|
96
|
|
|
} |