Passed
Push — master ( 9792fc...04e5e0 )
by Tim
06:59
created

Server::invokeTest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 0
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\Store\Memcache;
4
5
use SimpleSAML\Module\monitor\State;
6
use SimpleSAML\Module\monitor\TestData;
7
use SimpleSAML\Module\monitor\TestResult;
8
9
final class Server extends \SimpleSAML\Module\monitor\TestCaseFactory
10
{
11
    /** @var array|false */
12
    private $serverStats;
13
14
15
    /** @var string */
16
    private $host;
17
18
19
    /**
20
     * @param \SimpleSAML\Module\monitor\TestData $testData
21
     *
22
     * @return void
23
     */
24
    protected function initialize(TestData $testData): void
25
    {
26
        $this->serverStats = $testData->getInputItem('serverStats');
27
        $this->host = $testData->getInputItem('host');
28
29
        parent::initialize($testData);
30
    }
31
32
33
    /**
34
     * @return void
35
     */
36
    public function invokeTest(): void
37
    {
38
        $testResult = new TestResult('Memcache Server Health', $this->host);
39
40
        if ($this->serverStats === false) {
41
            $testResult->setState(State::ERROR);
42
            $testResult->setMessage('Host is down');
43
        } else {
44
            $bytesUsed = $this->serverStats['bytes'];
45
            $bytesLimit = $this->serverStats['limit_maxbytes'];
46
            $free = round(100.0 - (($bytesUsed / $bytesLimit) * 100));
47
            $testResult->addOutput($free, 'freePercentage');
48
49
            $testResult->setState(State::OK);
50
            $testResult->setMessage($free . '% free space');
51
        }
52
53
        $this->setTestResult($testResult);
54
    }
55
}
56