Passed
Push — master ( 95d545...35bca5 )
by Dmitry
04:09
created

EmailSendCheck::setPersistCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
use TonicHealthCheck\Check\Email\Entity\EmailSendReceiveCollection;
15
use TonicHealthCheck\Check\Email\Persist\PersistCollectionInterface;
16
17
/**
18
 * Class EmailSendCheck
19
 * @package TonicHealthCheck\Check\Email\Send
20
 */
21
class EmailSendCheck extends AbstractEmailCheck
22
{
23
    const CHECK = 'email-send-check';
24
    const MESSAGE_BODY = 'This is a test, you don\'t need to reply this massage.';
25
    const SEND_INTERVAL = 600;
26
    const SUBJECT_TEMPLATE = '%s:time:%d';
27
28
    /**
29
     * @var int
30
     */
31
    private $sendInterval;
32
33
    /**
34
     * @var Swift_Mailer $client
35
     */
36
    private $mailer;
37
38
    /**
39
     * @var PersistCollectionInterface
40
     */
41
    private $persistCollection;
42
43
    /**
44
     * @var EmailSendReceiveCollection
45
     */
46
    private $emailSendReceiveCollection;
47
48
    /**
49
     * @var string;
50
     */
51
    private $from;
52
53
    /**
54
     * @var string;
55
     */
56
    private $toSubject;
57
58
    /**
59
     * @param string        $checkNode
60
     * @param Swift_Mailer  $mailer
61
     * @param EntityManager $doctrine
0 ignored issues
show
Bug introduced by
There is no parameter named $doctrine. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     * @param string        $from
63
     * @param string        $toSubjects
64
     * @param int           $sendInterval
65
     */
66 8
    public function __construct(
67
        $checkNode,
68
        Swift_Mailer $mailer,
69
        PersistCollectionInterface $persistCollection,
70
        $from,
71
        $toSubjects,
72
        $sendInterval = self::SEND_INTERVAL
73
    ) {
74 8
        parent::__construct($checkNode);
75
76 8
        $this->setMailer($mailer);
77 8
        $this->setPersistCollection($persistCollection);
78 8
        $this->setFrom($from);
79 8
        $this->setToSubject($toSubjects);
80 8
        $this->setSendInterval($sendInterval);
81 8
    }
82
83
    /**
84
     * Check email can send and receive messages
85
     * @return bool|void
86
     * @throws EmailSendCheckException
87
     */
88 7
    public function check()
89
    {
90 7
        $this->setEmailSendReceiveColl($this->getPersistCollection()->load());
91
92
93 7
        $lastSandedEmail = $this->getEmailSendReceiveColl()->at(
94 7
            $this->getEmailSendReceiveColl()->count()-1
95 7
        );
96 7
        if (null === $lastSandedEmail
97 7
            || empty($lastSandedEmail->getSentAt())
98 7
            || (time() - $lastSandedEmail->getSentAt()->getTimestamp()) > $this->getSendInterval()
99 7
        ) {
100 7
            $emailSendCheck = $this->createEmailSendReceive();
101
102 7
            $this->performSend($emailSendCheck);
103 3
        }
104 3
    }
105
106
    /**
107
     * @return Swift_Mailer
108
     */
109 7
    public function getMailer()
110
    {
111 7
        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
    /**
132
     * @return string
133
     */
134 7
    public function getFrom()
135
    {
136 7
        return $this->from;
137
    }
138
139
    /**
140
     * @return string
141
     */
142 7
    public function getToSubject()
143
    {
144 7
        return $this->toSubject;
145
    }
146
147
    /**
148
     * @return int
149
     */
150 7
    public function getSendInterval()
151
    {
152 7
        return $this->sendInterval;
153
    }
154
155
    /**
156
     * @param Swift_Mailer $mailer
157
     */
158 8
    protected function setMailer(Swift_Mailer $mailer)
159
    {
160 8
        $this->mailer = $mailer;
161 8
    }
162
163
    /**
164
     * @param PersistCollectionInterface $persistCollection
165
     */
166 8
    protected function setPersistCollection(PersistCollectionInterface $persistCollection)
167
    {
168 8
        $this->persistCollection = $persistCollection;
169 8
    }
170
171
    /**
172
     * @param EmailSendReceiveCollection $emailSendReceiveC
173
     */
174 7
    protected function setEmailSendReceiveColl(EmailSendReceiveCollection $emailSendReceiveC)
175
    {
176 7
        $this->emailSendReceiveCollection = $emailSendReceiveC;
177 7
    }
178
179
    /**
180
     * @param string $from
181
     */
182 8
    protected function setFrom($from)
183
    {
184 8
        $this->from = $from;
185 8
    }
186
187
    /**
188
     * @param string $toSubject
189
     */
190 8
    protected function setToSubject($toSubject)
191
    {
192 8
        $this->toSubject = $toSubject;
193 8
    }
194
195
    /**
196
     * @return string
197
     */
198 7
    protected function genEmailSubject()
199
    {
200 7
        return sprintf(static::SUBJECT_TEMPLATE, $this->getIndent(), date(DATE_RFC2822));
201
    }
202
203
    /**
204
     * @param int $sendInterval
205
     */
206 8
    protected function setSendInterval($sendInterval)
207
    {
208 8
        $this->sendInterval = $sendInterval;
209 8
    }
210
211
    /**
212
     * @return EmailSendReceive
213
     */
214 7
    protected function createEmailSendReceive()
215
    {
216 7
        $emailSendCheck = new EmailSendReceive();
217
218 7
        $emailSendCheck->setFrom($this->getFrom());
219 7
        $emailSendCheck->setTo($this->getToSubject());
220 7
        $emailSendCheck->setBody(static::MESSAGE_BODY);
221
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 3
    private function saveEmailSendReceive(EmailSendReceive $emailSendCheck)
261
    {
262
263 3
        $this->getEmailSendReceiveColl()->add($emailSendCheck);
264 3
        $this->getPersistCollection()->flush();
265 3
    }
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
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
279 3
            $emailSendCheck->setStatus(EmailSendReceive::STATUS_SANDED);
280 3
            $emailSendCheck->setSentAt(new DateTime());
281
282 3
            $this->saveEmailSendReceive($emailSendCheck);
283 7
        } catch (Swift_SwiftException $e) {
284
285 1
            $emailSendCheck->setStatus(EmailSendReceive::STATUS_SAND_ERROR);
286
287 1
            throw EmailSendCheckException::internalProblem($e);
288
        }
289 3
    }
290
}
291