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