Completed
Push — master ( 0ace3e...172714 )
by Tim
01:28
created

Connect::invokeTest()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 49
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 37
nc 8
nop 0
dl 0
loc 49
rs 8.5906
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\AuthSourc\Ldap;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestCase as TestCase;
7
8
final class Connect extends \SimpleSAML\Module\monitor\TestCase
0 ignored issues
show
Coding Style introduced by
Connect does not seem to conform to the naming convention (^sspmod_monitor_([A-Z][a-zA-Z0-9_]+)+$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
9
{
10
    private $connection = null;
11
12
    private $hostname = null;
13
    private $port = null;
14
    private $enableTls = null;
15
    private $timeout = null;
16
    private $referrals = null;
17
    private $debug = null;
18
19
    /*
20
     * @return void
21
     */
22
    protected function initialize()
23
    {
24
        $this->hostname = $this->getInput('hostname');
25
26
        $authsourceData = $this->getInput('authsource_data');
27
        $this->port = $authsourceData['port'];
28
        $this->enableTls = $authsourceData['enable_tls'];
29
        $this->timeout = isSet($authsourceData['timeout']) ? $authsourceData['timeout'] : 30;
30
        $this->referrals = isSet($authsourceData['referrals']) ? $authsourceData['referrals'] : true;
31
        $this->debug = isSet($authsourceData['debug']) ? $authsourceData['debug'] : false;
32
    }
33
34
    /*
35
     * @return void
36
     */
37
    protected function invokeTest()
38
    {
39
        try {
40
            $this->connection = new SimpleSAML_Auth_LDAP(
41
                $this->hostname,
42
                $this->enableTls,
43
                $this->debug,
44
                $this->timeout,
45
                $this->port,
46
                $this->referrals
47
            );
48
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class SimpleSAML\Module\monito...uthSourc\Ldap\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
49
            $this->setState(State::FATAL);
50
            $msg = str_replace('Library - LDAP __construct(): ', '', $e->getMessage());
51
            $connectString = $this->hostname;
52
            if (!preg_match('/^(ldap[s]?:\/\/(.*))$/', $this->hostname, $matches)) {
53
                $connectString = $this->hostname . ':' . $this->port;
54
            }
55
            $this->addMessage(State::FATAL, 'Network connection', $connectString, $msg);
56
            return;
57
        }
58
        $testsuite = $this->getTestSuite();
59
60
        // Actually connect and pull certificates whenever possible
61
        if (preg_match('/^(ldaps:\/\/(.*))$/', $this->hostname, $matches)) {
62
            $uri = str_replace('ldaps://', 'ssl://', $this->hostname) . ':636';
63
            $context = stream_context_create(array("ssl" => array("capture_peer_cert" => true, "verify_peer" => true)));
64
        } else {
65
            $uri = 'tcp://' . $this->hostname . ':' . $this->port;
66
            $context = stream_context_create();
67
        }
68
69
        $test = new TestCase\Network\ConnectUri($testsuite, array('uri' => $uri, 'context' => $context));
70
        $state = $test->getState();
71
72
        if ($state === State::OK) {
73
            $connection = $test->getOutput('connection');
74
            $cert = stream_context_get_params($connection);
75
            if (isSet($cert['options']['ssl']['peer_certificate'])) {
76
                $this->addOutput(openssl_x509_parse($cert['options']['ssl']['peer_certificate']), 'certData');
77
            }
78
            $this->setState(State::OK);
79
            $this->addMessage(State::OK, 'Network connection', $this->hostname, 'Connection established');
80
        } else {
81
            $this->setState(State::ERROR);
82
            $this->addMessage(State::ERROR, 'Network connection', $this->hostname, 'Connection failed');
83
        }
84
        $this->addOutput($this->connection, 'connection');
85
    }
86
}
87