TriggerMgr::checkIsComparatorValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
namespace Monitor\Notification\Trigger;
3
4
use Monitor\Model\Notification;
5
use Monitor\Notification\Notifier;
6
use Monitor\Notification\Trigger\Comparator\Comparator;
7
use Monitor\Notification\Trigger\Comparator\Strategy\Context as StrategyContext;
8
use Monitor\Utils\PercentageHelper;
9
use Monitor\Model\Trigger;
10
use Monitor\Model\NotificationLog;
11
use Monitor\Service\NotificationLog as NotificationLogService;
12
use Doctrine\ORM\EntityRepository;
13
14
class TriggerMgr
15
{
16
    
17
    /**
18
     * @var \Monitor\Model\Trigger
19
     */
20
    private $triggers;
21
    /**
22
     * @var \Monitor\Notification\Trigger\Comparator\Comparator
23
     */
24
    private $comparator;
25
    /**
26
     * @var \Monitor\Notification\Notifier
27
     */
28
    private $notifier;
29
    /**
30
     * @var \Monitor\Utils\PercentageHelper
31
     */
32
    private $percentageHelper;
33
    /**
34
     * @var \Doctrine\ORM\EntityRepository
35
     */
36
    private $serviceRepository;
37
    /**
38
     * @var \Monitor\Service\NotificationLog
39
     */
40
    private $notificationLogService;
41
42
    public function __construct(
43
        Notifier $notifier,
44
        PercentageHelper $percentageHelper,
45
        EntityRepository $triggerRepository,
46
        EntityRepository $serviceRepository,
47
        NotificationLogService $notificationLogService,
48
        Comparator $comparator
49
    ) {
50
        $this->notifier = $notifier;
51
        $this->percentageHelper = $percentageHelper;
52
        $this->triggers = $triggerRepository->findAll();
0 ignored issues
show
Documentation Bug introduced by
It seems like $triggerRepository->findAll() of type array is incompatible with the declared type object<Monitor\Model\Trigger> of property $triggers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
        $this->serviceRepository = $serviceRepository;
54
        $this->notificationLogService = $notificationLogService;
55
        $this->comparator = $comparator;
56
    }
57
58
    /**
59
     * Check if triggers should be fired up
60
     *
61
     * @access public
62
     * @param  array $serverData
63
     */
64
    public function checkTriggers(array $serverData, $msDelay)
65
    {
66
        foreach ($this->triggers as $trigger) {
0 ignored issues
show
Bug introduced by
The expression $this->triggers of type object<Monitor\Model\Trigger> is not traversable.
Loading history...
67
            if ($this->shouldTriggerBeFired($trigger, $serverData, $msDelay)) {
68
                $this->fireTrigger($trigger, $serverData);
69
            }
70
        }
71
    }
72
73
    /**
74
     * Check if trigger meet conditions to be fired
75
     *
76
     * @access public
77
     * @param  Trigger $trigger
78
     * @param  array   $serverData
79
     * @param  int     $msDelay
80
     * @return boolean
81
     */
82
    public function shouldTriggerBeFired(Trigger $trigger, array $serverData, $msDelay)
83
    {
84
        $this->checkIsComparatorValid();
85
        if (! $this->notificationLogService->hasNotificationDelayExpired(
86
            $trigger->getId(),
87
            $serverData['server_id'],
88
            $msDelay
89
        )) {
90
            return false;
91
        }
92
        $strategy = new StrategyContext($trigger->getType());
93
        return $strategy->compare(
94
            $trigger,
95
            $serverData,
96
            $this->serviceRepository,
97
            $this->percentageHelper,
98
            $this->comparator
99
        );
100
    }
101
102
    /**
103
     * Check comparator validity
104
     *
105
     * @access private
106
     * @throws \Exception if comparator is not valid
107
     */
108
    private function checkIsComparatorValid()
109
    {
110
        if (! $this->comparator) {
111
            throw new \Exception('Comparator invalid');
112
        }
113
    }
114
115
    /**
116
     * Firing trigger
117
     *
118
     * @access private
119
     * @param  Trigger $trigger
120
     * @param  array   $serverData
121
     * @return boolean
122
     */
123
    private function fireTrigger(Trigger $trigger, array $serverData)
124
    {
125
        $notification = $this->notifier->triggerHasBeenFired($trigger, $serverData);
126
        $log = new NotificationLog;
127
        $log->setTriggerId($trigger->getId());
128
        $log->setServerId($serverData['server_id']);
129
        $log->setMessage($notification->getMessage());
130
        $log->setCreated(time());
131
        $this->notificationLogService->save($log);
132
        return true;
133
    }
134
}
135