Passed
Branch master (70a077)
by Tim
02:25
created

Modules::setRequiredPhpModules()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 20
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();
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();
72
        $this->setRequiredPhpModules();
73
        $this->setRequiredSspModules();
74
        $this->setCategory('Modules');
75
    }
76
77
78
    /**
79
     * @return void
80
     */
81
    private function addRequiredApacheModule($module)
82
    {
83
        if (!in_array($module, $this->requiredApacheModules)) {
84
            $this->requiredApacheModules[] = $module;
85
        }
86
    }
87
88
89
    /**
90
     * @return void
91
     */
92
    private function setRequiredApacheModules()
93
    {
94
        // Apache Modules
95
        $this->addRequiredApacheModule('mod_php|mod_php5');
96
        if (\SimpleSAML\Utils\HTTP::isHTTPS()) {
97
            $this->addRequiredApacheModule('mod_ssl');
98
        }
99
100
        // Determine extra required modules
101
        $configuration = $this->getConfiguration();
102
        $globalConfig = $configuration->getGlobalConfig();
103
        $store = $globalConfig->getValue('store.type');
104
        if (array_key_exists($store, $this->storeApacheDependencies)) {
105
            $this->addRequiredApacheModule($this->storeApacheDependencies[$store]);
106
        }
107
    }
108
109
110
    /**
111
     * @return void
112
     */
113
    private function addRequiredPhpModule($module)
114
    {
115
        if (!in_array($module, $this->requiredPhpModules)) {
116
            $this->requiredPhpModules[] = $module;
117
        }
118
    }
119
120
121
    /**
122
     * @return void
123
     */
124
    private function setRequiredPhpModules()
125
    {
126
        // PHP modules required
127
        $composerFile = \SimpleSAML\Utils\System::resolvePath('composer.json');
128
        $composerData = file_get_contents($composerFile);
129
        $composer = json_decode($composerData, true);
130
        $composerRequired = $composer['require'];
131
132
        foreach ($composerRequired as $ext => $ver) {
133
            if (preg_match('/^ext-/', $ext)) {
134
                $this->addRequiredPhpModule(substr($ext, 4));
135
            }
136
        }
137
138
        // Determine extra required modules
139
        $configuration = $this->getConfiguration();
140
        $globalConfig = $configuration->getGlobalConfig();
141
        $store = $globalConfig->getValue('store.type');
142
        if (array_key_exists($store, $this->storePhpDependencies)) {
143
            $this->addRequiredPhpModule($this->storePhpDependencies[$store]);
144
        }
145
    }
146
147
    /**
148
     * @return void
149
     */
150
    private function setRequiredSspModules()
151
    {
152
        $modules = \SimpleSAML\Module::getModules();
153
        foreach ($modules as $module) {
154
            if (\SimpleSAML\Module::isModuleEnabled($module)) {
155
                if (array_key_exists($module, $this->moduleApacheDependencies)) {
156
                    $dependencies = \SimpleSAML\Utils\Arrays::Arrayize($this->moduleApacheDependencies[$module]);
157
                    foreach ($dependencies as $dependency) {
158
                        $this->addRequiredApacheModule($dependency);
159
                    }
160
                }
161
                if (array_key_exists($module, $this->modulePhpDependencies)) {
162
                    $dependencies = \SimpleSAML\Utils\Arrays::Arrayize($this->modulePhpDependencies[$module]);
163
                    foreach ($dependencies as $dependency) {
164
                        $this->addRequiredPhpModule($dependency);
165
                    }
166
                }
167
            }
168
        }
169
    }
170
171
    /**
172
     * @return array
173
     */
174
    public function getAvailableApacheModules()
175
    {
176
        $configuration = $this->getConfiguration();
177
        return $configuration->getAvailableApacheModules();
178
    }
179
180
    /**
181
     * @return array
182
     */
183
    public function getAvailablePhpModules()
184
    {
185
        $configuration = $this->getConfiguration();
186
        return $configuration->getAvailablePhpModules();
187
    }
188
189
    /**
190
     * @return array
191
     */
192
    private function getRequiredApacheModules()
193
    {
194
        return $this->requiredApacheModules;
195
    }
196
197
198
    /**
199
     * @return array
200
     */
201
    private function getRequiredPhpModules()
202
    {
203
        return $this->requiredPhpModules;
204
    }
205
206
207
    /**
208
     * @return void
209
     */
210
    protected function invokeTestSuite()
211
    {
212
        $configuration = $this->getConfiguration();
213
214
        // Test Apache modules
215
        $testData = new TestData([
216
            'available' => $configuration->getAvailableApacheModules(),
217
            'required' => $this->getRequiredApacheModules(),
218
            'dependencies' => $this->moduleApacheDependencies,
219
            'type' => 'Apache',
220
            'testClass' => TestCase\Module\Apache,
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\Module\monito...\TestCase\Module\Apache was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
221
        ]);
222
        $apacheTest = new Modules\ModuleSet($configuration, $testData);
223
        $apacheTestResult = $apacheTest->getTestResult();
224
        
225
         // Test Php modules
226
        $testData = new TestData([
227
            'available' => $configuration->getAvailablePhpModules(),
228
            'required' => $this->getRequiredPhpModules(),
229
            'dependencies' => $this->modulePhpDependencies,
230
            'type' => 'Php',
231
            'testClass' => TestCase\Module\Php,
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\Module\monito...ite\TestCase\Module\Php was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
232
        ]);
233
        $phpTest = new Modules\ModuleSet($configuration, $testData);
234
        $phpTestResult = $phpTest->getTestResult();
235
236
        $this->addTestResult($apacheTestResult);
237
        $this->addTestResult($phpTestResult);
238
239
        $testResult = new TestResult('Modules', '');
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monitor\TestSuite\TestResult was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
240
        $testResult->setState($this->calculateState());
241
        
242
        $this->setTestResult($testResult);
243
    }
244
}
245