|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Azine\MailgunWebhooksBundle\Services; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\Translation\Translator; |
|
7
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
8
|
|
|
use Azine\MailgunWebhooksBundle\Entity\EmailTrafficStatistics; |
|
9
|
|
|
use Doctrine\ORM\EntityManager; |
|
10
|
|
|
|
|
11
|
|
|
class AzineMailgunMailerService |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Swift_Mailer |
|
15
|
|
|
*/ |
|
16
|
|
|
private $mailer; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Twig_Environment |
|
20
|
|
|
*/ |
|
21
|
|
|
private $twig; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var TranslatorInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $translator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $fromEmail; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $ticketId; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $ticketSubject; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $ticketMessage; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $spamAlertsRecipientEmail; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var EntityManager |
|
55
|
|
|
*/ |
|
56
|
|
|
private $entityManager; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var int |
|
60
|
|
|
*/ |
|
61
|
|
|
private $sendNotificationsInterval; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* AzineMailgunMailerService constructor. |
|
65
|
|
|
* |
|
66
|
|
|
* @param \Swift_Mailer $mailer |
|
67
|
|
|
* @param \Twig_Environment $twig |
|
68
|
|
|
* @param TranslatorInterface $translator |
|
69
|
|
|
* @param string $fromEmail |
|
70
|
|
|
* @param string $ticketId |
|
71
|
|
|
* @param string $ticketSubject |
|
72
|
|
|
* @param string $ticketMessage |
|
73
|
|
|
* @param string $spamAlertsRecipientEmail |
|
74
|
|
|
* @param EntityManager $entityManager |
|
75
|
|
|
* @param int $sendNotificationsInterval |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct( |
|
78
|
|
|
\Swift_Mailer $mailer, |
|
79
|
|
|
\Twig_Environment $twig, |
|
80
|
|
|
TranslatorInterface $translator, |
|
81
|
|
|
$fromEmail, |
|
82
|
|
|
$ticketId, |
|
83
|
|
|
$ticketSubject, |
|
84
|
|
|
$ticketMessage, |
|
85
|
|
|
$spamAlertsRecipientEmail, |
|
86
|
|
|
EntityManager $entityManager, |
|
|
|
|
|
|
87
|
|
|
$sendNotificationsInterval |
|
88
|
|
|
) { |
|
89
|
|
|
$this->mailer = $mailer; |
|
90
|
|
|
$this->twig = $twig; |
|
91
|
|
|
$this->translator = $translator; |
|
92
|
|
|
$this->fromEmail = $fromEmail; |
|
93
|
|
|
$this->ticketId = $ticketId; |
|
94
|
|
|
$this->ticketSubject = $ticketSubject; |
|
95
|
|
|
$this->ticketMessage = $ticketMessage; |
|
96
|
|
|
$this->spamAlertsRecipientEmail = $spamAlertsRecipientEmail; |
|
97
|
|
|
$this->entityManager = $entityManager; |
|
98
|
|
|
$this->sendNotificationsInterval = $sendNotificationsInterval; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $eventId |
|
103
|
|
|
* @throws \Exception |
|
104
|
|
|
* @return int $messagesSent |
|
105
|
|
|
*/ |
|
106
|
|
|
public function sendSpamComplaintNotification($eventId) |
|
107
|
|
|
{ |
|
108
|
|
|
$messagesSent = 0; |
|
109
|
|
|
$failedRecipients = []; |
|
110
|
|
|
|
|
111
|
|
|
/** @var \Swift_Message $message */ |
|
112
|
|
|
$message = $this->mailer->createMessage(); |
|
113
|
|
|
$message->setTo($this->spamAlertsRecipientEmail) |
|
114
|
|
|
->setFrom($this->fromEmail) |
|
115
|
|
|
->setSubject($this->translator->trans('notification.spam_complaint_received')) |
|
116
|
|
|
->setBody( |
|
117
|
|
|
$this->twig->render('@AzineMailgunWebhooks/Email/notification.html.twig', array('eventId' => $eventId, 'ticketId' => $this->ticketId)), |
|
118
|
|
|
'text/html' |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
$lastSpamReport = $this->entityManager->getRepository(EmailTrafficStatistics::class) |
|
122
|
|
|
->getLastByAction(EmailTrafficStatistics::SPAM_ALERT_SENT); |
|
123
|
|
|
|
|
124
|
|
|
if($lastSpamReport instanceof EmailTrafficStatistics) { |
|
125
|
|
|
|
|
126
|
|
|
$time = new \DateTime(); |
|
127
|
|
|
$timeDiff = $time->diff($lastSpamReport->getCreated()); |
|
128
|
|
|
|
|
129
|
|
|
if($timeDiff->i > $this->sendNotificationsInterval) { |
|
130
|
|
|
|
|
131
|
|
|
$messagesSent = $this->mailer->send($message, $failedRecipients); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
}else{ |
|
135
|
|
|
|
|
136
|
|
|
$messagesSent = $this->mailer->send($message, $failedRecipients); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if($messagesSent > 0) { |
|
140
|
|
|
|
|
141
|
|
|
$spamAlert = new EmailTrafficStatistics(); |
|
142
|
|
|
$spamAlert->setAction(EmailTrafficStatistics::SPAM_ALERT_SENT); |
|
143
|
|
|
$this->entityManager->persist($spamAlert); |
|
144
|
|
|
$this->entityManager->flush($spamAlert); |
|
145
|
|
|
$this->entityManager->clear(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if($messagesSent == 0 && !empty($failedRecipients)){ |
|
149
|
|
|
|
|
150
|
|
|
throw new \Exception('Tried to send notification about spam complaint but no messages were sent'); |
|
151
|
|
|
} |
|
152
|
|
|
return $messagesSent; |
|
153
|
|
|
} |
|
154
|
|
|
} |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.