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

Configuration::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 13
rs 9.9666
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 Configuration extends \SimpleSAML\Module\Monitor\TestCaseFactory
10
{
11
    /** @var \SimpleSAML\Auth\LDAP|null */
12
    private $connection = null;
13
14
    /** @var string */
15
    private $hostname = '';
16
17
    /** @var integer */
18
    private $port = 636;
19
20
    /** @var bool */
21
    private $enableTls = false;
22
23
    /** @var integer */
24
    private $timeout = 3;
25
26
    /** @var bool */
27
    private $referrals = false;
28
29
    /** @var bool */
30
    private $debug = false;
31
32
33
    /**
34
     * @param \SimpleSAML\Module\Monitor\TestData $testData
35
     *
36
     * @return void
37
     */
38
    protected function initialize(TestData $testData): void
39
    {
40
        $authSourceData = $testData->getInputItem('authSourceData');
41
        $this->hostname = $authSourceData->getString('hostname', '<< unset >>');
42
        $this->port = $authSourceData->getInteger('port', 636);
43
        $this->enableTls = $authSourceData->getBoolean('enable_tls', false);
44
        $this->timeout = $authSourceData->getInteger('timeout', 3);
45
        $this->referrals = $authSourceData->getBoolean('referrals', false);
46
        $this->debug = $authSourceData->getBoolean('debug', false);
47
48
        $this->setSubject($this->hostname);
49
50
        parent::initialize($testData);
51
    }
52
53
54
    /**
55
     * @return void
56
     */
57
    public function invokeTest(): void
58
    {
59
        if (preg_match('/^(ldap[s]?:\/\/(.*))$/', $this->hostname, $matches)) {
60
            $connectString = $this->hostname;
61
        } else {
62
            $connectString = $this->hostname . ':' . $this->port;
63
        }
64
65
        $testResult = new TestResult('LDAP configuration', $connectString);
66
67
        try {
68
            $this->connection = new \SimpleSAML\Auth\LDAP(
69
                $this->hostname,
70
                $this->enableTls,
71
                $this->debug,
72
                $this->timeout,
73
                $this->port,
74
                $this->referrals
75
            );
76
            $state = State::OK;
77
        } catch (\Exception $error) {
78
            $state = State::FATAL;
79
        }
80
81
        if (isset($error)) {
82
            // When you feed str_replace a string, outcome will be string too, but Psalm doesn't see it that way
83
84
            /** @var string $msg */
85
            $msg = str_replace('Library - LDAP __construct(): ', '', $error->getMessage());
86
        } else {
87
            $msg = 'Configuration syntax OK';
88
            $testResult->addOutput($this->connection, 'connection');
89
        }
90
91
        $testResult->setState($state);
92
        $testResult->setMessage($msg);
93
        $this->setTestResult($testResult);
94
    }
95
}
96