Completed
Push — master ( 17bcec...d36846 )
by Dmitry
02:23
created

IncidentSiren::notifySubject()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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