|
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 \Monitor\Service\NotificationLog |
|
18
|
|
|
*/ |
|
19
|
|
|
private $notificationLogService; |
|
20
|
|
|
/** |
|
21
|
|
|
* Notification repository |
|
22
|
|
|
* @var \Doctrine\ORM\EntityRepository |
|
23
|
|
|
*/ |
|
24
|
|
|
private $repository; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $notificationData; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
Parser $notificationParser, |
|
32
|
|
|
NotificationLogService $notificationLogService, |
|
33
|
|
|
EntityRepository $repository |
|
34
|
|
|
) { |
|
35
|
|
|
|
|
36
|
|
|
$this->notificationParser = $notificationParser; |
|
37
|
|
|
$this->notificationLogService = $notificationLogService; |
|
38
|
|
|
$this->repository = $repository; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Add notification data, we will use them in notification services |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $data |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setNotificationData(array $data) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->notificationData = $data; |
|
49
|
|
|
} |
|
50
|
|
|
/** |
|
51
|
|
|
* Get Notification by id |
|
52
|
|
|
* |
|
53
|
|
|
* @param int $id |
|
54
|
|
|
* @return \Monitor\Model\Notification $notification |
|
55
|
|
|
*/ |
|
56
|
|
|
private function getNotificationById($id) |
|
57
|
|
|
{ |
|
58
|
|
|
$notification = $this->repository->find($id); |
|
59
|
|
|
return $notification; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Parse notification message |
|
64
|
|
|
* |
|
65
|
|
|
* @param \Monitor\Model\Notification $notification |
|
66
|
|
|
* @param array $data |
|
67
|
|
|
*/ |
|
68
|
|
|
private function parseNotification(Notification $notification, $data) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->notificationParser->parse($notification, $data); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Prepare notification |
|
75
|
|
|
* |
|
76
|
|
|
* @access private |
|
77
|
|
|
* @param Trigger $trigger |
|
78
|
|
|
* @param array $serverData |
|
79
|
|
|
* @return \Monitor\Notification\Notification |
|
80
|
|
|
*/ |
|
81
|
|
|
private function prepareNotification(Trigger $trigger, array $serverData) |
|
82
|
|
|
{ |
|
83
|
|
|
$notificationId = $trigger->getNotificationId(); |
|
84
|
|
|
$notification = $this->getNotificationById($notificationId); |
|
85
|
|
|
//merge server data and trigger properties so we can use them in fulfilling notification message |
|
86
|
|
|
$data = array_merge($serverData, $trigger->toArray()); |
|
87
|
|
|
$this->parseNotification($notification, $data); |
|
|
|
|
|
|
88
|
|
|
return $notification; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* |
|
94
|
|
|
* @param Trigger $trigger |
|
95
|
|
|
* @param array $serverData |
|
96
|
|
|
* @return \Monitor\Notification\Notification |
|
97
|
|
|
*/ |
|
98
|
|
|
public function triggerHasBeenFired(Trigger $trigger, array $serverData) |
|
99
|
|
|
{ |
|
100
|
|
|
$notification = $this->prepareNotification($trigger, $serverData); |
|
101
|
|
|
$this->notifyServices($notification); |
|
|
|
|
|
|
102
|
|
|
return $notification; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Send notification to notification service |
|
107
|
|
|
* |
|
108
|
|
|
* @access public |
|
109
|
|
|
* @param Notification $notification |
|
110
|
|
|
* @return |
|
111
|
|
|
*/ |
|
112
|
|
|
public function notifyServices(Notification $notification) |
|
113
|
|
|
{ |
|
114
|
|
|
foreach ($this->observers as $observer) { |
|
115
|
|
|
$observer->sendNotification($notification, $this->notificationData); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
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: