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

Memcache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 70
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 4
A invokeTest() 0 46 4
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite\Store;
4
5
use SimpleSAML\Module\monitor\TestConfiguration;
6
use SimpleSAML\Module\monitor\TestCase;
7
use SimpleSAML\Module\monitor\TestData;
8
use SimpleSAML\Module\monitor\TestResult;
9
use SimpleSAML\Module\monitor\State;
10
11
final class Memcache extends \SimpleSAML\Module\monitor\TestSuiteFactory
12
{
13
    /** var string|null */
14
    private $class = null;
15
16
17
    /**
18
     * @param \SimpleSAML\Module\monitor\TestConfiguration $configuration
19
     */
20
    public function __construct(TestConfiguration $configuration)
21
    {
22
        $class = class_exists('Memcache') ? 'Memcache' : (class_exists('Memcached') ? 'Memcached' : null);
23
        if ($class !== null) {
24
            $this->class = $class;
25
            $this->setCategory('Memcache sessions');
26
        }
27
28
        parent::__construct($configuration);
29
    }
30
31
32
    /**
33
     * @return void
34
     */
35
    public function invokeTest(): void
36
    {
37
        $testResult = new TestResult('Memcache', 'Overall health');
38
39
        if ($this->class === null) {
40
            $testResult->setState(State::FATAL);
41
            $testResult->setMessage('Missing PHP module');
42
            $this->addTestResult($testResult);
43
        } else {
44
            // Check Memcache-servers
45
46
            $stats = \SimpleSAML\Memcache::getRawStats();
47
            $i = 1;
48
            foreach ($stats as $key => $serverGroup) {
49
                $results = [];
50
                foreach ($serverGroup as $host => $serverStats) {
51
                    $input = [
52
                        'serverStats' => $serverStats,
53
                        'host' => $host
54
                    ];
55
                    $testData = new TestData($input);
56
                    $serverTest = new TestCase\Store\Memcache\Server($testData);
57
                    $results[] = $serverTest->getTestResult();
58
                }
59
60
61
                $input = [
62
                    'results' => $results,
63
                    'group' => strval($i)
64
                ];
65
                $testData = new TestData($input);
66
                $groupTest = new TestCase\Store\Memcache\ServerGroup($testData);
67
                $groupTestResult = $groupTest->getTestResult();
68
                $this->addTestResult($groupTestResult);
69
70
                // Add individual server results
71
                $this->addTestResults($results);
72
73
                $i++;
74
            }
75
76
            $state = $this->calculateState();
77
78
            $testResult->setState($state);
79
        }
80
        $this->setTestResult($testResult);
81
    }
82
}
83