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