1 | <?php |
||
20 | class EmailSendCheck extends AbstractEmailCheck |
||
21 | { |
||
22 | const CHECK = 'email-send-check'; |
||
23 | const MESSAGE_BODY = 'This is a test, you don\'t need to reply this massage.'; |
||
24 | const SEND_INTERVAL = 600; |
||
25 | const SUBJECT_TEMPLATE = '%s:time:%d'; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | private $sendInterval; |
||
31 | |||
32 | /** |
||
33 | * @var Swift_Mailer $client |
||
34 | */ |
||
35 | private $mailer; |
||
36 | |||
37 | /** |
||
38 | * @var PersistCollectionInterface |
||
39 | */ |
||
40 | private $persistCollection; |
||
41 | |||
42 | /** |
||
43 | * @var EmailSendReceiveCollection |
||
44 | */ |
||
45 | private $emailSendReceiveCollection; |
||
46 | |||
47 | /** |
||
48 | * @var string; |
||
49 | */ |
||
50 | private $from; |
||
51 | |||
52 | /** |
||
53 | * @var string; |
||
54 | */ |
||
55 | private $toSubject; |
||
56 | |||
57 | /** |
||
58 | * @param string $checkNode |
||
59 | * @param Swift_Mailer $mailer |
||
60 | * @param string $from |
||
61 | * @param string $toSubjects |
||
62 | * @param int $sendInterval |
||
63 | */ |
||
64 | 8 | public function __construct( |
|
65 | $checkNode, |
||
66 | Swift_Mailer $mailer, |
||
67 | PersistCollectionInterface $persistCollection, |
||
68 | $from, |
||
69 | $toSubjects, |
||
70 | $sendInterval = self::SEND_INTERVAL |
||
71 | ) { |
||
72 | 8 | parent::__construct($checkNode); |
|
73 | |||
74 | 8 | $this->setMailer($mailer); |
|
75 | 8 | $this->setPersistCollection($persistCollection); |
|
76 | 8 | $this->setFrom($from); |
|
77 | 8 | $this->setToSubject($toSubjects); |
|
78 | 8 | $this->setSendInterval($sendInterval); |
|
79 | 8 | } |
|
80 | |||
81 | /** |
||
82 | * Check email can send and receive messages |
||
83 | * @return bool|void |
||
84 | * @throws EmailSendCheckException |
||
85 | */ |
||
86 | 7 | public function check() |
|
87 | { |
||
88 | 7 | $this->setEmailSendReceiveColl($this->getPersistCollection()->load()); |
|
89 | |||
90 | |||
91 | |||
92 | 7 | $lastSandedEmail = $this->getEmailSendReceiveColl()->count()-1 >=0 ? |
|
93 | 7 | $this->getEmailSendReceiveColl()->at( |
|
94 | 7 | $this->getEmailSendReceiveColl()->count()-1 |
|
95 | 7 | ) |
|
96 | 7 | :null; |
|
97 | 7 | if (null === $lastSandedEmail |
|
98 | 7 | || empty($lastSandedEmail->getSentAt()) |
|
99 | 7 | || (time() - $lastSandedEmail->getSentAt()->getTimestamp()) > $this->getSendInterval() |
|
100 | 7 | ) { |
|
101 | 6 | $emailSendCheck = $this->createEmailSendReceive(); |
|
102 | |||
103 | 6 | $this->performSend($emailSendCheck); |
|
104 | 2 | } |
|
105 | 3 | } |
|
106 | |||
107 | /** |
||
108 | * @return Swift_Mailer |
||
109 | */ |
||
110 | 6 | public function getMailer() |
|
111 | { |
||
112 | 6 | return $this->mailer; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return PersistCollectionInterface |
||
117 | */ |
||
118 | 7 | public function getPersistCollection() |
|
119 | { |
||
120 | 7 | return $this->persistCollection; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return EmailSendReceiveCollection |
||
125 | */ |
||
126 | 7 | public function getEmailSendReceiveColl() |
|
127 | { |
||
128 | 7 | return $this->emailSendReceiveCollection; |
|
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | 6 | public function getFrom() |
|
136 | { |
||
137 | 6 | return $this->from; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return string |
||
142 | */ |
||
143 | 6 | public function getToSubject() |
|
144 | { |
||
145 | 6 | return $this->toSubject; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * @return int |
||
150 | */ |
||
151 | 7 | public function getSendInterval() |
|
152 | { |
||
153 | 7 | return $this->sendInterval; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param Swift_Mailer $mailer |
||
158 | */ |
||
159 | 8 | protected function setMailer(Swift_Mailer $mailer) |
|
160 | { |
||
161 | 8 | $this->mailer = $mailer; |
|
162 | 8 | } |
|
163 | |||
164 | /** |
||
165 | * @param PersistCollectionInterface $persistCollection |
||
166 | */ |
||
167 | 8 | protected function setPersistCollection(PersistCollectionInterface $persistCollection) |
|
168 | { |
||
169 | 8 | $this->persistCollection = $persistCollection; |
|
170 | 8 | } |
|
171 | |||
172 | /** |
||
173 | * @param EmailSendReceiveCollection $emailSendReceiveC |
||
174 | */ |
||
175 | 7 | protected function setEmailSendReceiveColl(EmailSendReceiveCollection $emailSendReceiveC) |
|
176 | { |
||
177 | 7 | $this->emailSendReceiveCollection = $emailSendReceiveC; |
|
178 | 7 | } |
|
179 | |||
180 | /** |
||
181 | * @param string $from |
||
182 | */ |
||
183 | 8 | protected function setFrom($from) |
|
184 | { |
||
185 | 8 | $this->from = $from; |
|
186 | 8 | } |
|
187 | |||
188 | /** |
||
189 | * @param string $toSubject |
||
190 | */ |
||
191 | 8 | protected function setToSubject($toSubject) |
|
192 | { |
||
193 | 8 | $this->toSubject = $toSubject; |
|
194 | 8 | } |
|
195 | |||
196 | /** |
||
197 | * @return string |
||
198 | */ |
||
199 | 6 | protected function genEmailSubject() |
|
200 | { |
||
201 | 6 | return sprintf(static::SUBJECT_TEMPLATE, $this->getIndent(), date(DATE_RFC2822)); |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param int $sendInterval |
||
206 | */ |
||
207 | 8 | protected function setSendInterval($sendInterval) |
|
208 | { |
||
209 | 8 | $this->sendInterval = $sendInterval; |
|
210 | 8 | } |
|
211 | |||
212 | /** |
||
213 | * @return EmailSendReceive |
||
214 | */ |
||
215 | 6 | protected function createEmailSendReceive() |
|
216 | { |
||
217 | 6 | $emailSendCheck = new EmailSendReceive(); |
|
218 | |||
219 | 6 | $emailSendCheck->setFrom($this->getFrom()); |
|
220 | 6 | $emailSendCheck->setTo($this->getToSubject()); |
|
221 | 6 | $emailSendCheck->setBody(static::MESSAGE_BODY); |
|
222 | 6 | $emailSendCheck->setSubject($this->genEmailSubject()); |
|
223 | |||
224 | 6 | return $emailSendCheck; |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param Swift_Mime_Message $message |
||
229 | * @param EmailSendReceive $emailSendCheck |
||
230 | * @throws EmailSendCheckException |
||
231 | */ |
||
232 | 6 | protected function sendMessage(Swift_Mime_Message $message, EmailSendReceive $emailSendCheck) |
|
242 | |||
243 | /** |
||
244 | * @param EmailSendReceive $emailSendCheck |
||
245 | * @return Swift_Mime_MimePart |
||
246 | */ |
||
247 | 6 | protected function buildMessage(EmailSendReceive $emailSendCheck) |
|
256 | |||
257 | /** |
||
258 | * @param EmailSendReceive $emailSendCheck |
||
259 | */ |
||
260 | 3 | private function saveEmailSendReceive(EmailSendReceive $emailSendCheck) |
|
266 | |||
267 | /** |
||
268 | * @param EmailSendReceive $emailSendCheck |
||
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 | 6 | } catch (Swift_SwiftException $e) { |
|
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: