summaryrefslogtreecommitdiffstats
path: root/library/Director/CheckPlugin/Check.php
blob: d05f5a7fa9b899aabee0e1809149855ea8f8ea32 (plain)
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
<?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;
    }
}