|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Check\Email\Send; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use Swift_Mailer; |
|
7
|
|
|
use Swift_Message; |
|
8
|
|
|
use Swift_Mime_Message; |
|
9
|
|
|
use Swift_Mime_MimePart; |
|
10
|
|
|
use Swift_SwiftException; |
|
11
|
|
|
use TonicHealthCheck\Check\Email\AbstractEmailCheck; |
|
12
|
|
|
use TonicHealthCheck\Check\Email\Entity\EmailSendReceive; |
|
13
|
|
|
use TonicHealthCheck\Check\Email\Entity\EmailSendReceiveCollection; |
|
14
|
|
|
use TonicHealthCheck\Check\Email\Persist\PersistCollectionInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class EmailSendCheck. |
|
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:time:%s'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
private $sendInterval; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Swift_Mailer |
|
33
|
|
|
*/ |
|
34
|
|
|
private $mailer; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var PersistCollectionInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $persistCollection; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var EmailSendReceiveCollection |
|
43
|
|
|
*/ |
|
44
|
|
|
private $emailSendReceiveCollection; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string; |
|
48
|
|
|
*/ |
|
49
|
|
|
private $from; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string; |
|
53
|
|
|
*/ |
|
54
|
|
|
private $toSubject; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $checkNode |
|
58
|
|
|
* @param Swift_Mailer $mailer |
|
59
|
|
|
* @param string $from |
|
60
|
|
|
* @param string $toSubjects |
|
61
|
|
|
* @param int $sendInterval |
|
62
|
|
|
*/ |
|
63
|
8 |
|
public function __construct( |
|
64
|
|
|
$checkNode, |
|
65
|
|
|
Swift_Mailer $mailer, |
|
66
|
|
|
PersistCollectionInterface $persistCollection, |
|
67
|
|
|
$from, |
|
68
|
|
|
$toSubjects, |
|
69
|
|
|
$sendInterval = self::SEND_INTERVAL |
|
70
|
|
|
) { |
|
71
|
8 |
|
parent::__construct($checkNode); |
|
72
|
|
|
|
|
73
|
8 |
|
$this->setMailer($mailer); |
|
74
|
8 |
|
$this->setPersistCollection($persistCollection); |
|
75
|
8 |
|
$this->setFrom($from); |
|
76
|
8 |
|
$this->setToSubject($toSubjects); |
|
77
|
8 |
|
$this->setSendInterval($sendInterval); |
|
78
|
8 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Check email can send and receive messages. |
|
82
|
|
|
* |
|
83
|
|
|
* @return void |
|
84
|
|
|
* |
|
85
|
|
|
* @throws EmailSendCheckException |
|
86
|
|
|
*/ |
|
87
|
7 |
|
public function performCheck() |
|
88
|
|
|
{ |
|
89
|
7 |
|
$this->setEmailSendReceiveColl($this->getPersistCollection()->load()); |
|
90
|
|
|
|
|
91
|
7 |
|
$lastSandedEmail = $this->getEmailSendReceiveColl()->count() - 1 >= 0 ? |
|
92
|
7 |
|
$this->getEmailSendReceiveColl()->at( |
|
93
|
7 |
|
$this->getEmailSendReceiveColl()->count() - 1 |
|
94
|
|
|
) |
|
95
|
7 |
|
: null; |
|
96
|
7 |
|
if (null === $lastSandedEmail |
|
97
|
7 |
|
|| empty($lastSandedEmail->getSentAt()) |
|
98
|
7 |
|
|| (time() - $lastSandedEmail->getSentAt()->getTimestamp()) > $this->getSendInterval() |
|
99
|
|
|
) { |
|
100
|
6 |
|
$emailSendCheck = $this->createEmailSendReceive(); |
|
101
|
|
|
|
|
102
|
6 |
|
$this->performSend($emailSendCheck); |
|
103
|
|
|
} |
|
104
|
3 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return Swift_Mailer |
|
108
|
|
|
*/ |
|
109
|
6 |
|
public function getMailer() |
|
110
|
|
|
{ |
|
111
|
6 |
|
return $this->mailer; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return PersistCollectionInterface |
|
116
|
|
|
*/ |
|
117
|
7 |
|
public function getPersistCollection() |
|
118
|
|
|
{ |
|
119
|
7 |
|
return $this->persistCollection; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return EmailSendReceiveCollection |
|
124
|
|
|
*/ |
|
125
|
7 |
|
public function getEmailSendReceiveColl() |
|
126
|
|
|
{ |
|
127
|
7 |
|
return $this->emailSendReceiveCollection; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
6 |
|
public function getFrom() |
|
134
|
|
|
{ |
|
135
|
6 |
|
return $this->from; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
6 |
|
public function getToSubject() |
|
142
|
|
|
{ |
|
143
|
6 |
|
return $this->toSubject; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return int |
|
148
|
|
|
*/ |
|
149
|
7 |
|
public function getSendInterval() |
|
150
|
|
|
{ |
|
151
|
7 |
|
return $this->sendInterval; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param Swift_Mailer $mailer |
|
156
|
|
|
*/ |
|
157
|
8 |
|
protected function setMailer(Swift_Mailer $mailer) |
|
158
|
|
|
{ |
|
159
|
8 |
|
$this->mailer = $mailer; |
|
160
|
8 |
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param PersistCollectionInterface $persistCollection |
|
164
|
|
|
*/ |
|
165
|
8 |
|
protected function setPersistCollection(PersistCollectionInterface $persistCollection) |
|
166
|
|
|
{ |
|
167
|
8 |
|
$this->persistCollection = $persistCollection; |
|
168
|
8 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param EmailSendReceiveCollection $emailSendReceiveC |
|
172
|
|
|
*/ |
|
173
|
7 |
|
protected function setEmailSendReceiveColl(EmailSendReceiveCollection $emailSendReceiveC) |
|
174
|
|
|
{ |
|
175
|
7 |
|
$this->emailSendReceiveCollection = $emailSendReceiveC; |
|
176
|
7 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param string $from |
|
180
|
|
|
*/ |
|
181
|
8 |
|
protected function setFrom($from) |
|
182
|
|
|
{ |
|
183
|
8 |
|
$this->from = $from; |
|
184
|
8 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param string $toSubject |
|
188
|
|
|
*/ |
|
189
|
8 |
|
protected function setToSubject($toSubject) |
|
190
|
|
|
{ |
|
191
|
8 |
|
$this->toSubject = $toSubject; |
|
192
|
8 |
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
6 |
|
protected function genEmailSubject() |
|
198
|
|
|
{ |
|
199
|
6 |
|
return sprintf(static::SUBJECT_TEMPLATE, $this->getIndent(), date(DATE_W3C)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param int $sendInterval |
|
204
|
|
|
*/ |
|
205
|
8 |
|
protected function setSendInterval($sendInterval) |
|
206
|
|
|
{ |
|
207
|
8 |
|
$this->sendInterval = $sendInterval; |
|
208
|
8 |
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return EmailSendReceive |
|
212
|
|
|
*/ |
|
213
|
6 |
|
protected function createEmailSendReceive() |
|
214
|
|
|
{ |
|
215
|
6 |
|
$emailSendCheck = new EmailSendReceive(); |
|
216
|
|
|
|
|
217
|
6 |
|
$emailSendCheck->setFrom($this->getFrom()); |
|
218
|
6 |
|
$emailSendCheck->setTo($this->getToSubject()); |
|
219
|
6 |
|
$emailSendCheck->setBody(static::MESSAGE_BODY); |
|
220
|
6 |
|
$emailSendCheck->setSubject($this->genEmailSubject()); |
|
221
|
|
|
|
|
222
|
6 |
|
return $emailSendCheck; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param Swift_Mime_Message $message |
|
227
|
|
|
* @param EmailSendReceive $emailSendCheck |
|
228
|
|
|
* |
|
229
|
|
|
* @throws EmailSendCheckException |
|
230
|
|
|
*/ |
|
231
|
6 |
|
protected function sendMessage(Swift_Mime_Message $message, EmailSendReceive $emailSendCheck) |
|
232
|
|
|
{ |
|
233
|
6 |
|
$failedRecipients = []; |
|
234
|
6 |
|
$numSent = $this->getMailer()->send($message, $failedRecipients); |
|
235
|
4 |
|
$this->getMailer()->getTransport()->stop(); |
|
236
|
4 |
|
if (!$numSent) { |
|
237
|
2 |
|
$emailSendCheck->setStatus(EmailSendReceive::STATUS_SAND_ERROR); |
|
238
|
2 |
|
throw EmailSendCheckException::doesNotSendMessage(array_keys($failedRecipients)); |
|
239
|
|
|
} |
|
240
|
2 |
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param EmailSendReceive $emailSendCheck |
|
244
|
|
|
* |
|
245
|
|
|
* @return Swift_Mime_MimePart |
|
246
|
|
|
*/ |
|
247
|
6 |
|
protected function buildMessage(EmailSendReceive $emailSendCheck) |
|
248
|
|
|
{ |
|
249
|
6 |
|
$message = Swift_Message::newInstance($emailSendCheck->getSubject()) |
|
250
|
6 |
|
->setFrom($emailSendCheck->getFrom()) |
|
251
|
6 |
|
->setTo($emailSendCheck->getTo()) |
|
252
|
6 |
|
->setBody($emailSendCheck->getBody()); |
|
253
|
|
|
|
|
254
|
6 |
|
return $message; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @param EmailSendReceive $emailSendCheck |
|
259
|
|
|
*/ |
|
260
|
3 |
|
private function saveEmailSendReceive(EmailSendReceive $emailSendCheck) |
|
261
|
|
|
{ |
|
262
|
3 |
|
$this->getEmailSendReceiveColl()->add($emailSendCheck); |
|
263
|
3 |
|
$this->getPersistCollection()->flush(); |
|
264
|
3 |
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @param EmailSendReceive $emailSendCheck |
|
268
|
|
|
* |
|
269
|
|
|
* @throws EmailSendCheckException |
|
270
|
|
|
*/ |
|
271
|
6 |
|
private function performSend(EmailSendReceive $emailSendCheck) |
|
272
|
|
|
{ |
|
273
|
6 |
|
$message = $this->buildMessage($emailSendCheck); |
|
274
|
|
|
|
|
275
|
|
|
try { |
|
276
|
6 |
|
$emailSendCheck->setSentAt(new DateTime()); |
|
277
|
6 |
|
$this->sendMessage($message, $emailSendCheck); |
|
|
|
|
|
|
278
|
2 |
|
$emailSendCheck->setStatus(EmailSendReceive::STATUS_SANDED); |
|
279
|
2 |
|
$this->saveEmailSendReceive($emailSendCheck); |
|
280
|
4 |
|
} catch (Swift_SwiftException $e) { |
|
281
|
1 |
|
$emailSendCheck->setStatus(EmailSendReceive::STATUS_SAND_ERROR); |
|
282
|
1 |
|
$this->saveEmailSendReceive($emailSendCheck); |
|
283
|
1 |
|
throw EmailSendCheckException::internalProblem($e); |
|
284
|
|
|
} |
|
285
|
2 |
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
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: