Completed
Push — master ( 710cf8...455f55 )
by Tim
05:18 queued 03:42
created

Configuration   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
C initialize() 0 26 7
B invokeTest() 0 33 4
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\TestCase as TestCase;
7
use \SimpleSAML\Module\monitor\TestData as TestData;
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
     * @param TestData $testData
48
     *
49
     * @return void
50
     */
51
    protected function initialize($testData)
52
    {
53
        $authSourceData = $testData->getInput('authSourceData');
54
        if (isSet($authSourceData['hostname'])) {
55
            $this->hostname = $authSourceData['hostname'];
56
        }
57
        if (isSet($authSourceData['port'])) {
58
            $this->port = $authSourceData['port'];
59
        }
60
        if (isSet($authSourceData['enable_tls'])) {
61
            $this->enableTls = $authSourceData['enable_tls'];
62
        }
63
        if (isSet($authSourceData['timeout'])) {
64
            $this->timeout = $authSourceData['timeout'];
65
        }
66
        if (isSet($authSourceData['referrals'])) {
67
            $this->referrals = $authSourceData['referrals'];
68
        }
69
        if (isSet($authSourceData['debug'])) {
70
            $this->debug = $authSourceData['debug'];
71
        }
72
73
        $this->setSubject($this->hostname);
74
75
        parent::initialize($testData);
76
    }
77
78
    /*
79
     * @return void
80
     */
81
    protected function invokeTest()
82
    {
83
        if (preg_match('/^(ldap[s]?:\/\/(.*))$/', $this->hostname, $matches)) {
84
            $connectString = $this->hostname;
85
        } else {
86
            $connectString = $this->hostname . ':' . $this->port;
87
        }
88
89
        $testResult = new TestResult('LDAP configuration', $connectString);
90
91
        try {
92
            $this->connection = new \SimpleSAML_Auth_LDAP(
93
                $this->hostname,
94
                $this->enableTls,
95
                $this->debug,
96
                $this->timeout,
97
                $this->port,
98
                $this->referrals
99
            );
100
            $state = State::OK;
101
            $msg = 'Configuration syntax OK';
102
        } catch (\Exception $e) {
103
            $state = State::FATAL;
104
            $msg = str_replace('Library - LDAP __construct(): ', '', $e->getMessage());
105
        }
106
107
        if ($state === State::OK) {
108
            $this->addOutput($this->connection, 'connection');
109
        }
110
        $testResult->setState($state);
111
        $testResult->setMessage($msg);
112
        $this->setTestResult($testResult);
113
    }
114
}
115