1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Check\Email\Send; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Swift_Mailer; |
8
|
|
|
use Swift_Message; |
9
|
|
|
use Swift_Mime_Message; |
10
|
|
|
use Swift_Mime_MimePart; |
11
|
|
|
use Swift_SwiftException; |
12
|
|
|
use TonicHealthCheck\Check\Email\AbstractEmailCheck; |
13
|
|
|
use TonicHealthCheck\Check\Email\Entity\EmailSendReceive; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class EmailSendCheck |
17
|
|
|
* @package TonicHealthCheck\Check\Email\Send |
18
|
|
|
*/ |
19
|
|
|
class EmailSendCheck extends AbstractEmailCheck |
20
|
|
|
{ |
21
|
|
|
const CHECK = 'email-send-check'; |
22
|
|
|
const MESSAGE_BODY = 'This is a test, you don\'t need to reply this massage.'; |
23
|
|
|
const SEND_INTERVAL = 600; |
24
|
|
|
const SUBJECT_TEMPLATE = '%s:#%d'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $sendInterval; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Swift_Mailer $client |
33
|
|
|
*/ |
34
|
|
|
private $mailer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EntityManager |
38
|
|
|
*/ |
39
|
|
|
private $doctrine; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string; |
43
|
|
|
*/ |
44
|
|
|
private $from; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string; |
48
|
|
|
*/ |
49
|
|
|
private $toSubject; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $checkNode |
53
|
|
|
* @param Swift_Mailer $mailer |
54
|
|
|
* @param EntityManager $doctrine |
55
|
|
|
* @param string $from |
56
|
|
|
* @param string $toSubjects |
57
|
|
|
* @param int $sendInterval |
58
|
|
|
*/ |
59
|
4 |
|
public function __construct( |
60
|
|
|
$checkNode, |
61
|
|
|
Swift_Mailer $mailer, |
62
|
|
|
EntityManager $doctrine, |
63
|
|
|
$from, |
64
|
|
|
$toSubjects, |
65
|
|
|
$sendInterval = self::SEND_INTERVAL |
66
|
|
|
) { |
67
|
4 |
|
parent::__construct($checkNode); |
68
|
|
|
|
69
|
4 |
|
$this->setMailer($mailer); |
70
|
4 |
|
$this->setDoctrine($doctrine); |
71
|
4 |
|
$this->setFrom($from); |
72
|
4 |
|
$this->setToSubject($toSubjects); |
73
|
4 |
|
$this->setSendInterval($sendInterval); |
74
|
4 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Check email can send and receive messages |
78
|
|
|
* @return bool|void |
79
|
|
|
* @throws EmailSendCheckException |
80
|
|
|
*/ |
81
|
4 |
|
public function check() |
82
|
|
|
{ |
83
|
4 |
|
$emailSendReceiveR = $this->getDoctrine()->getRepository(EmailSendReceive::class); |
84
|
4 |
|
$lastSandedEmail = $emailSendReceiveR->findOneBy([], ['sentAt' => 'DESC']); |
85
|
4 |
|
if (null === $lastSandedEmail |
86
|
4 |
|
|| empty($lastSandedEmail->getSentAt()) |
87
|
4 |
|
|| (time() - $lastSandedEmail->getSentAt()->getTimestamp()) > $this->getSendInterval() |
88
|
4 |
|
) { |
89
|
4 |
|
$emailSendCheck = $this->createEmailSendReceive(); |
90
|
|
|
|
91
|
4 |
|
$this->performSend($emailSendCheck); |
92
|
1 |
|
} |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return Swift_Mailer |
97
|
|
|
*/ |
98
|
4 |
|
public function getMailer() |
99
|
|
|
{ |
100
|
4 |
|
return $this->mailer; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return EntityManager |
105
|
|
|
*/ |
106
|
4 |
|
public function getDoctrine() |
107
|
|
|
{ |
108
|
4 |
|
return $this->doctrine; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
4 |
|
public function getFrom() |
115
|
|
|
{ |
116
|
4 |
|
return $this->from; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
4 |
|
public function getToSubject() |
123
|
|
|
{ |
124
|
4 |
|
return $this->toSubject; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
4 |
|
public function getSendInterval() |
131
|
|
|
{ |
132
|
4 |
|
return $this->sendInterval; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param Swift_Mailer $mailer |
137
|
|
|
*/ |
138
|
4 |
|
protected function setMailer(Swift_Mailer $mailer) |
139
|
|
|
{ |
140
|
4 |
|
$this->mailer = $mailer; |
141
|
4 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param EntityManager $doctrine |
145
|
|
|
*/ |
146
|
4 |
|
protected function setDoctrine(EntityManager $doctrine) |
147
|
|
|
{ |
148
|
4 |
|
$this->doctrine = $doctrine; |
149
|
4 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $from |
153
|
|
|
*/ |
154
|
4 |
|
protected function setFrom($from) |
155
|
|
|
{ |
156
|
4 |
|
$this->from = $from; |
157
|
4 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $toSubject |
161
|
|
|
*/ |
162
|
4 |
|
protected function setToSubject($toSubject) |
163
|
|
|
{ |
164
|
4 |
|
$this->toSubject = $toSubject; |
165
|
4 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param EmailSendReceive $emailSendCheck |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
4 |
|
protected function genEmailSubject(EmailSendReceive $emailSendCheck) |
172
|
|
|
{ |
173
|
4 |
|
return sprintf(static::SUBJECT_TEMPLATE, $this->getCheckIdent(), $emailSendCheck->getId()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param int $sendInterval |
178
|
|
|
*/ |
179
|
4 |
|
protected function setSendInterval($sendInterval) |
180
|
|
|
{ |
181
|
4 |
|
$this->sendInterval = $sendInterval; |
182
|
4 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return EmailSendReceive |
186
|
|
|
*/ |
187
|
4 |
|
protected function createEmailSendReceive() |
188
|
|
|
{ |
189
|
4 |
|
$emailSendCheck = new EmailSendReceive(); |
190
|
|
|
|
191
|
4 |
|
$emailSendCheck->setFrom($this->getFrom()); |
192
|
4 |
|
$emailSendCheck->setTo($this->getToSubject()); |
193
|
4 |
|
$emailSendCheck->setBody(static::MESSAGE_BODY); |
194
|
|
|
|
195
|
4 |
|
$this->saveEmailSendReceive($emailSendCheck); |
196
|
|
|
|
197
|
4 |
|
$emailSendCheck->setSubject($this->genEmailSubject($emailSendCheck)); |
198
|
|
|
|
199
|
4 |
|
$this->saveEmailSendReceive($emailSendCheck); |
200
|
|
|
|
201
|
4 |
|
return $emailSendCheck; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param Swift_Mime_Message $message |
206
|
|
|
* @param EmailSendReceive $emailSendCheck |
207
|
|
|
* @throws EmailSendCheckException |
208
|
|
|
*/ |
209
|
4 |
|
protected function sendMessage(Swift_Mime_Message $message, EmailSendReceive $emailSendCheck) |
210
|
|
|
{ |
211
|
4 |
|
$failedRecipients = []; |
212
|
4 |
|
$numSent = $this->getMailer()->send($message, $failedRecipients); |
213
|
2 |
|
$this->getMailer()->getTransport()->stop(); |
214
|
2 |
|
if (!$numSent) { |
215
|
1 |
|
$emailSendCheck->setStatus(EmailSendReceive::STATUS_SAND_ERROR); |
216
|
1 |
|
$this->saveEmailSendReceive($emailSendCheck); |
217
|
1 |
|
throw EmailSendCheckException::doesNotSendMessage(array_keys($failedRecipients)); |
218
|
|
|
} |
219
|
1 |
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param EmailSendReceive $emailSendCheck |
223
|
|
|
* @return Swift_Mime_MimePart |
224
|
|
|
*/ |
225
|
4 |
|
protected function buildMessage(EmailSendReceive $emailSendCheck) |
226
|
|
|
{ |
227
|
4 |
|
$message = Swift_Message::newInstance($emailSendCheck->getSubject()) |
228
|
4 |
|
->setFrom($emailSendCheck->getFrom()) |
229
|
4 |
|
->setTo($emailSendCheck->getTo()) |
230
|
4 |
|
->setBody($emailSendCheck->getBody()); |
231
|
|
|
|
232
|
4 |
|
return $message; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param EmailSendReceive $emailSendCheck |
237
|
|
|
*/ |
238
|
4 |
|
private function saveEmailSendReceive(EmailSendReceive $emailSendCheck) |
239
|
|
|
{ |
240
|
4 |
|
$this->getDoctrine()->persist($emailSendCheck); |
241
|
4 |
|
$this->getDoctrine()->flush(); |
242
|
4 |
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param EmailSendReceive $emailSendCheck |
246
|
|
|
* @throws EmailSendCheckException |
247
|
|
|
*/ |
248
|
4 |
|
private function performSend(EmailSendReceive $emailSendCheck) |
249
|
|
|
{ |
250
|
4 |
|
$message = $this->buildMessage($emailSendCheck); |
251
|
|
|
|
252
|
|
|
try { |
253
|
4 |
|
$this->sendMessage($message, $emailSendCheck); |
|
|
|
|
254
|
1 |
|
$emailSendCheck->setStatus(EmailSendReceive::STATUS_SANDED); |
255
|
1 |
|
$emailSendCheck->setSentAt(new DateTime()); |
256
|
1 |
|
$this->saveEmailSendReceive($emailSendCheck); |
257
|
4 |
|
} catch (Swift_SwiftException $e) { |
258
|
1 |
|
$emailSendCheck->setStatus(EmailSendReceive::STATUS_SAND_ERROR); |
259
|
1 |
|
$this->saveEmailSendReceive($emailSendCheck); |
260
|
1 |
|
throw EmailSendCheckException::internalProblem($e); |
261
|
|
|
} |
262
|
1 |
|
} |
263
|
|
|
} |
264
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: