|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Reporter; |
|
13
|
|
|
|
|
14
|
|
|
use Swift_Mailer; |
|
15
|
|
|
use Swift_Message; |
|
16
|
|
|
use ZendDiagnostics\Check\CheckInterface; |
|
17
|
|
|
use ZendDiagnostics\Result\Collection as ResultsCollection; |
|
18
|
|
|
use ZendDiagnostics\Result\ResultInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
|
|
|
|
|
21
|
|
|
* @author louis <[email protected]>, Vladimir Turnaev <[email protected]> |
|
|
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
|
|
23
|
|
|
class Mailer extends ReporterAbstract |
|
24
|
|
|
{ |
|
25
|
|
|
protected $mailer; |
|
26
|
|
|
protected $recipient; |
|
27
|
|
|
protected $subject; |
|
28
|
|
|
protected $sender; |
|
29
|
|
|
protected $sendOnWarning; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* @param string $recipient |
|
|
|
|
|
|
33
|
|
|
* @param string $sender |
|
|
|
|
|
|
34
|
|
|
* @param string $subject |
|
|
|
|
|
|
35
|
|
|
* @param bool $sendOnWarning |
|
|
|
|
|
|
36
|
|
|
*/ |
|
37
|
8 |
|
public function __construct(Swift_Mailer $mailer, $recipient, $sender, $subject, $sendOnWarning = true) |
|
38
|
|
|
{ |
|
39
|
8 |
|
$this->mailer = $mailer; |
|
40
|
8 |
|
$this->recipient = $recipient; |
|
41
|
8 |
|
$this->sender = $sender; |
|
42
|
8 |
|
$this->subject = $subject; |
|
43
|
8 |
|
$this->sendOnWarning = $sendOnWarning; |
|
44
|
8 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
|
|
|
|
|
49
|
7 |
|
public function onFinish(ResultsCollection $results) |
|
50
|
|
|
{ |
|
51
|
7 |
|
parent::onFinish($results); |
|
52
|
|
|
|
|
53
|
7 |
|
if ($results->getUnknownCount() > 0) { |
|
54
|
1 |
|
$this->sendEmail($results); |
|
55
|
|
|
|
|
56
|
1 |
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
6 |
|
if ($results->getWarningCount() > 0 && $this->sendOnWarning) { |
|
60
|
1 |
|
$this->sendEmail($results); |
|
61
|
|
|
|
|
62
|
1 |
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
5 |
|
if ($results->getFailureCount() > 0) { |
|
66
|
2 |
|
$this->sendEmail($results); |
|
67
|
|
|
|
|
68
|
2 |
|
return; |
|
69
|
|
|
} |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
private function sendEmail(ResultsCollection $results) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
4 |
|
$body = ''; |
|
75
|
|
|
|
|
76
|
4 |
|
foreach ($results as $check) { |
|
77
|
|
|
/* @var $check CheckInterface */ |
|
78
|
|
|
/* @var $result ResultInterface */ |
|
79
|
4 |
|
$result = $results[$check] ?? null; |
|
80
|
|
|
|
|
81
|
4 |
|
if ($result instanceof ResultInterface) { |
|
82
|
4 |
|
$body .= sprintf("Check: %s\n", $check->getLabel()); |
|
83
|
4 |
|
$body .= sprintf("Message: %s\n\n", $result->getMessage()); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
$message = (new Swift_Message()) |
|
88
|
4 |
|
->setSubject($this->subject) |
|
89
|
4 |
|
->setFrom($this->sender) |
|
90
|
4 |
|
->setTo($this->recipient) |
|
91
|
4 |
|
->setBody($body); |
|
92
|
|
|
|
|
93
|
4 |
|
$this->mailer->send($message); |
|
94
|
4 |
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|