diff options
Diffstat (limited to 'library/Director/CheckPlugin/Check.php')
-rw-r--r-- | library/Director/CheckPlugin/Check.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/library/Director/CheckPlugin/Check.php b/library/Director/CheckPlugin/Check.php new file mode 100644 index 0000000..d05f5a7 --- /dev/null +++ b/library/Director/CheckPlugin/Check.php @@ -0,0 +1,59 @@ +<?php + +namespace Icinga\Module\Director\CheckPlugin; + +use Exception; + +class Check extends CheckResults +{ + public function call(callable $check, $errorState = 'CRITICAL') + { + try { + $check(); + } catch (Exception $e) { + $this->fail($e, $errorState); + } + + return $this; + } + + public function assertTrue($check, $message, $errorState = 'CRITICAL') + { + if ($this->makeBool($check, $message) === true) { + $this->succeed($message); + } else { + $this->fail($message, $errorState); + } + + return $this; + } + + public function assertFalse($check, $message, $errorState = 'CRITICAL') + { + if ($this->makeBool($check, $message) === false) { + $this->succeed($message); + } else { + $this->fail($message, $errorState); + } + + return $this; + } + + protected function makeBool($check, &$message) + { + if (is_callable($check)) { + try { + $check = $check(); + } catch (Exception $e) { + $message .= ': ' . $e->getMessage(); + return false; + } + } + + if (! is_bool($check)) { + return null; + } + + return $check; + } +} |