|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Incident\Siren; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use SplSubject; |
|
7
|
|
|
use TonicHealthCheck\Incident\IncidentInterface; |
|
8
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\NotificationTypeInterface; |
|
9
|
|
|
use TonicHealthCheck\Incident\Siren\Subject\Subject; |
|
10
|
|
|
use TonicHealthCheck\Incident\Siren\Subject\SubjectCollection; |
|
11
|
|
|
use TonicHealthCheck\Incident\Siren\Subject\SubjectInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class IncidentSiren. |
|
15
|
|
|
*/ |
|
16
|
|
|
class IncidentSiren implements \SplObserver |
|
17
|
|
|
{ |
|
18
|
|
|
const FILE_NAME_ALARM_TRIGGER = 'alarm_triger.data'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var NotificationTypeInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $notificationTypeI; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* email or tel numbers. |
|
27
|
|
|
* |
|
28
|
|
|
* @var SubjectCollection |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $subjects; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* IncidentSiren constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param NotificationTypeInterface $notificationTypeI |
|
36
|
|
|
* @param null|SubjectCollection $subjects |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public function __construct(NotificationTypeInterface $notificationTypeI, SubjectCollection $subjects = null) |
|
39
|
|
|
{ |
|
40
|
3 |
|
$this->setNotificationTypeI($notificationTypeI); |
|
41
|
3 |
|
if (null === $subjects) { |
|
42
|
1 |
|
$subjects = new SubjectCollection(); |
|
43
|
|
|
} |
|
44
|
3 |
|
$this->setSubjects($subjects); |
|
45
|
3 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Receive update from subject. |
|
49
|
|
|
* |
|
50
|
|
|
* @link http://php.net/manual/en/splobserver.update.php |
|
51
|
|
|
* |
|
52
|
|
|
* @param SplSubject $subject <p> |
|
53
|
|
|
* The <b>SplSubject</b> notifying the observer of an update. |
|
54
|
|
|
* </p> |
|
55
|
|
|
* |
|
56
|
|
|
* @since 5.1.0 |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function update(SplSubject $subject) |
|
59
|
|
|
{ |
|
60
|
1 |
|
if ($subject instanceof IncidentInterface) { |
|
61
|
|
|
/* @var IncidentInterface $subject */ |
|
62
|
1 |
|
$this->notify($subject); |
|
63
|
|
|
} |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param IncidentInterface $incident |
|
68
|
|
|
* @param null|array $subjects |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function notify(IncidentInterface $incident, $subjects = null) |
|
71
|
|
|
{ |
|
72
|
2 |
|
$subjects = null === $subjects ? $this->getSubjects() : $subjects; |
|
73
|
|
|
/** @var SubjectInterface $subject */ |
|
74
|
2 |
|
foreach ($subjects as $subject) { |
|
75
|
|
|
try { |
|
76
|
2 |
|
$this->notifySubject($incident, $subject); |
|
77
|
1 |
|
} catch (Exception $e) { |
|
78
|
2 |
|
$this->showUserError($e); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return NotificationTypeInterface |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function getNotificationTypeI() |
|
87
|
|
|
{ |
|
88
|
2 |
|
return $this->notificationTypeI; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return SubjectCollection |
|
93
|
|
|
*/ |
|
94
|
3 |
|
public function getSubjects() |
|
95
|
|
|
{ |
|
96
|
3 |
|
return $this->subjects; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param SubjectCollection $subjects |
|
101
|
|
|
*/ |
|
102
|
3 |
|
protected function setSubjects(SubjectCollection $subjects) |
|
103
|
|
|
{ |
|
104
|
3 |
|
$this->subjects = $subjects; |
|
105
|
3 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param NotificationTypeInterface $notificationTypeI |
|
109
|
|
|
*/ |
|
110
|
3 |
|
protected function setNotificationTypeI(NotificationTypeInterface $notificationTypeI) |
|
111
|
|
|
{ |
|
112
|
3 |
|
$this->notificationTypeI = $notificationTypeI; |
|
113
|
3 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param IncidentInterface $incident |
|
117
|
|
|
* @param Subject $subject |
|
118
|
|
|
*/ |
|
119
|
2 |
|
protected function notifySubject(IncidentInterface $incident, $subject) |
|
120
|
|
|
{ |
|
121
|
2 |
|
if (null === $subject->getSchedule() || $subject->getSchedule()->isDue()) { |
|
122
|
2 |
|
$this->getNotificationTypeI()->notify($subject, $incident); |
|
123
|
|
|
} |
|
124
|
1 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param Exception $errorException |
|
128
|
|
|
*/ |
|
129
|
1 |
|
protected function showUserError(Exception $errorException) |
|
130
|
|
|
{ |
|
131
|
1 |
|
user_error($errorException->getMessage(), E_USER_WARNING); |
|
132
|
|
|
}// @codeCoverageIgnore |
|
133
|
|
|
} |
|
134
|
|
|
|