Notifier::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
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
        if (! $notification) {
86
            throw new \Exception('Notification not found');
87
        }
88
        //merge server data and trigger properties so we can use them in fulfilling notification message
89
        $data = array_merge($serverData, $trigger->toArray());
90
        $this->parseNotification($notification, $data);
91
        return $notification;
92
    }
93
94
    /**
95
     *
96
     *
97
     * @param  Trigger $trigger
98
     * @param  array   $serverData
99
     * @return \Monitor\Notification\Notification
100
     */
101
    public function triggerHasBeenFired(Trigger $trigger, array $serverData)
102
    {
103
        $notification = $this->prepareNotification($trigger, $serverData);
104
        $this->notifyServices($notification);
105
        return $notification;
106
    }
107
108
    /**
109
     * Send notification to notification service
110
     *
111
     * @access public
112
     * @param  Notification $notification
113
     * @return
114
     */
115
    public function notifyServices(Notification $notification)
116
    {
117
        foreach ($this->observers as $observer) {
118
            $observer->sendNotification($notification, $this->notificationData);
119
        }
120
    }
121
}
122