Completed
Pull Request — master (#219)
by Markus
11:05
created

SwiftMailerReporter::onBeforeRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Liip\MonitorBundle\Helper;
4
5
use ArrayObject;
6
use Swift_Mailer;
7
use Swift_Message;
8
use ZendDiagnostics\Check\CheckInterface;
9
use ZendDiagnostics\Result\Collection as ResultsCollection;
10
use ZendDiagnostics\Result\ResultInterface;
11
12
/**
13
 * @author louis <[email protected]>
14
 */
15
class SwiftMailerReporter extends AbstractMailReporter
16
{
17
    private $mailer;
18
19
    /**
20
     * @param Swift_Mailer $mailer
21
     * @param string|array $recipients
22
     * @param string       $sender
23
     * @param string       $subject
24
     * @param bool         $sendOnWarning
25
     */
26
    public function __construct(Swift_Mailer $mailer, $recipients, $sender, $subject, $sendOnWarning = true)
27
    {
28
        $this->mailer = $mailer;
29
30
        parent::__construct($recipients, $sender, $subject, $sendOnWarning);
31
    }
32
33
    protected function sendEmail(ResultsCollection $results)
34
    {
35
        $body = '';
36
37
        foreach ($results as $check) {
38
            /* @var $check  CheckInterface */
39
            /* @var $result ResultInterface */
40
            $result = $results[$check] ?? null;
41
42
            if ($result instanceof ResultInterface) {
43
                $body .= sprintf("Check: %s\n", $check->getLabel());
44
                $body .= sprintf("Message: %s\n\n", $result->getMessage());
45
            }
46
        }
47
48
        $message = (new Swift_Message())
49
            ->setSubject($this->subject)
50
            ->setFrom($this->sender)
51
            ->setTo($this->recipients)
52
            ->setBody($body);
53
54
        $this->mailer->send($message);
55
    }
56
}
57