|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Incident\Siren\NotificationType; |
|
4
|
|
|
|
|
5
|
|
|
use Swift_Mailer; |
|
6
|
|
|
use Swift_Message; |
|
7
|
|
|
use TonicHealthCheck\Incident\IncidentInterface; |
|
8
|
|
|
use TonicHealthCheck\Incident\Siren\Subject\SubjectInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class EmailNotificationType. |
|
12
|
|
|
*/ |
|
13
|
|
|
class EmailNotificationType implements NotificationTypeInterface |
|
14
|
|
|
{ |
|
15
|
|
|
const EMAIL_BODY_T = "IncidentMessage:%s\nIncidentType:%s"; |
|
16
|
|
|
const EMAIL_SUBJECT_T = 'Health Check Incident:%s'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string; |
|
20
|
|
|
*/ |
|
21
|
|
|
private $fromName; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Swift_Mailer; |
|
25
|
|
|
*/ |
|
26
|
|
|
private $mailer; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string; |
|
30
|
|
|
*/ |
|
31
|
|
|
private $from; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* EmailNotificationType constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Swift_Mailer $mailer |
|
37
|
|
|
* @param string $from |
|
38
|
|
|
* @param string $fromName |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function __construct(Swift_Mailer $mailer, $from, $fromName) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->setMailer($mailer); |
|
43
|
1 |
|
$this->setFrom($from); |
|
44
|
1 |
|
$this->setFromName($fromName); |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param SubjectInterface $subject |
|
49
|
|
|
* @param IncidentInterface $incident |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function notify(SubjectInterface $subject, IncidentInterface $incident) |
|
52
|
|
|
{ |
|
53
|
1 |
|
if ($incident->getStatus() != IncidentInterface::STATUS_OK) { |
|
54
|
1 |
|
$message = Swift_Message::newInstance() |
|
55
|
1 |
|
->setTo($subject->getTarget()) |
|
56
|
1 |
|
->setFrom($this->getFrom(), $this->getFromName()) |
|
57
|
1 |
|
->setSubject(sprintf(self::EMAIL_SUBJECT_T, $incident->getIdent())) |
|
58
|
1 |
|
->setBody(sprintf(self::EMAIL_BODY_T, $incident->getMessage(), $incident->getType())); |
|
59
|
1 |
|
$this->getMailer()->send($message); |
|
60
|
1 |
|
$this->getMailer()->getTransport()->stop(); |
|
61
|
|
|
} |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return Swift_Mailer |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function getMailer() |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->mailer; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getFrom() |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->from; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getFromName() |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->fromName; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param Swift_Mailer $mailer |
|
90
|
|
|
*/ |
|
91
|
1 |
|
protected function setMailer($mailer) |
|
92
|
|
|
{ |
|
93
|
1 |
|
$this->mailer = $mailer; |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $from |
|
98
|
|
|
*/ |
|
99
|
1 |
|
protected function setFrom($from) |
|
100
|
|
|
{ |
|
101
|
1 |
|
$this->from = $from; |
|
102
|
1 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $fromName |
|
106
|
|
|
*/ |
|
107
|
1 |
|
protected function setFromName($fromName) |
|
108
|
|
|
{ |
|
109
|
1 |
|
$this->fromName = $fromName; |
|
110
|
1 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|