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

CallbackCheck   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkStatus() 0 14 4
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