Passed
Branch master (899b2b)
by Bret R.
02:52
created

CallbackCheck::checkStatus()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 14
rs 9.9332
1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
6
class CallbackCheck extends AbstractCheck
7
{
8
9
    /**
10
     * @var callable
11
     */
12
    protected $callback;
13
14
    /**
15
     * Constructor
16
     *
17
     * @param string $label
18
     * @param callable $callback
19
     */
20
    public function __construct(string $label, callable $callback)
21
    {
22
        parent::__construct($label);
23
        $this->callback = $callback;
24
    }
25
26
    /**
27
     * Check callback
28
     *
29
     * @return Result
30
     */
31
    public function checkStatus(): Result
32
    {
33
        $result = new Result($this->label);
34
        try {
35
            $return = \call_user_func($this->callback, $this->label);
36
            if ($return instanceof Result) {
37
                $result = $return;
38
            } else if ($return !== true) {
39
                $result->setError($return);
40
            }
41
        } catch (\Throwable $e) {
42
            $result->setError($e->getMessage());
43
        }
44
        return $result;
45
    }
46
}
47