Completed
Branch feature-unit-tests (9d0273)
by Tim
01:44
created

TestConfiguration   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 254
rs 10
c 0
b 0
f 0
wmc 30

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getAvailableApacheModules() 0 3 1
A getRequestVars() 0 3 1
A setGlobalConfig() 0 4 1
A setModuleConfig() 0 4 1
A getMetadataConfig() 0 4 1
A getServerVars() 0 3 1
A getModuleConfig() 0 4 1
A getAuthSourceConfig() 0 4 1
A setAuthsourceConfig() 0 4 1
A setAvailablePhpModules() 0 3 1
A setMetadataConfig() 0 15 4
A getAvailablePhpModules() 0 3 1
A __construct() 0 11 1
B getAvailableApacheModulesCgi() 0 29 6
B getAvailableMetadataSets() 0 17 5
A getGlobalConfig() 0 3 1
A setAvailableApacheModules() 0 7 2
1
<?php
2
3
namespace SimpleSAML\Module\monitor;
4
5
use \SimpleSAML\Module\monitor\DependencyInjection as DependencyInjection;
6
use \SimpleSAML_Configuration as ApplicationConfiguration;
7
use \SimpleSAML_Metadata_MetaDataStorageSource as MetaDataStorageSource;
8
	
9
10
final class TestConfiguration
11
{
12
    /**
13
     * @var ApplicationConfiguration
14
     */
15
    private $globalConfig;
16
17
    /**
18
     * @var ApplicationConfiguration
19
     */
20
    private $moduleConfig;
21
22
    /**
23
     * @var ApplicationConfiguration
24
     */
25
    private $authSourceConfig;
26
27
    /**
28
     * @var array
29
     */
30
    private $metadataConfig;
31
32
    /**
33
     * @var array
34
     */
35
    private $availableApacheModules;
36
37
    /**
38
     * @var array
39
     */
40
    private $availablePhpModules;
41
42
    /**
43
     * @var DependencyInjection
44
     */
45
    private $serverVars;
46
47
    /**
48
     * @var DependencyInjection
49
     */
50
    private $requestVars;
51
52
    /**
53
     * @param DependencyInjection $serverVars
54
     * @param DependencyInjection $requestVars
55
     */
56
    public function __construct($serverVars, $requestVars, $globalConfig, $authSourceConfig, $moduleConfig)
57
    {
58
        $this->serverVars = $serverVars;
59
        $this->requestVars = $requestVars;
60
61
        $this->setAuthsourceConfig($authSourceConfig);
62
        $this->setModuleConfig($moduleConfig);
63
        $this->setGlobalConfig($globalConfig);
64
        $this->setMetadataConfig();
65
        $this->setAvailableApacheModules();
66
        $this->setAvailablePhpModules();
67
    }
68
69
    /**
70
     * @param ApplicationConfiguration $authSourceConfig
71
     *
72
     * @return void
73
     */
74
    private function setAuthsourceConfig($authSourceConfig)
75
    {
76
        assert($authSourceConfig instanceof ApplicationConfiguration);
77
        $this->authSourceConfig = $authSourceConfig;
78
    }
79
80
    /**
81
     * @param ApplicationConfiguration $moduleConfig
82
     *
83
     * @return void
84
     */
85
    private function setModuleConfig($moduleConfig)
86
    {
87
        assert($moduleConfig instanceof ApplicationConfiguration);
88
        $this->moduleConfig = $moduleConfig;
89
    }
90
91
    /**
92
     * #param ApplicationConfiguration $globalConfig
93
     *
94
     * @return void
95
     */
96
    private function setGlobalConfig($globalConfig)
97
    {
98
        assert($globalConfig instanceof ApplicationConfiguration);
99
        $this->globalConfig = $globalConfig;
100
    }
101
102
    /**
103
     * @return void
104
     */
105
    private function setMetadataConfig()
106
    {
107
        $sets = $this->getAvailableMetadataSets();
108
        $sources = $this->globalConfig->getValue('metadata.sources');
109
        $handlers = MetaDataStorageSource::parseSources($sources);
110
        $metadata = [];
111
        if (!empty($sets)) {
112
            foreach ($handlers as $handler) {
113
                foreach ($sets as $set) {
114
                    $metadata[$set] = $handler->getMetadataSet($set);
115
                }
116
            }
117
        }
118
        assert(is_array($metadata));
119
        $this->metadataConfig = $metadata;
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    protected function getAvailableMetadataSets()
126
    {
127
        $globalConfig = $this->getGlobalConfig();
128
        $sets = array();
129
        if ($globalConfig->getBoolean('enable.saml20-idp', false)) {
130
            $sets = array_merge($sets, array('saml20-idp-hosted', 'saml20-sp-remote', 'saml20-idp-remote'));
131
        }
132
        if ($globalConfig->getBoolean('enable.shib13-idp', false)) {
133
            $sets = array_merge($sets, array('shib13-idp-hosted', 'shib13-sp-hosted', 'shib13-sp-remote', 'shib13-idp-remote'));
134
        }
135
        if ($globalConfig->getBoolean('enable.adfs-idp', false)) {
136
            $sets = array_merge($sets, array('adfs-idp-hosted', 'adfs-sp-remote'));
137
        }
138
        if ($globalConfig->getBoolean('enable.wsfed-sp', false)) {
139
            $sets = array_merge($sets, array('wsfed-sp-hosted', 'wsfed-idp-remote'));
140
        }
141
        return $sets;
142
    }
143
144
    /**
145
     * @return void
146
     */
147
    private function setAvailableApacheModules()
148
    {
149
        // Determine available Apache-modules
150
        if (function_exists('apache_get_modules')) {
151
            $this->availableApacheModules = apache_get_modules();
152
        } else { // CGI-mode
153
            $this->availableApacheModules = $this->getAvailableApacheModulesCgi();
154
        }
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    private function getAvailableApacheModulesCgi()
161
    {
162
        $knownLocations = array(
163
            '/usr/sbin/httpd',
164
            '/usr/sbin/apache2',
165
            '/opt/rh/httpd24/root/usr/sbin/httpd'
166
        );
167
168
        $output = null;
169
        foreach ($knownLocations as $location) {
170
            if (file_exists($location)) {
171
                exec("$location -t -D DUMP_MODULES", $output);
172
                break;
173
            }
174
        }
175
176
        if ($output === null) {
177
            return array(); // Cannot determine available modules
178
        }
179
        array_shift($output);
180
181
        $modules = array();
182
        foreach ($output as $module) {
183
            $module = ltrim($module);
184
            if (($res = preg_replace('/(_module \((shared|static)\))/', '', $module)) !== $module) {
185
                $modules[] = 'mod_' . $res;
186
            } // else skip
187
        }
188
        return $modules;
189
    }
190
191
    /**
192
     * @return void
193
     */
194
    private function setAvailablePhpModules()
195
    {
196
        $this->availablePhpModules = array_merge(get_loaded_extensions(), get_loaded_extensions(true));
197
    }
198
199
    /**
200
     * @return array
201
     */
202
    public function getAvailableApacheModules()
203
    {
204
        return $this->availableApacheModules;
205
    }
206
207
    /**
208
     * @return array
209
     */
210
    public function getAvailablePhpModules()
211
    {
212
        return $this->availablePhpModules;
213
    }
214
215
    /**
216
     * @return DependencyInjection
217
     */
218
    public function getServerVars()
219
    {
220
        return $this->serverVars;
221
    }
222
223
    /**
224
     * @return DependencyInjection
225
     */
226
    public function getRequestVars()
227
    {
228
        return $this->requestVars;
229
    }
230
231
    /**
232
     * @return ApplicationConfiguration
233
     */
234
    public function getGlobalConfig()
235
    {
236
        return $this->globalConfig;
237
    }
238
239
    /**
240
     * @return ApplicationConfiguration
241
     */
242
    public function getModuleConfig()
243
    {
244
        assert($this->moduleConfig instanceof ApplicationConfiguration);
245
        return $this->moduleConfig;
246
    }
247
248
    /**
249
     * @return ApplicationConfiguration
250
     */
251
    public function getAuthSourceConfig()
252
    {
253
        assert($this->authSourceConfig instanceof ApplicationConfiguration);
254
        return $this->authSourceConfig;
255
    }
256
257
    /**
258
     * @return array
259
     */
260
    public function getMetadataConfig()
261
    {
262
        assert(is_array($this->metadataConfig));
263
        return $this->metadataConfig;
264
    }
265
}
266