|
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
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return SubscriptionEvent[] |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getLastEventsByDay(int $days = 30): array |
|
101
|
|
|
{ |
|
102
|
|
|
$qb = $this->createQueryBuilder('se'); |
|
103
|
|
|
|
|
104
|
|
|
$rows = $qb |
|
105
|
|
|
->select([ |
|
106
|
|
|
'NEW Skobkin\Bundle\PointToolsBundle\DTO\DailyEvents(DAY(se.date), COUNT(se))', |
|
107
|
|
|
'DAY(se.date) as day', |
|
108
|
|
|
]) |
|
109
|
|
|
->groupBy('day') |
|
110
|
|
|
->orderBy('day', 'DESC') |
|
111
|
|
|
->setMaxResults($days) |
|
112
|
|
|
->getQuery()->getResult() |
|
113
|
|
|
; |
|
114
|
|
|
|
|
115
|
|
|
$result = []; |
|
116
|
|
|
|
|
117
|
|
|
// Removing unnecessary element, saving DTO |
|
118
|
|
|
// @todo remove crutches, refactor query |
|
119
|
|
|
foreach ($rows as $row) { |
|
120
|
|
|
unset($row['day']); |
|
121
|
|
|
$result[] = reset($row); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $result; |
|
125
|
|
|
} |
|
126
|
|
|
} |