1
|
|
|
<?php |
2
|
|
|
namespace Azine\MailgunWebhooksBundle\Services; |
3
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Dominik Businger |
11
|
|
|
*/ |
12
|
|
|
class AzineMailgunService { |
13
|
|
|
/** |
14
|
|
|
* @var ManagerRegistry |
15
|
|
|
*/ |
16
|
|
|
private $managerRegistry; |
17
|
|
|
|
18
|
3 |
|
public function __construct(ManagerRegistry $managerRegistry) { |
19
|
3 |
|
$this->managerRegistry = $managerRegistry; |
20
|
3 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Deletes all MailgunEvents that are older than the ageLimit |
24
|
|
|
* @param \DateTime $ageLimit |
25
|
|
|
*/ |
26
|
1 |
|
public function removeOldEventEntries(\DateTime $ageLimit) { |
27
|
1 |
|
return $this->removeEvents(null, $ageLimit); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Deletes all MailgunEvents of the given type(s) that are older than the ageLimit |
32
|
|
|
* |
33
|
|
|
* Possible Types are: |
34
|
|
|
* |
35
|
|
|
* Event-Type Description |
36
|
|
|
* --------------------------------------------------------------------- |
37
|
|
|
* accepted Mailgun accepted the request to send/forward the email and the message has been placed in queue. |
38
|
|
|
* rejected Mailgun rejected the request to send/forward the email. |
39
|
|
|
* delivered Mailgun sent the email and it was accepted by the recipient email server. |
40
|
|
|
* failed Mailgun could not deliver the email to the recipient email server. |
41
|
|
|
* opened The email recipient opened the email and enabled image viewing. Open tracking must be enabled in the Mailgun control panel, and the CNAME record must be pointing to mailgun.org. |
42
|
|
|
* clicked The email recipient clicked on a link in the email. Click tracking must be enabled in the Mailgun control panel, and the CNAME record must be pointing to mailgun.org. |
43
|
|
|
* unsubscribed The email recipient clicked on the unsubscribe link. Unsubscribe tracking must be enabled in the Mailgun control panel. |
44
|
|
|
* complained The email recipient clicked on the spam complaint button within their email client. Feedback loops enable the notification to be received by Mailgun. |
45
|
|
|
* stored Mailgun has stored an incoming message |
46
|
|
|
* |
47
|
|
|
* @param string|array of string|null $type |
48
|
|
|
* @param \DateTime $ageLimit |
49
|
|
|
* @return integer number of deleted records |
50
|
|
|
*/ |
51
|
3 |
|
public function removeEvents($type = null, \DateTime $ageLimit) { |
52
|
3 |
|
$qb = $this->getManager()->createQueryBuilder()->delete("Azine\MailgunWebhooksBundle\Entity\MailgunEvent", "e")->andWhere("e.timestamp < :age") |
53
|
3 |
|
->setParameter("age", $ageLimit->getTimestamp()); |
54
|
|
|
|
55
|
3 |
|
if (is_string($type)) { |
56
|
1 |
|
$qb->andWhere("e.event = :type")->setParameter("type", $type); |
57
|
2 |
|
} elseif (is_array($type)) { |
58
|
1 |
|
$qb->andWhere("e.event in (:type)")->setParameter("type", $type); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
return $qb->getQuery()->execute(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Delete all MailgunEvents that match the given criteria (added as "andWhere" clauses). |
66
|
|
|
* |
67
|
|
|
* @param array $criteria |
68
|
|
|
* @return \Doctrine\ORM\mixed |
69
|
|
|
*/ |
70
|
|
|
public function removeEventsBy(array $criteria) |
71
|
|
|
{ |
72
|
|
|
$qb = $this->getManager()->createQueryBuilder() |
73
|
|
|
->delete("Azine\MailgunWebhooksBundle\Entity\MailgunEvent", "e"); |
74
|
|
|
foreach ($criteria as $field => $value){ |
75
|
|
|
if($value == null){ |
76
|
|
|
$qb->andWhere("e.$field is null");
|
77
|
|
|
|
78
|
|
|
} else { |
79
|
|
|
$qb->andWhere("e.$field = :$field") |
80
|
|
|
->setParameter($field, $value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $qb->getQuery()->execute(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return EntityManager |
89
|
|
|
*/ |
90
|
3 |
|
private function getManager() { |
91
|
3 |
|
return $this->managerRegistry->getManager(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|