Completed
Push — master ( 09bb23...610f60 )
by Tim
01:49
created

Modules::processTestResults()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.2
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
10
class Modules extends \SimpleSAML\Module\monitor\TestSuiteFactory
11
{
12
    /**
13
     * @var array
14
     */
15
    private $requiredApacheModules = array();
16
17
    /**
18
     * @var array
19
     */
20
    // Important!!  Modules-names are handled case-sensitive!!
21
    private $storeApacheDependencies = array();
0 ignored issues
show
Unused Code introduced by
The property $storeApacheDependencies is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
    /**
24
     * @var array
25
     */
26
    private $moduleApacheDependencies = array(
27
        'negotiateext' => 'mod_auth_kerb|mod_auth_gssapi'
28
    );
29
30
    /**
31
     * @var array
32
     */
33
    private $requiredPhpModules = array();
34
35
    /**
36
     * @var array
37
     */
38
    private $storePhpDependencies = array(
39
        'memcache' => 'memcached|memcache',
40
        'phpsession' => 'session',
41
        'redis' => 'redis',
42
        'redissentinel' => 'redis',
43
        'riak:Store' => 'riak',
44
        'sql' => 'PDO'
45
    );
46
47
    /**
48
     * @var array
49
     */
50
    private $modulePhpDependencies = array(
51
        'authfacebook' => array('curl', 'json'),
52
        'authYubiKey' => 'curl',
53
// TODO: consent only requires pdo when database backend is used.. Should probably add this to required-list when processing metadata
54
//        'consent' => 'PDO',
55
        'consentAdmin' => 'PDO',
56
        'ldap' => 'ldap',
57
        'memcacheMonitor' => 'memcached|memcache',
58
        'negotiate' => 'krb5',
59
        'radius' => 'radius',
60
        'riak' => 'riak',
61
        'sqlauth' => 'PDO'
62
    );
63
64
    /**
65
     * @param TestData|null $testData
66
     *
67
     * @return void
68
     */
69
    protected function initialize($testData = null)
70
    {
71
        $this->setRequiredApacheModules();
0 ignored issues
show
Bug introduced by
The method setRequiredApacheModules() does not seem to exist on object<SimpleSAML\Module...itor\TestSuite\Modules>.

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...
72
        $this->setRequiredPhpModules();
73
        $this->setRequiredSspModules();
74
    }
75
76
    /**
77
     * @return void
78
     */
79
    private function addRequiredPhpModule($module)
80
    {
81
        if (!in_array($module, $this->requiredPhpModules)) {
82
            $this->requiredPhpModules[] = $module;
83
        }
84
    }
85
86
87
    /**
88
     * @return void
89
     */
90
    private function setRequiredPhpModules()
91
    {
92
        // PHP modules required
93
        $composerFile = \SimpleSAML\Utils\System::resolvePath('composer.json');
94
        $composerData = file_get_contents($composerFile);
95
        $composer = json_decode($composerData, true);
96
        $composerRequired = $composer['require'];
97
98
        foreach ($composerRequired as $ext => $ver) {
99
            if (preg_match('/^ext-/', $ext)) {
100
                $this->addRequiredPhpModule(substr($ext, 4));
101
            }
102
        }
103
104
        // Determine extra required modules
105
        $configuration = $this->getConfiguration();
106
        $globalConfig = $configuration->getGlobalConfig();
107
        $store = $globalConfig->getValue('store.type');
108
        if (array_key_exists($store, $this->storePhpDependencies)) {
109
            $this->addRequiredPhpModule($this->storePhpDependencies[$store]);
110
        }
111
    }
112
113
    /**
114
     * @return void
115
     */
116
    private function setRequiredSspModules()
117
    {
118
        $modules = \SimpleSAML\Module::getModules();
119
        foreach ($modules as $module) {
120
            if (\SimpleSAML\Module::isModuleEnabled($module)) {
121
                if (array_key_exists($module, $this->moduleApacheDependencies)) {
122
                    $dependencies = \SimpleSAML\Utils\Arrays::Arrayize($this->moduleApacheDependencies[$module]);
123
                    foreach ($dependencies as $dependency) {
124
                        $this->addRequiredApacheModule($dependency);
0 ignored issues
show
Bug introduced by
The method addRequiredApacheModule() does not seem to exist on object<SimpleSAML\Module...itor\TestSuite\Modules>.

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...
125
                    }
126
                }
127
                if (array_key_exists($module, $this->modulePhpDependencies)) {
128
                    $dependencies = \SimpleSAML\Utils\Arrays::Arrayize($this->modulePhpDependencies[$module]);
129
                    foreach ($dependencies as $dependency) {
130
                        $this->addRequiredPhpModule($dependency);
131
                    }
132
                }
133
            }
134
        }
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function getAvailableApacheModules()
141
    {
142
        $configuration = $this->getConfiguration();
143
        return $configuration->getAvailableApacheModules();
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function getAvailablePhpModules()
150
    {
151
        $configuration = $this->getConfiguration();
152
        return $configuration->getAvailablePhpModules();
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    private function getRequiredApacheModules()
159
    {
160
        return $this->requiredApacheModules;
161
    }
162
163
164
    /**
165
     * @return array
166
     */
167
    private function getRequiredPhpModules()
168
    {
169
        return $this->requiredPhpModules;
170
    }
171
172
173
    /**
174
     * @return array<string,array>
175
     */
176
    private function getModuleDependencies()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
177
    {
178
        return ['Apache' => $this->moduleApacheDependencies, 'Php' => $this->modulePhpDependencies];
179
    }
180
181
    /**
182
     * @return void
183
     */
184
    protected function invokeTestSuite()
185
    {
186
        $configuration = $this->getConfiguration();
187
188
        // Test Apache modules
189
        $testData = new TestData([
190
            'available' => $configuration->getAvailableApacheModules(),
191
            'required' => $this->getRequiredApacheModules(),
192
            'dependencies' => $this->moduleApacheDependencies,
193
            'type' => 'Apache',
194
            'testClass' => TestCase\Module\Apache,
195
        ]);
196
        $apacheTest = new Modules\ModuleSet($configuration, $testData);
197
        $apacheTestResult = $apacheTest->getTestResult();
198
        
199
         // Test Php modules
200
        $testData = new TestData([
201
            'available' => $configuration->getAvailablePhpModules(),
202
            'required' => $this->getRequiredPhpModules(),
203
            'dependencies' => $this->modulePhpDependencies,
204
            'type' => 'Php',
205
            'testClass' => TestCase\Module\Php,
206
        ]);
207
        $phpTest = new Modules\ModuleSet($configuration, $testData);
208
        $phpTestResult = $phpTest->getTestResult();
209
210
        $this->addTestResults($apacheTestResult);
0 ignored issues
show
Bug introduced by
The method addTestResults() does not exist on SimpleSAML\Module\monitor\TestSuite\Modules. Did you maybe mean addTestResult()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
211
        $this->addTestResults($phpTestResult);
0 ignored issues
show
Bug introduced by
The method addTestResults() does not exist on SimpleSAML\Module\monitor\TestSuite\Modules. Did you maybe mean addTestResult()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
212
213
        $testResult = new TestResult('Modules', '');
214
        $testResult->setState($this->calculateState());
215
        
216
        $this->setTestResult($testResult);
217
    }
218
}
219