|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Azine\MailgunWebhooksBundle\Services; |
|
5
|
|
|
|
|
6
|
|
|
use Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\HetrixtoolsServiceResponse; |
|
7
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
8
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
9
|
|
|
use Azine\MailgunWebhooksBundle\Entity\EmailTrafficStatistics; |
|
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 ManagerRegistry |
|
55
|
|
|
*/ |
|
56
|
|
|
private $managerRegistry; |
|
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 ManagerRegistry $managerRegistry |
|
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
|
|
|
ManagerRegistry $managerRegistry, |
|
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->managerRegistry = $managerRegistry; |
|
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->managerRegistry->getManager()->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
|
|
|
$manager = $this->managerRegistry->getManager(); |
|
144
|
|
|
$manager->persist($spamAlert); |
|
145
|
|
|
$manager->flush($spamAlert); |
|
146
|
|
|
$manager->clear(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if($messagesSent == 0 && !empty($failedRecipients)){ |
|
150
|
|
|
|
|
151
|
|
|
throw new \Exception('Tried to send notification about spam complaint but no messages were sent'); |
|
152
|
|
|
} |
|
153
|
|
|
return $messagesSent; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param HetrixtoolsServiceResponse $response |
|
158
|
|
|
* @throws \Exception |
|
159
|
|
|
* @return int $messagesSent |
|
160
|
|
|
*/ |
|
161
|
|
|
public function sendBlacklistNotification(HetrixtoolsServiceResponse $response) |
|
162
|
|
|
{ |
|
163
|
|
|
$messagesSent = 0; |
|
|
|
|
|
|
164
|
|
|
$failedRecipients = []; |
|
165
|
|
|
|
|
166
|
|
|
/** @var \Swift_Message $message */ |
|
167
|
|
|
$message = $this->mailer->createMessage(); |
|
168
|
|
|
$message->setTo($this->spamAlertsRecipientEmail) |
|
169
|
|
|
->setFrom($this->fromEmail) |
|
170
|
|
|
->setSubject($this->translator->trans('notification.blacklist_received')) |
|
171
|
|
|
->setBody( |
|
172
|
|
|
$this->twig->render('@AzineMailgunWebhooks/Email/blacklistNotification.html.twig', array('response' => $response)), |
|
173
|
|
|
'text/html' |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
|
|
$messagesSent = $this->mailer->send($message, $failedRecipients); |
|
177
|
|
|
|
|
178
|
|
|
if($messagesSent == 0 && !empty($failedRecipients)){ |
|
179
|
|
|
|
|
180
|
|
|
throw new \Exception('Tried to send notification about ip is blacklisted but no messages were sent'); |
|
181
|
|
|
} |
|
182
|
|
|
return $messagesSent; |
|
183
|
|
|
} |
|
184
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.