-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlertStates.php
124 lines (109 loc) · 2.65 KB
/
AlertStates.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Monitoring;
use Monitoring\State\StateInterface;
use Monitoring\State\StateAbstract;
use Monitoring\State\StateFactory;
use Monitoring\Handler\HandlerInterface;
/**
* Collect States and delegate to each State evens
*
* Class AlertStateList
* @package Monitoring
*/
class AlertStates extends StateAbstract
{
/**
* @var HandlerInterface
*/
protected $_handler;
/**
* @var array of StateInterface
*/
protected $_states = array();
/**
* @param HandlerInterface $handler
* @param array $states
* @param string $base_path
*/
public function __construct( HandlerInterface $handler, $states = array(), $base_path = __DIR__ )
{
$this->_handler = $handler;
if ( count($states) > 0 ) {
foreach ($states as $stateConfig) {
if (isset($stateConfig['params'])) {
$stateConfig['params']['base_path'] = $base_path;
} else {
$stateConfig['params'] = array('base_path' => $base_path);
}
$state = StateFactory::getInstance()->createByConfig( $this->_handler, $stateConfig );
if ( $state instanceof StateInterface ) {
$this->addState( $state );
}
}
}
}
/**
* Doing errors verify of each items
*/
public function verifyError()
{
if ( $this->count() > 0 ) {
foreach($this->getStates() as $state) {
$state->verifyError();
}
}
}
/**
* Calculate states count
*
* @return int
*/
public function count()
{
return count($this->getStates());
}
/**
* Return all states
*
* @return array
*/
public function getStates()
{
return $this->_states;
}
/**
* Add State
*
* @param StateInterface $state
*/
public function addState( StateInterface $state )
{
if ( !$this->isState($state) ) {
$this->_states[ get_class($state) ] = $state;
}
}
/**
* Remove state
*
* @param StateInterface $state
*/
public function removeState( StateInterface $state )
{
if ( $this->isState($state) ) {
unset( $this->_states[ get_class($state) ] );
}
}
/**
* Check if state set
*
* @param {StateInterface|string} $state
* @return bool
*/
public function isState( $state )
{
if ($state instanceof StateInterface) {
$state = get_class($state);
}
return isset( $this->_states[ $state ] );
}
}