Passed
Branch master (4b23d6)
by Tim
04:40
created

AuthSources::invokeTest()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 59
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 59
rs 8.3146
c 0
b 0
f 0
cc 7
nc 11
nop 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;
7
use SimpleSAML\Module\Monitor\TestResult;
8
use SimpleSAML\Module\Monitor\TestData;
9
10
final class AuthSources extends \SimpleSAML\Module\Monitor\TestSuiteFactory
11
{
12
    /** @var \SimpleSAML\Configuration */
13
    private $authSourceConfig;
14
15
    /** @var array|bool */
16
    private $checkAuthSources;
17
18
    /** @var array|null */
19
    private $authSourceSpecifics;
20
21
22
    /**
23
     * @param \SimpleSAML\Module\Monitor\TestConfiguration $configuration
24
     */
25
    public function __construct(TestConfiguration $configuration)
26
    {
27
        $moduleConfig = $configuration->getModuleConfig();
28
        $this->authSourceConfig = $configuration->getAuthSourceConfig();
29
        $this->checkAuthSources = $moduleConfig->getValue('checkAuthSources', true);
30
        $this->authSourceSpecifics = $moduleConfig->getValue('authSourceSpecifics', null);
31
        $this->setCategory('Authentication sources');
32
33
        parent::__construct($configuration);
34
    }
35
36
37
    /**
38
     * @return void
39
     */
40
    public function invokeTest(): void
41
    {
42
        if ($this->checkAuthSources === true) {
43
            $authSources = $this->authSourceConfig->getOptions();
44
        } elseif (is_array($this->checkAuthSources)) {
45
            $authSources = array_intersect($this->authSourceConfig->getOptions(), $this->checkAuthSources);
46
        } else { // false or invalid value
47
            return;
48
        }
49
50
        $configuration = $this->getConfiguration();
51
        $output = [];
52
53
        foreach ($authSources as $authSourceId) {
54
            $authSourceSpecifics = $this->getAuthSourceSpecifics($authSourceId);
55
            $authSourceData = $this->authSourceConfig->getValue($authSourceId);
56
            $input = [
57
                'authSourceId' => $authSourceId,
58
                'authSourceData' => $this->authSourceConfig->getValue($authSourceId),
59
                'authSourceSpecifics' => $authSourceSpecifics,
60
            ];
61
            $testData = new TestData($input);
62
63
            switch ($authSourceData[0]) {
64
                case 'ldap:LDAP':
65
                    $ldapTest = new AuthSource\Ldap($configuration, $testData);
66
                    $this->addTestResults($ldapTest->getTestResults());
67
                    $output[$authSourceId] = $ldapTest->getArrayizeTestResults();
68
                    break;
69
                case 'negotiate:Negotiate':
70
                    $negoTest = new AuthSource\Negotiate($configuration, $testData);
71
                    $this->addTestResults($negoTest->getTestResults());
72
73
                    // We need to do some convertions from Negotiate > LDAP
74
                    $this->convertAuthSourceData($authSourceData);
0 ignored issues
show
Bug introduced by
It seems like $authSourceData can also be of type null; however, parameter $authSourceData of SimpleSAML\Module\Monito...convertAuthSourceData() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
                    $this->convertAuthSourceData(/** @scrutinizer ignore-type */ $authSourceData);
Loading history...
75
                    $testData->setInput($authSourceData, 'authSourceData');
76
77
                    $ldapTest = new AuthSource\Ldap($configuration, $testData);
78
                    $this->addTestResults($ldapTest->getTestResults());
79
80
                    $output[$authSourceId] = array_merge(
81
                        $negoTest->getArrayizeTestResults(),
82
                        $ldapTest->getArrayizeTestResults()
83
                    );
84
                    break;
85
                case 'multiauth:MultiAuth':
86
                    // Relies on other authSources
87
                    continue 2;
88
                default:
89
                    // Not implemented
90
                    continue 2;
91
            }
92
        }
93
94
        $state = $this->calculateState();
95
        $testResult = new TestResult('Authentication sources');
96
        $testResult->setState($state);
97
        $testResult->setOutput($output);
98
        $this->setTestResult($testResult);
99
    }
100
101
102
    /**
103
     * @param string $authSourceId
104
     *
105
     * @return array|null
106
     */
107
    private function getAuthSourceSpecifics(string $authSourceId): ?array
108
    {
109
        if (is_array($this->authSourceSpecifics)) {
110
            if (array_key_exists($authSourceId, $this->authSourceSpecifics)) {
111
                return $this->authSourceSpecifics[$authSourceId];
112
            }
113
        }
114
        return null;
115
    }
116
117
118
    /**
119
     * @param array $authSourceData
120
     *
121
     * @return void
122
     */
123
    private function convertAuthSourceData(array &$authSourceData): void
124
    {
125
        // LDAP and Negotiate authSources use different names for equal properties
126
        // Hopefully this function can go away in SSP 2.0
127
        if (isset($authSourceData['debugLDAP'])) {
128
            $authSourceData['debug'] = $authSourceData['debugLDAP'];
129
            unset($authSourceData['debugLDAP']);
130
        }
131
        if (isset($authSourceData['adminUser'])) {
132
            $authSourceData['search.username'] = $authSourceData['adminUser'];
133
            unset($authSourceData['adminUser']);
134
        }
135
        if (isset($authSourceData['adminPassword'])) {
136
            $authSourceData['search.password'] = $authSourceData['adminPassword'];
137
            unset($authSourceData['adminPassword']);
138
        }
139
        if (isset($authSourceData['base'])) {
140
            $authSourceData['search.base'] = $authSourceData['base'];
141
            unset($authSourceData['base']);
142
        }
143
    }
144
}
145