Passed
Push — master ( f275b9...3d5dc7 )
by Tim
02:55
created

ConnectorFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 23
c 1
b 0
f 0
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromAuthSource() 0 37 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\ldap;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Error;
10
use SimpleSAML\Module\ldap\Connector;
11
12
use function current;
13
use function sprintf;
14
15
class ConnectorFactory
16
{
17
    /**
18
     * @param string $authSource
19
     * @return \SimpleSAML\Module\ldap\ConnectorInterface
20
     */
21
    public static function fromAuthSource(string $authSource): ConnectorInterface
22
    {
23
        // Get the authsources file, which should contain the config
24
        $authSources = Configuration::getConfig('authsources.php');
25
26
        // Verify that the authsource config exists
27
        if (!$authSources->hasValue($authSource)) {
28
            throw new Error\Exception(sprintf(
29
                'Authsource [%s] not found in authsources.php',
30
                $authSource
31
            ));
32
        }
33
34
        // Get just the specified authsource config values
35
        $ldapConfig = $authSources->getConfigItem($authSource);
36
        $type = $ldapConfig->toArray();
37
        Assert::oneOf(current($type), ['ldap:Ldap']);
38
39
        $encryption = $ldapConfig->getOptionalString('encryption', 'ssl');
40
        Assert::oneOf($encryption, ['none', 'ssl', 'tls']);
41
42
        $version = $ldapConfig->getOptionalInteger('version', 3);
43
        Assert::positiveInteger($version);
44
45
        $class = $ldapConfig->getOptionalString('connector', Connector\Ldap::class);
46
        Assert::classExists($class);
47
        Assert::implementsInterface($class, ConnectorInterface::class);
48
49
        return /** @psalm-var \SimpleSAML\Module\ldap\ConnectionInterface */ new $class(
50
            $ldapConfig->getString('connection_string'),
51
            $encryption,
52
            $version,
53
            $ldapConfig->getOptionalString('extension', 'ext_ldap'),
54
            $ldapConfig->getOptionalBoolean('debug', false),
55
            [
56
                'network_timeout' => $ldapConfig->getOptionalInteger('timeout', 3),
57
                'referrals' => $ldapConfig->getOptionalBoolean('referrals', false),
58
            ]
59
        );
60
    }
61
}
62