1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Azine\MailgunWebhooksBundle\Entity\Repositories; |
4
|
|
|
|
5
|
|
|
use Azine\MailgunWebhooksBundle\Entity\MailgunEvent; |
6
|
|
|
use Azine\MailgunWebhooksBundle\Entity\MailgunMessageSummary; |
7
|
|
|
use Doctrine\ORM\QueryBuilder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* MailgunMessageSummaryRepository |
11
|
|
|
* |
12
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
13
|
|
|
* repository methods below. |
14
|
|
|
*/ |
15
|
|
|
class MailgunMessageSummaryRepository extends \Doctrine\ORM\EntityRepository |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
public function getFieldsToOrderBy(){ |
19
|
|
|
return array( |
20
|
|
|
'lastOpened', |
21
|
|
|
'sendDate', |
22
|
|
|
'firstOpened', |
23
|
|
|
'openCount', |
24
|
|
|
'fromAddress', |
25
|
|
|
'toAddress', |
26
|
|
|
'deliveryStatis', |
27
|
|
|
'subject', |
28
|
|
|
'senderIp' |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get distinct list of email recipients. |
34
|
|
|
* |
35
|
|
|
* @return array of string |
36
|
|
|
*/ |
37
|
|
|
public function getRecipients() |
38
|
|
|
{ |
39
|
|
|
$q = $this->getEntityManager()->createQueryBuilder() |
40
|
|
|
->select('m.toAddress as recipient') |
41
|
|
|
->from($this->getEntityName(), 'm') |
42
|
|
|
->distinct() |
43
|
|
|
->orderBy('m.toAddress ', 'asc') |
44
|
|
|
->getQuery(); |
45
|
|
|
|
46
|
|
|
$result = array(); |
47
|
|
|
foreach ($q->execute() as $next) { |
48
|
|
|
$result[] = $next['recipient']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $result; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get distinct list of email senders. |
56
|
|
|
* |
57
|
|
|
* @return array of string |
58
|
|
|
*/ |
59
|
|
|
public function getSenders() |
60
|
|
|
{ |
61
|
|
|
$q = $this->getEntityManager()->createQueryBuilder() |
62
|
|
|
->select('m.fromAddress as sender') |
63
|
|
|
->from($this->getEntityName(), 'm') |
64
|
|
|
->distinct() |
65
|
|
|
->orderBy('m.fromAddress ', 'asc') |
66
|
|
|
->getQuery(); |
67
|
|
|
|
68
|
|
|
$result = array(); |
69
|
|
|
foreach ($q->execute() as $next) { |
70
|
|
|
$result[] = $next['sender']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getMessageSummaries($criteria, $orderBy, $limit, $offset){ |
77
|
|
|
$qb = $this->getMessageSummaryQuery($criteria); |
78
|
|
|
|
79
|
|
|
$orderField = key($orderBy); |
80
|
|
|
$orderDirection = $orderBy[$orderField]; |
81
|
|
|
$qb->orderBy('m.'.$orderField, $orderDirection); |
82
|
|
|
if (-1 != $limit) { |
83
|
|
|
$qb->setMaxResults($limit); |
84
|
|
|
$qb->setFirstResult($offset); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$result = $qb->getQuery()->execute(); |
88
|
|
|
|
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getMessageSummaryCount($criteria){ |
93
|
|
|
return $this->getMessageSummaryQuery($criteria)->select('count(m.id)')->getQuery()->getSingleScalarResult(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function getMessageSummaryQuery($criteria){ |
97
|
|
|
$qb = $this->createQueryBuilder('m'); |
98
|
|
|
if (array_key_exists('fromAddress', $criteria) && '' != $criteria['fromAddress']) { |
99
|
|
|
$qb->andWhere('m.fromAddress = :fromAddress') |
100
|
|
|
->setParameter('fromAddress', $criteria['fromAddress']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (array_key_exists('toAddress', $criteria) && '' != $criteria['toAddress']) { |
104
|
|
|
$qb->andWhere('m.toAddress like :toAddress') |
105
|
|
|
->setParameter('toAddress', '%'.$criteria['toAddress'].'%'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (array_key_exists('search', $criteria) && '' != $criteria['search']) { |
109
|
|
|
$qb->andWhere('(m.toAddress like :search OR m.subject like :search OR m.fromAddress like :search )') |
110
|
|
|
->setParameter('search', '%'.$criteria['search'].'%'); |
111
|
|
|
} |
112
|
|
|
return $qb; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function findSummary($fromAddress, $toAddresses, $sendTime, $subject){ |
|
|
|
|
116
|
|
|
// extract email-address part |
117
|
|
|
$from = mailparse_rfc822_parse_addresses($fromAddress)[0]; |
118
|
|
|
|
119
|
|
|
$qb = $this->createQueryBuilder('m'); |
120
|
|
|
$qb->where('m.sendDate < :higherBound AND m.sendDate > :lowerBound AND m.fromAddress like :fromAddress') |
121
|
|
|
->setParameters('lowerBound', $lowerBound) |
|
|
|
|
122
|
|
|
->setParameters('higherBound', $higherBound) |
|
|
|
|
123
|
|
|
->setParameter('fromAddress', $from); |
124
|
|
|
|
125
|
|
|
// extract email-address parts |
126
|
|
|
$to = ""; |
127
|
|
|
foreach (mailparse_rfc822_parse_addresses($toAddresses) as $nextMatch){ |
128
|
|
|
$to += "'".$nextMatch['address']."',"; |
129
|
|
|
} |
130
|
|
|
$to = '('.trim($to, ", ").')'; |
131
|
|
|
|
132
|
|
|
if(strlen($to) > 0){ |
133
|
|
|
$qb->andWhere('m.toAddress in :to')->setParameters('to', $to); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if(strlen($to) > 0){ |
137
|
|
|
$qb->andWhere('m.subject like :subject')->setParameters('subject', $subject); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function createOrUpdateMessageSummary(MailgunEvent $event){ |
142
|
|
|
$messageSummary = $this->findOneBy(array('id' => $event->getMessageId())); |
143
|
|
|
if ($messageSummary == null) { |
144
|
|
|
$ip = $event->getIp() != null ? $event->getIp() : "unknown"; |
145
|
|
|
$messageSummary = new MailgunMessageSummary($event->getMessageId(), $event->getDateTime(), $event->getSender(), $event->getRecipient(), "no subject found yet", $ip); |
146
|
|
|
} |
147
|
|
|
$messageSummary->updateDeliveryStatus($event->getEvent()); |
148
|
|
|
|
149
|
|
|
if ($event->getEvent() == 'opened') { |
150
|
|
|
if ($messageSummary->getFirstOpened() == null || $messageSummary->getFirstOpened() > $event->getDateTime()) { |
151
|
|
|
$messageSummary->setFirstOpened($event->getDateTime()); |
152
|
|
|
} |
153
|
|
|
if($messageSummary->getLastOpened() == null || $messageSummary->getLastOpened() < $event->getDateTime()) { |
154
|
|
|
$messageSummary->setLastOpened($event->getDateTime()); |
155
|
|
|
} |
156
|
|
|
$messageSummary->increaseOpenCount(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
foreach ($event->getMessageHeaders() as $header) { |
160
|
|
|
if($header[0] == 'X-Mailgun-Sending-Ip') { |
161
|
|
|
$messageSummary->setSenderIp($header[1]); |
162
|
|
|
} else |
163
|
|
|
|
164
|
|
|
if($header[0] == 'Subject') { |
165
|
|
|
$messageSummary->setSubject($header[1]); |
166
|
|
|
} else |
167
|
|
|
|
168
|
|
|
if($header[0] == 'Sender') { |
169
|
|
|
$messageSummary->setFromAddress($header[1]); |
170
|
|
|
} else |
171
|
|
|
|
172
|
|
|
if($header[0] == 'To') { |
173
|
|
|
$messageSummary->appendToToAddress($header[1]); |
174
|
|
|
} else |
175
|
|
|
|
176
|
|
|
if($header[0] == 'Cc') { |
177
|
|
|
$messageSummary->appendToToAddress($header[1]); |
178
|
|
|
} else |
179
|
|
|
|
180
|
|
|
if($header[0] == 'Bcc') { |
181
|
|
|
$messageSummary->appendToToAddress($header[1]); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$manager = $this->getEntityManager(); |
186
|
|
|
$manager->persist($messageSummary); |
187
|
|
|
$manager->flush($messageSummary); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.