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

AuthSources::invokeTest()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 54
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 38
nc 11
nop 0
dl 0
loc 54
rs 7.8331
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite;
4
5
use \SimpleSAML_Configuration as ApplicationConfiguration;
6
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration;
7
use \SimpleSAML\Module\monitor\TestResult as TestResult;
8
use \SimpleSAML\Module\monitor\TestData as TestData;
9
10
final class AuthSources extends \SimpleSAML\Module\monitor\TestSuiteFactory
11
{
12
    /**
13
     * @var ApplicationConfiguration
14
     */
15
    private $authSourceConfig;
16
17
    /**
18
     * @var array|bool
19
     */
20
    private $checkAuthSources = true;
21
22
    /**
23
     * @param TestConfiguration $configuration
24
     */
25
    public function __construct($configuration)
26
    {
27
        $moduleConfig = $configuration->getModuleConfig();
28
        $this->authSourceConfig = $configuration->getAuthSourceConfig();
29
        $this->checkAuthSources = $moduleConfig->getValue('checkAuthSources', true);
30
        $this->setCategory('Authentication sources');
31
32
        parent::__construct($configuration);
33
    }
34
35
    /**
36
     * @return void
37
     */
38
    public function invokeTest()
39
    {
40
        if ($this->checkAuthSources === true) {
41
            $authSources = $this->authSourceConfig->getOptions();
42
        } else if (is_array($this->checkAuthSources)) {
43
            $authSources = array_intersect($this->authSourceConfig->getOptions(), $this->checkAuthSources);
44
        } else { // false or invalid value
45
            return;
46
        }
47
48
        $configuration = $this->getConfiguration();
49
        $output = [];
50
51
        foreach ($authSources as $authSourceId) {
52
            $authSourceData = $this->authSourceConfig->getValue($authSourceId);
53
            $input = array(
54
                'authSourceId' => $authSourceId,
55
                'authSourceData' => $authSourceData
56
            );
57
            $testData = new TestData($input);
58
59
            switch ($authSourceData[0]) {
60
                case 'ldap:LDAP':
61
                    $ldapTest = new AuthSource\Ldap($configuration, $testData);
62
                    $this->addTestResults($ldapTest->getTestResults());
63
                    $output[$authSourceId] = $ldapTest->getArrayizeTestResults();
64
                    break;
65
                case 'negotiate:Negotiate':
66
                    $negoTest = new AuthSource\Negotiate($configuration, $testData);
67
                    $this->addTestResults($negoTest->getTestResults());
68
69
                    // We need to do some convertions from Negotiate > LDAP
70
                    $this->convertAuthSourceData($authSourceData);
71
                    $testData->setInput($authSourceData, 'authSourceData');
72
73
                    $ldapTest = new AuthSource\Ldap($configuration, $testData);
74
                    $this->addTestResults($ldapTest->getTestResults());
75
76
                    $output[$authSourceId] = array_merge($negoTest->getArrayizeTestResults() ,$ldapTest->getArrayizeTestResults());
77
                    break;
78
                case 'multiauth:MultiAuth':
79
                    // Relies on other authSources
80
                    continue 2;
81
                default:
82
                    // Not implemented
83
                    continue 2;
84
            }
85
        }
86
87
        $state = $this->calculateState();
88
        $testResult = new TestResult('Authentication sources');
89
        $testResult->setState($state);
90
        $testResult->setOutput($output);
91
        $this->setTestResult($testResult);
92
    }
93
94
    /**
95
     * @param array $authSourceData
96
     *
97
     * @return void
98
     */
99
    private function convertAuthSourceData(&$authSourceData)
100
    {
101
        // LDAP and Negotiate authSources use different names for equal properties
102
        // Hopefully this function can go away in SSP 2.0
103
        if (isSet($authSourceData['debugLDAP'])) {
104
            $authSourceData['debug'] = $authSourceData['debugLDAP'];
105
            unset($authSourceData['debugLDAP']);
106
        }
107
        if (isSet($authSourceData['adminUser'])) {
108
            $authSourceData['search.username'] = $authSourceData['adminUser'];
109
            unset($authSourceData['adminUser']);
110
        }
111
        if (isSet($authSourceData['adminPassword'])) {
112
            $authSourceData['search.password'] = $authSourceData['adminPassword'];
113
            unset($authSourceData['adminPassword']);
114
        }
115
        if (isSet($authSourceData['base'])) {
116
            $authSourceData['search.base'] = $authSourceData['base'];
117
            unset($authSourceData['base']);
118
        }
119
    }
120
}
121