Completed
Push — master ( b511fc...91dc95 )
by Tim
09:58 queued 08:30
created

Search::invokeTest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 18
rs 9.4285
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);
0 ignored issues
show
Unused Code introduced by
$distinguishedName 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...
81
            $msg = 'Search succesful';
82
            $state = State::OK;
83
        } catch (\SimpleSAML_Error_Error $e) {
84
            $msg = str_replace('Library - LDAP searchfordn(): ', '', $e->getMessage());
85
            $state = State::ERROR;
86
        }
87
88
        $testResult->setState($state);
89
        $testResult->setMessage($msg);
90
        $this->setTestResult($testResult);
91
    }
92
}
93