Completed
Push — master ( 7b44ba...d9c836 )
by Tim
01:38
created

AuthSources::invokeTestSuite()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 35
c 1
b 0
f 0
nc 11
nop 0
dl 0
loc 52
rs 7.2396

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\TestData as TestData;
8
9
final class AuthSources extends \SimpleSAML\Module\monitor\TestSuiteFactory
10
{
11
    /**
12
     * @var array|bool
13
     */
14
    private $checkAuthSources = true;
15
16
    /**
17
     * @var ApplicationConfiguration
18
     */
19
    private $authSourceConfig;
20
21
22
    /**
23
     * @param TestConfiguration $configuration
24
     */
25
    public function __construct($configuration)
26
    {
27
        $moduleConfig = $configuration->getModuleConfig();
28
29
        $this->authSourceConfig = $configuration->getAuthSourceConfig();
30
        $this->checkAuthSources = $moduleConfig->getValue('checkAuthSources', true);
31
32
        parent::__construct($configuration);
33
    }
34
35
36
    /**
37
     * @return void
38
     */
39
    protected function invokeTestSuite()
40
    {
41
        if ($this->checkAuthSources === true) {
42
            $authSources = $this->authSourceConfig->getOptions();
43
        } else if (is_array($this->checkAuthSources)) {
44
            $authSources = array_intersect($this->authSourceConfig->getOptions(), $this->checkAuthSources);
45
        } else { // false or invalid value
46
            return;
47
        }
48
49
        $configuration = $this->getConfiguration();
50
        foreach ($authSources as $authSourceId) {
51
            $authSourceData = $this->authSourceConfig->getValue($authSourceId);
52
            $input = array(
53
                'authSourceId' => $authSourceId,
54
                'authSourceData' => $authSourceData
55
            );
56
            $testData = new TestData($input);
57
58
            switch ($authSourceData[0]) {
59
                case 'ldap:LDAP':
60
                    $test = new AuthSource\Ldap($configuration, $testData);
61
                    $testResult = $test->getTestResult();
0 ignored issues
show
Unused Code introduced by
$testResult is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
                    $this->addTestResult($test->getTestResult());
63
                    //$this->addMessages($test->getMessages(), $authSourceId);
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
                    break;
65
                case 'negotiate:Negotiate':
66
                    $test = new AuthSource\Negotiate($configuration, $testData);
67
                    $testResult = $test->getTestResult();
0 ignored issues
show
Unused Code introduced by
$testResult is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
                    $this->addTestResult($test->getTestResult());
69
                    //$this->addMessages($test->getMessages(), $authSourceId);
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
71
                    // We need to do some convertions from Negotiate > LDAP
72
                    $this->convertAuthSourceData($authSourceData);
73
                    $testData->setInput($authSourceData, 'authSourceData');
74
75
                    $ldapTest = new AuthSource\Ldap($configuration, $testData);
0 ignored issues
show
Unused Code introduced by
$ldapTest is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
                    $testResult = $test->getTestResult();
0 ignored issues
show
Unused Code introduced by
$testResult is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
                    $this->addTestResult($test->getTestResult());
78
                    //$this->addMessages($ldapTest->getMessages(), $authSourceId);
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
                    break;
80
                case 'multiauth:MultiAuth':
81
                    // Relies on other authSources
82
                    continue 2;
83
                default:
84
                    // Not implemented
85
                    continue 2;
86
            }
87
        }
88
89
        $this->calculateState();
90
    }
91
92
93
    /**
94
     * @param array $authSourceData
95
     *
96
     * @return void
97
     */
98
    private function convertAuthSourceData(&$authSourceData)
99
    {
100
        // LDAP and Negotiate authSources use different names for equal properties
101
        // Hopefully this function can go away in SSP 2.0
102
        if (isSet($authSourceData['debugLDAP'])) {
103
            $authSourceData['debug'] = $authSourceData['debugLDAP'];
104
            unset($authSourceData['debugLDAP']);
105
        }
106
        if (isSet($authSourceData['adminUser'])) {
107
            $authSourceData['search.username'] = $authSourceData['adminUser'];
108
            unset($authSourceData['adminUser']);
109
        }
110
        if (isSet($authSourceData['adminPassword'])) {
111
            $authSourceData['search.password'] = $authSourceData['adminPassword'];
112
            unset($authSourceData['adminPassword']);
113
        }
114
        if (isSet($authSourceData['base'])) {
115
            $authSourceData['search.base'] = $authSourceData['base'];
116
            unset($authSourceData['base']);
117
        }
118
    }
119
}
120