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