Completed
Push — master ( 20d667...bbf4c7 )
by Lukas Kahwe
9s
created

SwiftMailerReporter::sendEmail()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 4
nc 5
nop 1
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
use ZendDiagnostics\Runner\Reporter\ReporterInterface;
12
13
/**
14
 * @author louis <[email protected]>
15
 */
16
class SwiftMailerReporter implements ReporterInterface
17
{
18
    private $mailer;
19
    private $recipient;
20
    private $subject;
21
    private $sender;
22
    private $sendOnWarning;
23
24
    /**
25
     * @param Swift_Mailer $mailer
26
     * @param string       $recipient
27
     * @param string       $sender
28
     * @param string       $subject
29
     * @param bool         $sendOnWarning
30
     */
31
    public function __construct(Swift_Mailer $mailer, $recipient, $sender, $subject, $sendOnWarning = true)
32
    {
33
        $this->mailer = $mailer;
34
        $this->recipient = $recipient;
35
        $this->sender = $sender;
36
        $this->subject = $subject;
37
        $this->sendOnWarning = $sendOnWarning;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function onStart(ArrayObject $checks, $runnerConfig)
44
    {
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function onBeforeRun(CheckInterface $check, $checkAlias = null)
51
    {
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null)
58
    {
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function onStop(ResultsCollection $results)
65
    {
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function onFinish(ResultsCollection $results)
72
    {
73
        if ($results->getUnknownCount() > 0) {
74
            $this->sendEmail($results);
75
76
            return;
77
        }
78
79
        if ($results->getWarningCount() > 0 && $this->sendOnWarning) {
80
            $this->sendEmail($results);
81
82
            return;
83
        }
84
85
        if ($results->getFailureCount() > 0) {
86
            $this->sendEmail($results);
87
88
            return;
89
        }
90
    }
91
92
    private function sendEmail(ResultsCollection $results)
93
    {
94
        $body = '';
95
96
        foreach ($results as $check) {
97
            /* @var $check  CheckInterface */
98
            /* @var $result ResultInterface */
99
            $result = isset($results[$check]) ? $results[$check] : null;
100
101
            if ($result instanceof ResultInterface) {
102
                $body .= sprintf("Check: %s\n", $check->getLabel());
103
                $body .= sprintf("Message: %s\n\n", $result->getMessage());
104
            }
105
        }
106
107
        $message = Swift_Message::newInstance()
108
            ->setSubject($this->subject)
109
            ->setFrom($this->sender)
110
            ->setTo($this->recipient)
111
            ->setBody($body);
112
113
        $this->mailer->send($message);
114
    }
115
}
116