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

Bind::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 Bind extends \SimpleSAML\Module\Monitor\TestCaseFactory
10
{
11
    /** @var \SimpleSAML\Module\ldap\Auth\Ldap */
12
    private $connection;
13
14
    /** @var string */
15
    private $username;
16
17
    /** @var string */
18
    private $password;
19
20
21
    /**
22
     * @param \SimpleSAML\Module\Monitor\TestData $testData
23
     *
24
     * @return void
25
     */
26
    protected function initialize(TestData $testData): void
27
    {
28
        $this->connection = $testData->getInputItem('connection');
29
        $authSourceData = $testData->getInputItem('authSourceData');
30
31
        $this->username = $authSourceData->getString('search.username', '<< unset >>');
32
        $this->password = $authSourceData->getString('search.password', '<< unset >>');
33
34
        parent::initialize($testData);
35
    }
36
37
   
38
    /**
39
     * @return void
40
     */
41
    public function invokeTest(): void
42
    {
43
        try {
44
            $bind = $this->connection->bind($this->username, $this->password);
45
        } catch (\Exception $error) {
46
            // Fallthru
47
        }
48
49
        $testResult = new TestResult('LDAP Bind', $this->username);
50
        if (isset($error)) {
51
            // When you feed str_replace a string, outcome will be string too, but Psalm doesn't see it that way
52
53
            /** @var string $msg */
54
            $msg = str_replace('Library - LDAP bind(): ', '', $error->getMessage());
55
            $testResult->setState(State::FATAL);
56
        } elseif ($bind === true) {
57
            $msg = 'Bind succesful';
58
            $testResult->setState(State::OK);
59
        } else {
60
            $msg = 'Authentication failed';
61
            $testResult->setState(State::ERROR);
62
        }
63
64
        $testResult->setMessage($msg);
65
        $this->setTestResult($testResult);
66
    }
67
}
68