Completed
Push — master ( 29977d...7face0 )
by Tim
02:36 queued 01:00
created

Search   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 92
rs 10
wmc 8
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B initialize() 0 28 4
B invokeTest() 0 27 4
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\AuthSource\Ldap;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestData as TestData;
7
use \SimpleSAML\Module\monitor\TestResult as TestResult;
8
use \SimpleSAML\Module\monitor\TestSuite as TestSuite;
9
10
final class Search extends \SimpleSAML\Module\monitor\TestCaseFactory
11
{
12
    /*
13
     * @var \SimpleSAML_Auth_LDAP|null
14
     */
15
    private $connection = null;
16
17
    /*
18
     * @var string|null
19
     */
20
    private $base = null;
21
22
    /*
23
     * @var string|null
24
     */
25
    private $username = null;
26
27
    /*
28
     * @var string|null
29
     */
30
    private $password = null;
31
32
    /*
33
     * @var array
34
     */
35
    private $attributes = array();
36
37
    /*
38
     * @param TestData $testData
39
     *
40
     * @return void
41
     */
42
    protected function initialize($testData)
43
    {
44
        $authSourceData = $testData->getInput('authSourceData');
45
46
        $base = $authSourceData['search.base'];
47
        $base = is_array($base) ? $base[0] : $base;
48
        if (($i = stripos($base, 'DC=')) > 0) {
49
            $base = substr($base, $i);
50
        }
51
        $this->base = $base;
52
53
        $username = $authSourceData['search.username'];
54
        $this->setSubject($username);
55
        if (strpos($username, 'DC=') > 0) {
56
            // We have been given a DN
57
            $username = ldap_explode_dn($username, 1);
58
            $this->username = $username[0];
59
            $this->attributes = array('cn');
60
        } else {
61
            // We have been given a sAMAccountName
62
            $this->username = $username;
63
            $this->attributes = array('sAMAccountName');
64
        }
65
        $this->password = $authSourceData['search.password'];
66
        $this->connection = $testData->getInput('connection');
67
68
        parent::initialize($testData);
69
    }
70
71
    /*
72
     * @return void
73
     */
74
    protected function invokeTest()
75
    {
76
        $connection = $this->connection;
77
        $testResult = new TestResult('LDAP Search', $this->getSubject());
78
79
        try {
80
            $distinguishedName = $connection->searchfordn($this->base, $this->attributes, $this->username);
81
        } catch (\Exception $e) {
82
            $distinguishedName = false;
83
        }
84
85
        if (is_string($distinguishedName)) {
86
            $msg = 'Search succesful';
87
            $state = State::OK;
88
        } elseif ($distinguishedName === false) {
89
            $msg = str_replace('Library - LDAP searchfordn(): ', '', $e->getMessage());
0 ignored issues
show
Bug introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
90
            $state = State::ERROR;
91
        } else {
92
            // Search for configured search.username returned no results; Shouldn't happen!!
93
            $msg = 'Invalid search result';
94
            $state = State::WARNING;
95
        }
96
97
        $testResult->setState($state);
98
        $testResult->setMessage($msg);
99
        $this->setTestResult($testResult);
100
    }
101
}
102