Completed
Push — master ( c500ff...0fb411 )
by Tim
04:14
created

AuthSources::invokeTestSuite()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 54
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

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

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\Module\monitor\TestData as TestData;
6
7
final class AuthSources extends \SimpleSAML\Module\monitor\TestSuiteFactory
8
{
9
    /**
10
     * @return void
11
     */
12
    protected function initialize() {}
13
14
    /**
15
     * @return void
16
     */
17
    protected function invokeTestSuite()
18
    {
19
        $configuration = $this->getConfiguration();
20
        $moduleConfig = $configuration->getModuleConfig();
21
        $authsourceConfig = $configuration->getAuthsourceConfig();
22
        $checkAuthsources = $moduleConfig->getValue('check_authsources', true);
23
24
        if ($checkAuthsources === true) {
25
            $authsources = $authsourceConfig->getOptions();
26
        } else if (is_array($checkAuthsources)) {
27
            $authsources = array_intersect($authsourceConfig->getOptions(), $checkAuthsources);
28
        } else { // false or invalid value
29
            return;
30
        }
31
32
        foreach ($authsources as $authsourceId) {
33
            $authsourceData = $authsourceConfig->getValue($authsourceId);
34
            $testData = new TestData(
35
                array(
36
                    'authsourceId' => $authsourceId,
37
                    'authsourceData' => $authsourceData
38
                )
39
            );
40
41
            switch ($authsourceData[0]) {
42
                case 'ldap:LDAP':
43
                    $test = new AuthSource\Ldap($configuration, $testData);
44
                    $this->addTest($test);
45
                    $this->addMessages($test->getMessages(), $authsourceId);
46
                    break;
47
                case 'negotiate:Negotiate':
48
                    $test = new AuthSource\Negotiate($configuration, $testData);
49
                    $this->addTest($test);
50
                    $this->addMessages($test->getMessages(), $authsourceId);
51
52
                    $this->convertAuthsourceData();
0 ignored issues
show
Bug introduced by
The call to convertAuthsourceData() misses a required argument $authsourceData.

This check looks for function calls that miss required arguments.

Loading history...
53
                    $testData->setInput($authsourceData, 'authsourceData');
54
55
                    $ldapTest = new AuthSource\Ldap($configuration, $testData);
56
                    $this->addTest($ldapTest);
57
                    $this->addMessages($ldapTest->getMessages(), $authsourceId);
58
59
                    break;
60
                case 'multiauth:MultiAuth':
61
                    // Relies on other authsources
62
                    continue 2;
63
                default:
64
                    // Not implemented
65
                    continue 2;
66
            }
67
        }
68
69
        $this->calculateState();
70
    }
71
72
    /**
73
     * @param array $authsourceData
74
     *
75
     * @return void
76
     */
77
    private function convertAuthsourceData(&$authsourceData)
78
    {
79
        // Prep authsource data
80
        if (isSet($authsourceData['debugLDAP'])) {
81
            $authsourceData['debug'] = $authsourceData['debugLDAP'];
82
            unset($authsourceData['debugLDAP']);
83
        }
84
        if (isSet($authsourceData['adminUser'])) {
85
            $authsourceData['search.username'] = $authsourceData['adminUser'];
86
            unset($authsourceData['adminUser']);
87
        }
88
        if (isSet($authsourceData['adminPassword'])) {
89
            $authsourceData['search.password'] = $authsourceData['adminPassword'];
90
            unset($authsourceData['adminPassword']);
91
        }
92
        if (isSet($authsourceData['base'])) {
93
            $authsourceData['search.base'] = $authsourceData['base'];
94
            unset($authsourceData['base']);
95
        }
96
    }
97
}
98