Passed
Push — master ( b2f2f7...4168d5 )
by Tim
01:45
created

Store::testSspSession()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 22
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite;
4
5
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration;
6
use \SimpleSAML\Module\monitor\State as State;
7
use \SimpleSAML\Module\monitor\TestCase as TestCase;
8
use \SimpleSAML\Module\monitor\TestData as TestData;
9
use \SimpleSAML\Logger as Logger;
10
11
final class Store extends \SimpleSAML\Module\monitor\TestSuiteFactory
12
{
13
    /**
14
     * var string|null
15
     */
16
    private $store = null;
17
18
    /**
19
     * @param TestConfiguration $configuration
20
     */
21
    public function __construct($configuration)
22
    {
23
        $globalConfig = $configuration->getGlobalConfig();
24
        $this->store = $globalConfig->getString('store.type', 'phpsession');
25
        $this->setCategory('Session store');
26
27
        parent::__construct($configuration);
28
    }
29
30
    /**
31
     * @return void
32
     */
33
    public function invokeTest()
34
    {
35
        $configuration = $this->getConfiguration();
36
37
        if ($this->store === 'phpsession') {
38
            $results = $this->testPhpSession();
39
        } else {
40
            $results = $this->testSspSession($configuration);
41
        }
42
43
        foreach ($results as $result) {
44
            $this->addTestResult($result);
45
        }
46
        $this->calculateState();
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    private function testSspSession($configuration)
53
    {
54
        $results = array();
55
56
        switch ($this->store) {
57
            case 'memcache':
58
                $test = new Store\Memcache($configuration);
59
                $results = $test->getTestResults();
60
                break;
61
//          case 'redis':
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
//          case 'redissentinel':
63
//              $test = new Store\Redis($configuration);
64
//              break;
65
            case 'sql':
66
                $test = new Store\Sql($configuration);
67
                $results = $test->getTestResults();
68
                break;
69
            default:
70
                Logger::warning("Not implemented;  $this->store - Skipping Store TestSuite.");
71
                break;
72
        }
73
        return $results;
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    private function testPhpSession()
80
    {
81
        $results = array();
82
        switch (ini_get('session.save_handler')) {
83
            case 'files':
84
                $input = [
85
                    'path' => session_save_path(),
86
                    'category' => 'Session storage'
87
                ];
88
                $testData = new TestData($input);
89
                $test = new TestCase\FileSystem\FreeSpace($testData);
90
                $results[] = $test->getTestResult();
91
                break;
92
            case 'memcache':
93
            case 'memcached':
94
                $configuration = \SimpleSAML_Configuration::setPreLoadedConfig(
0 ignored issues
show
Bug introduced by
The method setPreLoadedConfig() does not exist on SimpleSAML_Configuration. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
                /** @scrutinizer ignore-call */ 
95
                $configuration = \SimpleSAML_Configuration::setPreLoadedConfig(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
                    \SimpleSAML_Configuration::loadFromArray(
96
                        array(
97
                            'memcache_store.servers' => $this->parsePhpMemcachedConfiguration(session_save_path())
98
                        )
99
                    )
100
                );
101
102
                $test = new Store\Memcache($configuration);
103
                $results = $test->getTestResults();
104
                break;
105
//          case 'sqlite':
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
//          case 'mm':
107
            default:
108
                Logger::warning("Not implemented;  $this->store - Skipping Store TestSuite.");
109
                break;
110
        }
111
        return $results;
112
    }
113
114
    /**
115
     * @param string $spec
116
     *
117
     * @return array
118
     */
119
    private function parsePhpMemcachedConfiguration($spec)
120
    {
121
        $servers = preg_split('/\s*,\s*/', $spec);
122
123
        $results = array();
124
        foreach ($servers as $server) {
125
            $result = array();
126
            list($host, $params) = explode('?', $server);
127
            list($hostname, $port) = explode(':', $host);
128
129
            // Strip protocol when possible (memcache)
130
            $prefix = 'tcp://';
131
            if (substr($hostname, 0, 6) === $prefix) {
132
                $hostname = substr($hostname, 6);
133
            }
134
135
            $result['hostname'] = $hostname;
136
            if ($port !== null) {
137
                $result['port'] = $port;
138
            }
139
            parse_str($params, $tmp);
140
            $results[]  = array_merge($result, $tmp);
141
        }
142
143
        return array($results);
144
    }
145
}
146