1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Azine\MailgunWebhooksBundle\Entity\Repositories; |
4
|
|
|
|
5
|
|
|
use Azine\MailgunWebhooksBundle\Entity\MailgunEvent; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use Doctrine\ORM\QueryBuilder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* MailgunEventRepository. |
11
|
|
|
* |
12
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
13
|
|
|
* repository methods below. |
14
|
|
|
*/ |
15
|
|
|
class MailgunEventRepository extends EntityRepository |
16
|
|
|
{ |
17
|
|
|
public function getEventCount($criteria) |
18
|
|
|
{ |
19
|
|
|
$result = $this->getEventsQuery($criteria)->getQuery()->execute(); |
20
|
|
|
|
21
|
|
|
return sizeof($result); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getEvents($criteria, $orderBy, $limit, $offset) |
25
|
|
|
{ |
26
|
|
|
$qb = $this->getEventsQuery($criteria); |
27
|
|
|
$orderField = key($orderBy); |
28
|
|
|
$orderDirection = $orderBy[$orderField]; |
29
|
|
|
$qb->orderBy('e.'.$orderField, $orderDirection); |
30
|
|
|
if ($limit != -1) { |
31
|
|
|
$qb->setMaxResults($limit); |
32
|
|
|
$qb->setFirstResult($offset); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$result = $qb->getQuery()->execute(); |
36
|
|
|
|
37
|
|
|
return $result; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $criteria |
42
|
|
|
* |
43
|
|
|
* @return QueryBuilder |
44
|
|
|
*/ |
45
|
|
|
private function getEventsQuery($criteria) |
46
|
|
|
{ |
47
|
|
|
$lookForUnopened = array_key_exists('eventType', $criteria) && 'unopened' == $criteria['eventType']; |
48
|
|
|
if ($lookForUnopened) { |
49
|
|
|
unset($criteria['eventType']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$qb = $this->createQueryBuilder('e'); |
53
|
|
|
if (array_key_exists('domain', $criteria) && '' != $criteria['domain']) { |
54
|
|
|
$qb->andWhere('e.domain = :domain') |
55
|
|
|
->setParameter('domain', $criteria['domain']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
if (array_key_exists('recipient', $criteria) && '' != $criteria['recipient']) { |
59
|
|
|
$qb->andWhere('e.recipient like :recipient') |
60
|
|
|
->setParameter('recipient', '%'.$criteria['recipient'].'%'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (array_key_exists('eventType', $criteria) && 'all' != $criteria['eventType']) { |
64
|
|
|
$qb->andWhere('e.event = :eventType') |
65
|
|
|
->setParameter('eventType', $criteria['eventType']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
if (array_key_exists('search', $criteria) && '' != $criteria['search']) { |
69
|
|
|
$qb->andWhere('(e.messageHeaders like :search OR e.description like :search OR e.notification like :search OR e.reason like :search OR e.errorCode like :search OR e.ip like :search OR e.error like :search OR e.country like :search OR e.city like :search OR e.campaignId like :search OR e.campaignName like :search OR e.clientName like :search OR e.clientOs like :search OR e.clientType like :search OR e.deviceType like :search OR e.mailingList like :search OR e.messageId like :search OR e.tag like :search OR e.userAgent like :search OR e.url like :search)') |
70
|
|
|
->setParameter('search', '%'.$criteria['search'].'%'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($lookForUnopened) { |
74
|
|
|
$qb->andWhere("NOT EXISTS (SELECT o.id FROM AzineMailgunWebhooksBundle:MailgunEvent o WHERE o.messageId like e.messageId AND o.event in ('opened', 'clicked', 'unsubscribed', 'complained'))"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $qb; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function getEventTypes() |
81
|
|
|
{ |
82
|
|
|
$q = $this->getEntityManager()->createQueryBuilder() |
83
|
|
|
->select('e.event as event') |
84
|
|
|
->from($this->getEntityName(), 'e') |
85
|
|
|
->distinct() |
86
|
|
|
->orderBy('e.event ', 'asc') |
87
|
|
|
->getQuery(); |
88
|
|
|
$result = array(); |
89
|
|
|
foreach ($q->execute() as $next) { |
90
|
|
|
$result[] = $next['event']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $result; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function getDomains() |
97
|
|
|
{ |
98
|
|
|
$q = $this->getEntityManager()->createQueryBuilder() |
99
|
|
|
->select('e.domain as domain') |
100
|
|
|
->from($this->getEntityName(), 'e') |
101
|
|
|
->distinct() |
102
|
|
|
->orderBy('e.domain ', 'asc') |
103
|
|
|
->getQuery(); |
104
|
|
|
|
105
|
|
|
$result = array(); |
106
|
|
|
foreach ($q->execute() as $next) { |
107
|
|
|
$result[] = $next['domain']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function getRecipients() |
114
|
|
|
{ |
115
|
|
|
$q = $this->getEntityManager()->createQueryBuilder() |
116
|
|
|
->select('e.recipient as recipient') |
117
|
|
|
->from($this->getEntityName(), 'e') |
118
|
|
|
->distinct() |
119
|
|
|
->orderBy('e.recipient ', 'asc') |
120
|
|
|
->getQuery(); |
121
|
|
|
|
122
|
|
|
$result = array(); |
123
|
|
|
foreach ($q->execute() as $next) { |
124
|
|
|
$result[] = $next['recipient']; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $result; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get last known sender IP based on stored events list. |
132
|
|
|
* |
133
|
|
|
* @return string|null |
134
|
|
|
*/ |
135
|
|
|
public function getLastKnownSenderIp() |
136
|
|
|
{ |
137
|
|
|
$q = $this->getEntityManager()->createQueryBuilder() |
138
|
|
|
->select('e.messageHeaders as mh') |
139
|
|
|
->from($this->getEntityName(), 'e') |
140
|
|
|
->orderBy('e.timestamp', 'desc') |
141
|
|
|
->where('e.event IN (:events)') |
142
|
|
|
->setParameter('events', array('opened', 'delivered', 'bounced', 'dropped', 'complained')) |
143
|
|
|
->setMaxResults(50) |
144
|
|
|
->getQuery(); |
145
|
|
|
|
146
|
|
|
foreach ($q->execute() as $next) { |
147
|
|
|
if ($next['mh'] && isset($next['mh']['X-Mailgun-Sending-Ip'])) { |
148
|
|
|
return $next['mh']['X-Mailgun-Sending-Ip']; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return null; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getFieldsToOrderBy() |
156
|
|
|
{ |
157
|
|
|
return array( |
158
|
|
|
'campaignId', |
159
|
|
|
'campaignName', |
160
|
|
|
'city', |
161
|
|
|
'clientName', |
162
|
|
|
'clientOs', |
163
|
|
|
'clientType', |
164
|
|
|
'country', |
165
|
|
|
'description', |
166
|
|
|
'deviceType', |
167
|
|
|
'domain', |
168
|
|
|
'error', |
169
|
|
|
'errorCode', |
170
|
|
|
'event', |
171
|
|
|
'ip', |
172
|
|
|
'mailingList', |
173
|
|
|
'messageHeaders', |
174
|
|
|
'messageId', |
175
|
|
|
'notification', |
176
|
|
|
'reason', |
177
|
|
|
'recipient', |
178
|
|
|
'region', |
179
|
|
|
'tag', |
180
|
|
|
'timestamp', |
181
|
|
|
'type', |
182
|
|
|
'userAgent', |
183
|
|
|
'url', |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the most important events in the following priority |
189
|
|
|
* 1. Errors (rejected, failed) |
190
|
|
|
* 2. Warnings (complained, unsubscribed) |
191
|
|
|
* 3. Infos (accepted, delivered, opened, clicked, stored). |
192
|
|
|
* |
193
|
|
|
* @param int $count max number of events to fetch |
194
|
|
|
*/ |
195
|
|
|
public function getImportantEvents($count) |
196
|
|
|
{ |
197
|
|
|
$errors = $this->getEventsBySeverity(MailgunEvent::SEVERITY_ERROR, $count); |
198
|
|
|
$results = $errors; |
199
|
|
|
|
200
|
|
|
$getMoreCounter = $count - sizeof($errors); |
201
|
|
|
|
202
|
|
|
if ($getMoreCounter > 0) { |
203
|
|
|
$warnings = $this->getEventsBySeverity(MailgunEvent::SEVERITY_WARN, $count); |
204
|
|
|
$getMoreCounter = $getMoreCounter - sizeof($warnings); |
205
|
|
|
$results = array_merge($results, $warnings); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if ($getMoreCounter > 0) { |
209
|
|
|
$infos = $this->getEventsBySeverity(MailgunEvent::SEVERITY_INFO, $count); |
210
|
|
|
$results = array_merge($results, $infos); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $results; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get events by severity/type. |
218
|
|
|
* |
219
|
|
|
* @param string $severity [info, warning, error] |
220
|
|
|
* |
221
|
|
|
* @return Ambigous <multitype:, unknown> |
222
|
|
|
*/ |
223
|
|
|
public function getEventsBySeverity($severity = MailgunEvent::SEVERITY_INFO, $maxResults = 0) |
224
|
|
|
{ |
225
|
|
|
if (MailgunEvent::SEVERITY_INFO == $severity) { |
226
|
|
|
$criteria = "e.event = 'accepted' or e.event = 'delivered' or e.event = 'opened' or e.event = 'clicked' or e.event = 'stored'"; |
227
|
|
|
} elseif (MailgunEvent::SEVERITY_WARN == $severity) { |
228
|
|
|
$criteria = "e.event = 'complained' or e.event = 'unsubscribed'"; |
229
|
|
|
} elseif (MailgunEvent::SEVERITY_ERROR == $severity) { |
230
|
|
|
$criteria = "e.event = 'rejected' or e.event = 'failed' or e.event = 'dropped' or e.event = 'bounced'"; |
231
|
|
|
} else { |
232
|
|
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$qb = $this->createQueryBuilder('e') |
236
|
|
|
->where($criteria) |
237
|
|
|
->orderBy('e.timestamp ', 'desc'); |
238
|
|
|
|
239
|
|
|
if ($maxResults > 0) { |
240
|
|
|
$qb->setMaxResults($maxResults); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$q = $qb->getQuery(); |
244
|
|
|
$results = $q->execute(); |
245
|
|
|
|
246
|
|
|
return $results; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|