Node: try to avoid side-effects when...

...dealing with state mappings
This commit is contained in:
Thomas Gelf 2016-12-07 22:10:35 +01:00
parent a784b384f4
commit a7e9d815eb
2 changed files with 19 additions and 15 deletions

View file

@ -7,21 +7,21 @@ use Icinga\Module\Businessprocess\Web\Url;
class HostNode extends MonitoredNode class HostNode extends MonitoredNode
{ {
protected static $sortStateToStateMap = array( protected $sortStateToStateMap = array(
4 => self::ICINGA_DOWN, 4 => self::ICINGA_DOWN,
3 => self::ICINGA_UNREACHABLE, 3 => self::ICINGA_UNREACHABLE,
1 => self::ICINGA_PENDING, 1 => self::ICINGA_PENDING,
0 => self::ICINGA_UP 0 => self::ICINGA_UP
); );
protected static $stateToSortStateMap = array( protected $stateToSortStateMap = array(
self::ICINGA_PENDING => 1, self::ICINGA_PENDING => 1,
self::ICINGA_UNREACHABLE => 3, self::ICINGA_UNREACHABLE => 3,
self::ICINGA_DOWN => 4, self::ICINGA_DOWN => 4,
self::ICINGA_UP => 0, self::ICINGA_UP => 0,
); );
protected static $state_names = array( protected $stateNames = array(
'UP', 'UP',
'DOWN', 'DOWN',
'UNREACHABLE', 'UNREACHABLE',

View file

@ -22,7 +22,7 @@ abstract class Node
const ICINGA_UNREACHABLE = 2; const ICINGA_UNREACHABLE = 2;
const ICINGA_PENDING = 99; const ICINGA_PENDING = 99;
protected static $sortStateToStateMap = array( protected $sortStateToStateMap = array(
4 => self::ICINGA_CRITICAL, 4 => self::ICINGA_CRITICAL,
3 => self::ICINGA_UNKNOWN, 3 => self::ICINGA_UNKNOWN,
2 => self::ICINGA_WARNING, 2 => self::ICINGA_WARNING,
@ -30,7 +30,7 @@ abstract class Node
0 => self::ICINGA_OK 0 => self::ICINGA_OK
); );
protected static $stateToSortStateMap = array( protected $stateToSortStateMap = array(
self::ICINGA_PENDING => 1, self::ICINGA_PENDING => 1,
self::ICINGA_UNKNOWN => 3, self::ICINGA_UNKNOWN => 3,
self::ICINGA_CRITICAL => 4, self::ICINGA_CRITICAL => 4,
@ -94,7 +94,7 @@ abstract class Node
protected $className = 'unknown'; protected $className = 'unknown';
protected static $state_names = array( protected $stateNames = array(
'OK', 'OK',
'WARNING', 'WARNING',
'CRITICAL', 'CRITICAL',
@ -171,16 +171,17 @@ abstract class Node
public function getStateName($state = null) public function getStateName($state = null)
{ {
$states = $this->enumStateNames();
if ($state === null) { if ($state === null) {
return static::$state_names[ $this->getState() ]; return $states[ $this->getState() ];
} else { } else {
return static::$state_names[ $state ]; return $states[ $state ];
} }
} }
public function enumStateNames() public function enumStateNames()
{ {
return static::$state_names; return $this->stateNames;
} }
public function getState() public function getState()
@ -294,19 +295,22 @@ abstract class Node
protected function stateToSortState($state) protected function stateToSortState($state)
{ {
if (array_key_exists($state, static::$stateToSortStateMap)) { if (array_key_exists($state, $this->stateToSortStateMap)) {
return static::$stateToSortStateMap[$state]; return $this->stateToSortStateMap[$state];
} }
throw new ProgrammingError('Got invalid state: %s', var_export($state, 1)); throw new ProgrammingError(
'Got invalid state for node %s: %s',
$this->getName(),
var_export($state, 1) . var_export($this->stateToSortStateMap, 1)
);
} }
protected function sortStateTostate($sortState) protected function sortStateTostate($sortState)
{ {
$sortState = $sortState >> self::SHIFT_FLAGS; $sortState = $sortState >> self::SHIFT_FLAGS;
if (array_key_exists($sortState, $this->sortStateToStateMap)) {
if (array_key_exists($sortState, static::$sortStateToStateMap)) { return $this->sortStateToStateMap[$sortState];
return static::$sortStateToStateMap[$sortState];
} }
throw new ProgrammingError('Got invalid sorting state %s', $sortState); throw new ProgrammingError('Got invalid sorting state %s', $sortState);