Passed
Push — master ( 5520e8...3e77d8 )
by Dmitry
03:04
created

EmailSendCheck::check()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.2

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 20
ccs 12
cts 15
cp 0.8
rs 8.8571
cc 5
eloc 11
nc 4
nop 0
crap 5.2
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
 * @package TonicHealthCheck\Check\Email\Send
19
 */
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 ?
93 7
            $this->getEmailSendReceiveColl()->at(
94
                $this->getEmailSendReceiveColl()->count()-1
95
            )
96 7
            :null;
97 7
        if (null === $lastSandedEmail
98 7
            || empty($lastSandedEmail->getSentAt())
99
            || (time() - $lastSandedEmail->getSentAt()->getTimestamp()) > $this->getSendInterval()
100 7
        ) {
101 7
            $emailSendCheck = $this->createEmailSendReceive();
102
103 7
            $this->performSend($emailSendCheck);
104 3
        }
105 3
    }
106
107
    /**
108
     * @return Swift_Mailer
109
     */
110 7
    public function getMailer()
111
    {
112 7
        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 7
    public function getFrom()
136
    {
137 7
        return $this->from;
138
    }
139
140
    /**
141
     * @return string
142
     */
143 7
    public function getToSubject()
144
    {
145 7
        return $this->toSubject;
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function getSendInterval()
152
    {
153
        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 7
    protected function genEmailSubject()
200
    {
201 7
        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 7
    protected function createEmailSendReceive()
216
    {
217 7
        $emailSendCheck = new EmailSendReceive();
218
219 7
        $emailSendCheck->setFrom($this->getFrom());
220 7
        $emailSendCheck->setTo($this->getToSubject());
221 7
        $emailSendCheck->setBody(static::MESSAGE_BODY);
222 7
        $emailSendCheck->setSubject($this->genEmailSubject());
223
224 7
        return $emailSendCheck;
225
    }
226
227
    /**
228
     * @param Swift_Mime_Message $message
229
     * @param EmailSendReceive   $emailSendCheck
230
     * @throws EmailSendCheckException
231
     */
232 7
    protected function sendMessage(Swift_Mime_Message $message, EmailSendReceive $emailSendCheck)
233
    {
234 7
        $failedRecipients = [];
235 7
        $numSent = $this->getMailer()->send($message, $failedRecipients);
236 5
        $this->getMailer()->getTransport()->stop();
237 5
        if (!$numSent) {
238 2
            $emailSendCheck->setStatus(EmailSendReceive::STATUS_SAND_ERROR);
239 2
            throw EmailSendCheckException::doesNotSendMessage(array_keys($failedRecipients));
240
        }
241 3
    }
242
243
    /**
244
     * @param EmailSendReceive $emailSendCheck
245
     * @return Swift_Mime_MimePart
246
     */
247 7
    protected function buildMessage(EmailSendReceive $emailSendCheck)
248
    {
249 7
        $message = Swift_Message::newInstance($emailSendCheck->getSubject())
250 7
            ->setFrom($emailSendCheck->getFrom())
251 7
            ->setTo($emailSendCheck->getTo())
252 7
            ->setBody($emailSendCheck->getBody());
253
254 7
        return $message;
255
    }
256
257
    /**
258
     * @param EmailSendReceive $emailSendCheck
259
     */
260 4
    private function saveEmailSendReceive(EmailSendReceive $emailSendCheck)
261
    {
262
263 4
        $this->getEmailSendReceiveColl()->add($emailSendCheck);
264 4
        $this->getPersistCollection()->flush();
265 4
    }
266
267
    /**
268
     * @param EmailSendReceive $emailSendCheck
269
     * @throws EmailSendCheckException
270
     */
271 7
    private function performSend(EmailSendReceive $emailSendCheck)
272
    {
273 7
        $message = $this->buildMessage($emailSendCheck);
274
275
        try {
276 7
            $emailSendCheck->setSentAt(new DateTime());
277 7
            $this->sendMessage($message, $emailSendCheck);
0 ignored issues
show
Documentation introduced by
$message is of type object<Swift_Mime_MimePart>, but the function expects a object<Swift_Mime_Message>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
278 3
            $emailSendCheck->setStatus(EmailSendReceive::STATUS_SANDED);
279 3
            $this->saveEmailSendReceive($emailSendCheck);
280 7
        } 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 3
    }
286
}
287