Completed
Branch feature-unit-tests (9d0273)
by Tim
01:44
created

Configuration::invokeTest()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 24
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 34
rs 8.5806
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
9
final class Configuration extends \SimpleSAML\Module\monitor\TestCaseFactory
10
{
11
    /*
12
     * @var \SimpleSAML_Auth_LDAP|null
13
     */
14
    private $connection = null;
15
16
    /*
17
     * @var string|null
18
     */
19
    private $hostname = null;
20
21
    /*
22
     * @var integer
23
     */
24
    private $port = 636;
25
26
    /*
27
     * @var bool
28
     */
29
    private $enableTls = true;
30
31
    /*
32
     * @var integer
33
     */
34
    private $timeout = 3;
35
36
    /*
37
     * @var bool
38
     */
39
    private $referrals = false;
40
41
    /*
42
     * @var bool
43
     */
44
    private $debug = false;
45
46
47
    /*
48
     * @param TestData $testData
49
     *
50
     * @return void
51
     */
52
    protected function initialize($testData)
53
    {
54
        $authSourceData = $testData->getInputItem('authSourceData');
55
        if (isSet($authSourceData['hostname'])) {
56
            $this->hostname = $authSourceData['hostname'];
57
        }
58
        if (isSet($authSourceData['port'])) {
59
            $this->port = $authSourceData['port'];
60
        }
61
        if (isSet($authSourceData['enable_tls'])) {
62
            $this->enableTls = $authSourceData['enable_tls'];
63
        }
64
        if (isSet($authSourceData['timeout'])) {
65
            $this->timeout = $authSourceData['timeout'];
66
        }
67
        if (isSet($authSourceData['referrals'])) {
68
            $this->referrals = $authSourceData['referrals'];
69
        }
70
        if (isSet($authSourceData['debug'])) {
71
            $this->debug = $authSourceData['debug'];
72
        }
73
74
        $this->setSubject($this->hostname);
75
76
        parent::initialize($testData);
77
    }
78
79
80
    /*
81
     * @return void
82
     */
83
    public function invokeTest()
84
    {
85
        if (preg_match('/^(ldap[s]?:\/\/(.*))$/', $this->hostname, $matches)) {
86
            $connectString = $this->hostname;
87
        } else {
88
            $connectString = $this->hostname .':'.$this->port;
89
        }
90
91
        $testResult = new TestResult('LDAP configuration', $connectString);
92
93
        try {
94
            $this->connection = new \SimpleSAML_Auth_LDAP(
95
                $this->hostname,
96
                $this->enableTls,
97
                $this->debug,
98
                $this->timeout,
99
                $this->port,
100
                $this->referrals
101
            );
102
            $state = State::OK;
103
        } catch (\Exception $error) {
104
            $state = State::FATAL;
105
        }
106
107
        if (isSet($error)) {
108
            $msg = str_replace('Library - LDAP __construct(): ', '', $error->getMessage());
109
        } else {
110
            $msg = 'Configuration syntax OK';
111
            $testResult->addOutput($this->connection, 'connection');
112
        }
113
114
        $testResult->setState($state);
115
        $testResult->setMessage($msg);
116
        $this->setTestResult($testResult);
117
    }
118
}
119