Passed
Branch monitor-3.0.x (ef3617)
by Tim
04:19
created

Search   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 36
dl 0
loc 80
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invokeTest() 0 23 3
A initialize() 0 28 4
1
<?php
2
3
namespace SimpleSAML\Module\Monitor\TestCase\AuthSource\Ldap;
4
5
use SimpleSAML\Module\Monitor\State;
6
use SimpleSAML\Module\Monitor\TestData;
7
use SimpleSAML\Module\Monitor\TestResult;
8
9
final class Search extends \SimpleSAML\Module\Monitor\TestCaseFactory
10
{
11
    /** @var \SimpleSAML\Auth\LDAP */
12
    private $connection;
13
14
    /** @var string */
15
    private $base;
16
17
    /** @var string */
18
    private $username;
19
20
    /** @var string */
21
    private $password;
22
23
    /** @var array */
24
    private $attributes = [];
25
26
27
    /**
28
     * @param \SimpleSAML\Module\Monitor\TestData $testData
29
     *
30
     * @return void
31
     */
32
    protected function initialize(TestData $testData): void
33
    {
34
        $authSourceData = $testData->getInputItem('authSourceData');
35
36
        // Just to be on the safe side, strip off any OU's and search to whole directory
37
        $base = $authSourceData->getArrayizeString('search.base', '<< unset >>');
38
        $base = is_array($base) ? $base[0] : $base;
39
        if (($i = intval(stripos($base, 'DC='))) > 0) {
40
            $base = substr($base, $i);
41
        }
42
        $this->base = $base;
43
44
        $username = $authSourceData->getString('search.username', '<< unset >>');
45
        $this->setSubject($username);
46
        if (strpos($username, 'DC=') > 0) {
47
            // We have been given a DN
48
            $username = ldap_explode_dn($username, 1);
49
            $this->username = $username[0];
50
            $this->attributes = ['cn'];
51
        } else {
52
            // We have been given a sAMAccountName
53
            $this->username = $username;
54
            $this->attributes = ['sAMAccountName'];
55
        }
56
        $this->password = $authSourceData->getString('search.password', '<< unset >>');
57
        $this->connection = $testData->getInputItem('connection');
58
59
        parent::initialize($testData);
60
    }
61
62
63
    /**
64
     * @return void
65
     */
66
    public function invokeTest(): void
67
    {
68
        try {
69
            $this->connection->searchfordn($this->base, $this->attributes, $this->username);
0 ignored issues
show
Bug introduced by
The method searchfordn() does not exist on SimpleSAML\Auth\LDAP. ( Ignorable by Annotation )

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

69
            $this->connection->/** @scrutinizer ignore-call */ 
70
                               searchfordn($this->base, $this->attributes, $this->username);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
        } catch (\SimpleSAML\Error\Error $error) {
71
            // Fallthru
72
        }
73
74
        $testResult = new TestResult('LDAP Search', $this->getSubject());
75
76
        if (isset($error)) {
77
            // When you feed str_replace a string, outcome will be string too, but Psalm doesn't see it that way
78
79
            /** @var string $msg */
80
            $msg = str_replace('Library - LDAP searchfordn(): ', '', $error->getMessage());
81
            $testResult->setState(State::ERROR);
82
            $testResult->setMessage($msg);
83
        } else {
84
            $testResult->setState(State::OK);
85
            $testResult->setMessage('Search succesful');
86
        }
87
88
        $this->setTestResult($testResult);
89
    }
90
}
91