Completed
Push — master ( 288ab5...55621d )
by Ma
02:54
created

Notifier::hasNotificationDelayExpired()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 9
nc 3
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A Notifier::notifyServices() 0 6 2
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);
0 ignored issues
show
Bug introduced by
It seems like $notification defined by $this->getNotificationById($notificationId) on line 84 can be null; however, Monitor\Notification\Notifier::parseNotification() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $notification defined by $this->prepareNotification($trigger, $serverData) on line 100 can be null; however, Monitor\Notification\Notifier::notifyServices() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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