Completed
Branch master (9dcfc4)
by Daniel
24:32
created

EnvironmentCheckSuiteResult   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 111
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addResult() 0 11 1
A ShouldPass() 0 4 1
A Status() 0 4 1
A Details() 0 4 1
A toJSON() 0 12 2
B statusText() 0 20 5
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck;
4
5
use InvalidArgumentException;
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\View\ArrayData;
8
use SilverStripe\View\ViewableData;
9
10
/**
11
 * A single set of results from running an EnvironmentCheckSuite
12
 *
13
 * @package environmentcheck
14
 */
15
class EnvironmentCheckSuiteResult extends ViewableData
16
{
17
    /**
18
     * @var ArrayList
19
     */
20
    protected $details;
21
22
    /**
23
     * @var int
24
     */
25
    protected $worst = 0;
26
27
    public function __construct()
28
    {
29
        parent::__construct();
30
        $this->details = new ArrayList();
31
    }
32
33
    /**
34
     * @param int $status
35
     * @param string $message
36
     * @param string $checkIdentifier
37
     */
38
    public function addResult($status, $message, $checkIdentifier)
39
    {
40
        $this->details->push(new ArrayData([
41
            'Check' => $checkIdentifier,
42
            'Status' => $this->statusText($status),
43
            'StatusCode' => $status,
44
            'Message' => $message,
45
        ]));
46
47
        $this->worst = max($this->worst, $status);
48
    }
49
50
    /**
51
     * Returns true if there are no errors.
52
     *
53
     * @return bool
54
     */
55
    public function ShouldPass()
56
    {
57
        return $this->worst <= EnvironmentCheck::WARNING;
58
    }
59
60
    /**
61
     * Returns overall (i.e. worst) status as a string.
62
     *
63
     * @return string
64
     */
65
    public function Status()
66
    {
67
        return $this->statusText($this->worst);
68
    }
69
70
    /**
71
     * Returns detailed status information about each check.
72
     *
73
     * @return ArrayList
74
     */
75
    public function Details()
76
    {
77
        return $this->details;
78
    }
79
80
    /**
81
     * Convert the final result status and details to JSON.
82
     *
83
     * @return string
84
     */
85
    public function toJSON()
86
    {
87
        $result = [
88
            'Status' => $this->Status(),
89
            'ShouldPass' => $this->ShouldPass(),
90
            'Checks' => []
91
        ];
92
        foreach ($this->details as $detail) {
93
            $result['Checks'][] = $detail->toMap();
94
        }
95
        return json_encode($result);
96
    }
97
98
    /**
99
     * Return a text version of a status code.
100
     *
101
     * @param  int $status
102
     * @return string
103
     * @throws InvalidArgumentException
104
     */
105
    protected function statusText($status)
106
    {
107
        switch ($status) {
108
            case EnvironmentCheck::ERROR:
109
                return 'ERROR';
110
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
111
            case EnvironmentCheck::WARNING:
112
                return 'WARNING';
113
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
114
            case EnvironmentCheck::OK:
115
                return 'OK';
116
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
117
            case 0:
118
                return 'NO CHECKS';
119
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
120
            default:
121
                throw new InvalidArgumentException("Bad environment check status '$status'");
122
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
123
        }
124
    }
125
}
126