Passed
Branch monitor-2.5.x (8b654c)
by Tim
01:31
created

Store::testPhpSession()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 26
nc 6
nop 0
dl 0
loc 37
rs 9.1928
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Modules\Monitor\TestSuite;
4
5
use \SimpleSAML\Modules\Monitor\TestConfiguration as TestConfiguration;
6
use \SimpleSAML\Modules\Monitor\State as State;
7
use \SimpleSAML\Modules\Monitor\TestCase as TestCase;
8
use \SimpleSAML\Modules\Monitor\TestData as TestData;
9
use \SimpleSAML\Logger as Logger;
10
11
final class Store extends \SimpleSAML\Modules\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':
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
                if (method_exists('\SimpleSAML_Configuration', 'setPreLoadedConfig')) {
95
                    $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

95
                    /** @scrutinizer ignore-call */ 
96
                    $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...
96
                        \SimpleSAML_Configuration::loadFromArray(
97
                            array(
98
                                'memcache_store.servers' => $this->parsePhpMemcachedConfiguration(session_save_path())
99
                            )
100
                        )
101
                    );
102
103
                    $test = new Store\Memcache($configuration);
104
                    $results = $test->getTestResults();
105
                } else {
106
                    Logger::warning("Not implemented;  $this->store - Skipping Store TestSuite.");
107
                }
108
                break;
109
//          case 'sqlite':
110
//          case 'mm':
111
            default:
112
                Logger::warning("Not implemented;  $this->store - Skipping Store TestSuite.");
113
                break;
114
        }
115
        return $results;
116
    }
117
118
    /**
119
     * @param string $spec
120
     *
121
     * @return array
122
     */
123
    private function parsePhpMemcachedConfiguration($spec)
124
    {
125
        $servers = preg_split('/\s*,\s*/', $spec);
126
127
        $results = array();
128
        foreach ($servers as $server) {
129
            $result = array();
130
            list($host, $params) = explode('?', $server);
131
            list($hostname, $port) = explode(':', $host);
132
133
            // Strip protocol when possible (memcache)
134
            $prefix = 'tcp://';
135
            if (substr($hostname, 0, 6) === $prefix) {
136
                $hostname = substr($hostname, 6);
137
            }
138
139
            $result['hostname'] = $hostname;
140
            if ($port !== null) {
141
                $result['port'] = $port;
142
            }
143
            parse_str($params, $tmp);
144
            $results[]  = array_merge($result, $tmp);
145
        }
146
147
        return array($results);
148
    }
149
}
150