1
|
|
|
<?php |
2
|
|
|
namespace Monitor\Notification; |
3
|
|
|
|
4
|
|
|
use Monitor\Model\Notification; |
5
|
|
|
use Monitor\Model\Trigger; |
6
|
|
|
use Monitor\Service\NotificationLog as NotificationLogService; |
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
|
9
|
|
|
class Notifier extends Observable |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var \Monitor\Notification\Parser |
14
|
|
|
*/ |
15
|
|
|
private $notificationParser; |
16
|
|
|
/** |
17
|
|
|
* @var integer |
18
|
|
|
*/ |
19
|
|
|
private $notificationDelayInHours; |
20
|
|
|
/** |
21
|
|
|
* @var \Monitor\Service\NotificationLog |
22
|
|
|
*/ |
23
|
|
|
private $notificationLogService; |
24
|
|
|
/** |
25
|
|
|
* Notification repository |
26
|
|
|
* @var \Doctrine\ORM\EntityRepository |
27
|
|
|
*/ |
28
|
|
|
private $repository; |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $notificationData; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
Parser $notificationParser, |
36
|
|
|
$notificationDelay, |
37
|
|
|
NotificationLogService $notificationLogService, |
38
|
|
|
EntityRepository $repository |
39
|
|
|
) { |
40
|
|
|
|
41
|
|
|
$this->notificationParser = $notificationParser; |
42
|
|
|
$this->notificationDelayInHours = $notificationDelay; |
43
|
|
|
$this->notificationLogService = $notificationLogService; |
44
|
|
|
$this->repository = $repository; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add notification data, we will use them in notification services |
49
|
|
|
* |
50
|
|
|
* @param array $data |
51
|
|
|
*/ |
52
|
|
|
public function setNotificationData(array $data) |
53
|
|
|
{ |
54
|
|
|
$this->notificationData = $data; |
55
|
|
|
} |
56
|
|
|
/** |
57
|
|
|
* Get Notification by id |
58
|
|
|
* |
59
|
|
|
* @param int $id |
60
|
|
|
* @return \Monitor\Model\Notification $notification |
61
|
|
|
*/ |
62
|
|
|
private function getNotificationById($id) |
63
|
|
|
{ |
64
|
|
|
$notification = $this->repository->find($id); |
65
|
|
|
return $notification; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Parse notification message |
70
|
|
|
* |
71
|
|
|
* @param \Monitor\Model\Notification $notification |
72
|
|
|
* @param array $data |
73
|
|
|
*/ |
74
|
|
|
private function parseNotification(Notification $notification, $data) |
75
|
|
|
{ |
76
|
|
|
$this->notificationParser->parse($notification, $data); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Prepare notification |
81
|
|
|
* |
82
|
|
|
* @access private |
83
|
|
|
* @param Trigger $trigger |
84
|
|
|
* @param array $serverData |
85
|
|
|
* @return \Monitor\Notification\Notification |
86
|
|
|
*/ |
87
|
|
|
private function prepareNotification(Trigger $trigger, array $serverData) |
88
|
|
|
{ |
89
|
|
|
$notificationId = $trigger->getNotificationId(); |
90
|
|
|
$notification = $this->getNotificationById($notificationId); |
91
|
|
|
//merge server data and trigger properties so we can use them in fulfilling notification message |
92
|
|
|
$data = array_merge($serverData, $trigger->toArray()); |
93
|
|
|
$this->parseNotification($notification, $data); |
|
|
|
|
94
|
|
|
return $notification; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check if same type of notification for concret server has been sent already |
99
|
|
|
* |
100
|
|
|
* @access private |
101
|
|
|
* @param int $triggerId |
102
|
|
|
* @param int $serverId |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function hasNotificationDelayExpired($triggerId, $serverId, $msDelay) |
106
|
|
|
{ |
107
|
|
|
$queryResult = $this->notificationLogService->getLastForTrigger( |
108
|
|
|
$triggerId, |
109
|
|
|
$serverId |
110
|
|
|
); |
111
|
|
|
if ( ! $queryResult) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
$timeOfLastFiredUpTrigger = $queryResult[0]['created']; |
115
|
|
|
$timeDiff = $timeOfLastFiredUpTrigger - time(); |
116
|
|
|
return ($this->notificationDelayInHours * $msDelay + $timeDiff >= 0) ? false : true; |
117
|
|
|
} |
118
|
|
|
/** |
119
|
|
|
* |
120
|
|
|
* |
121
|
|
|
* @param Trigger $trigger |
122
|
|
|
* @param array $serverData |
123
|
|
|
* @return \Monitor\Notification\Notification |
124
|
|
|
*/ |
125
|
|
|
public function triggerHasBeenFired(Trigger $trigger, array $serverData) |
126
|
|
|
{ |
127
|
|
|
$notification = $this->prepareNotification($trigger, $serverData); |
128
|
|
|
$this->notifyServices($notification); |
|
|
|
|
129
|
|
|
return $notification; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Send notification to notification service |
134
|
|
|
* |
135
|
|
|
* @access public |
136
|
|
|
* @param Notification $notification |
137
|
|
|
* @return |
138
|
|
|
*/ |
139
|
|
|
public function notifyServices(Notification $notification) |
140
|
|
|
{ |
141
|
|
|
foreach ($this->observers as $observer) { |
142
|
|
|
$observer->sendNotification($notification, $this->notificationData); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: