ServerGroup::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\monitor\TestCase\Store\Memcache;
6
7
use SimpleSAML\Module\monitor\State;
8
use SimpleSAML\Module\monitor\TestData;
9
use SimpleSAML\Module\monitor\TestResult;
10
11
use function max;
12
use function min;
13
14
final class ServerGroup extends \SimpleSAML\Module\monitor\TestCaseFactory
15
{
16
    /** @var array */
17
    private array $results = [];
18
19
    /** @var string|null */
20
    private ?string $group;
21
22
23
    /**
24
     * @param \SimpleSAML\Module\monitor\TestData $testData
25
     *
26
     * @return void
27
     */
28
    protected function initialize(TestData $testData): void
29
    {
30
        $this->results = $testData->getInputItem('results');
31
        $this->group = $testData->getInputItem('group');
32
33
        parent::initialize($testData);
34
    }
35
36
37
    /**
38
     * @return void
39
     */
40
    public function invokeTest(): void
41
    {
42
        $testResult = new TestResult('Memcache Server Group Health', 'Group ' . $this->group);
43
44
        $states = [];
45
        foreach ($this->results as $result) {
46
            $states[] = $result->getState();
47
        }
48
        $state = min($states);
49
        if ($state !== max($states)) {
50
            $state = State::WARNING;
51
        }
52
        $testResult->setState($state);
53
54
        if ($state === State::OK) {
55
            $testResult->setMessage('Group is healthy');
56
        } elseif ($state === State::WARNING) {
57
            $testResult->setMessage('Group is crippled');
58
        } else {
59
            $testResult->setMessage('Group is down');
60
        }
61
62
        $this->setTestResult($testResult);
63
    }
64
}
65