Completed
Push — master ( d95fb9...37c016 )
by Tim
05:25
created

Memcache::invokeTestSuite()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 26
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 38
rs 6.7272
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
9
final class Memcache extends \SimpleSAML\Module\monitor\TestSuiteFactory
10
{
11
    /**
12
     * var string|null
13
     */
14
    private $class = null;
15
16
    /**
17
     * var string
18
     */
19
    private $serverGroupName = '** Unknown **';
20
21
    /**
22
     * @param TestConfiguration $configuration
23
     */
24
    public function __construct($configuration)
25
    {
26
        $class = class_exists('Memcache') ? 'Memcache' : (class_exists('Memcached') ? 'Memcached' : null);
27
        if ($class === null) {
28
            $globalConfig = $configuration->getGlobalConfig();
29
            $serverGroups = $globalConfig->getValue('memcache_store.servers', array());
30
            $serverGroupName = array_map(function($i) {
31
                $group = array_keys($i);
32
                return 'Server Group #' . ++$group[0];
33
            }, $serverGroups);
34
            $this->serverGroupName = implode(PHP_EOL, $serverGroupName);
35
        }
36
        $this->class = $class;
37
        $this->setCategory('Memcache sessions');
38
39
        parent::__construct($configuration);
40
    }
41
42
    /**
43
     * @return void
44
     */
45
    protected function invokeTestSuite()
46
    {
47
        $testResult = new TestResult('Memcache health', $this->serverGroupName);
48
49
        // Check Memcache-servers
50
        if ($this->class === null) {
51
            $testResult->setState(State::FATAL);
52
            $testResult->setMessage('Missing PHP module');
53
        } else {
54
            $stats = \SimpleSAML_Memcache::getRawStats();
55
56
            foreach ($stats as $key => $serverGroup) {
57
                $groupName = is_numeric($key) ? '#' . ++$key : "`$key'";
0 ignored issues
show
Unused Code introduced by
$groupName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
                $groupResults = array();
0 ignored issues
show
Unused Code introduced by
$groupResults is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
60
                foreach ($serverGroup as $host => $serverStats) {
61
                    $input = array(
62
                        'serverStats' => $serverStats,
63
                        'host' => $host
64
                    );
65
                    $testData = new TestData($input);
66
                    $groupTest = new TestCase\Store\Memcache\Server($this, $testData);
67
                    $this->addTestResult($groupTest->getTestResult());
68
                }
69
70
                $state = $this->calculateState();
71
                $testResult->setState($state);
72
                if ($state === State::OK) {
73
                    $testResult->setMessage('Group is healthy');
74
                } elseif ($state === State::WARNING) {
75
                    $testResult->setMessage('Group is crippled');
76
                } else {
77
                    $testResult->setMessage('Group is down');
78
                }
79
            }
80
        }
81
        $this->setTestResult($testResult);
82
    }
83
}
84