Cancelled
Branch master (244451)
by Tim
10:31
created

Memcache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

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