ExtensionAdapter   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 38
dl 0
loc 159
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 7 1
A setOption() 0 5 1
A __construct() 0 8 3
A connect() 0 9 4
A getConnection() 0 7 2
A unsetErrorHandler() 0 3 1
A getDiagnosticMessage() 0 3 1
A setErrorHandler() 0 7 1
A unbind() 0 9 2
A getOption() 0 8 1
1
<?php
2
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
12
 * @link          https://cakephp.org CakePHP(tm) Project
13
 * @since         1.0.0
14
 * @license       https://opensource.org/licenses/mit-license.php MIT License
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phauthentic\Authentication\Identifier\Ldap;
20
21
use ErrorException;
22
use RuntimeException;
23
24
/**
25
 * Provides a very thin OOP wrapper around the ldap_* functions.
26
 *
27
 * We don't need and want a huge LDAP lib for our purpose.
28
 *
29
 * But this makes it easier to unit test code that is using LDAP because we can
30
 * mock it very easy. It also provides some convenience.
31
 */
32
class ExtensionAdapter implements AdapterInterface
33
{
34
    /**
35
     * LDAP Object
36
     *
37
     * @var \LDAP\Connection|null
0 ignored issues
show
Bug introduced by
The type LDAP\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
     */
39
    protected $connection;
40
41
    /**
42
     * Constructor
43
     *
44
     * @throws \RuntimeException
45
     */
46
    public function __construct()
47
    {
48
        if (!extension_loaded('ldap')) {
49
            throw new RuntimeException('You must enable the ldap extension to use the LDAP identifier.');
50
        }
51
52
        if (!defined('LDAP_OPT_DIAGNOSTIC_MESSAGE')) {
53
            define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032);
54
        }
55
    }
56
57
    /**
58
     * Bind to LDAP directory
59
     *
60
     * @param string $bind Bind rdn
61
     * @param string $password Bind password
62
     * @return bool
63
     * @throws \ErrorException
64
     */
65
    public function bind(string $bind, string $password): bool
66
    {
67
        $this->setErrorHandler();
68
        $result = ldap_bind($this->getConnection(), $bind, $password);
0 ignored issues
show
Bug introduced by
$this->getConnection() of type LDAP\Connection is incompatible with the type resource expected by parameter $ldap of ldap_bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $result = ldap_bind(/** @scrutinizer ignore-type */ $this->getConnection(), $bind, $password);
Loading history...
69
        $this->unsetErrorHandler();
70
71
        return $result;
72
    }
73
74
    /**
75
     * Get the LDAP connection
76
     *
77
     * @return mixed
78
     * @throws \RuntimeException If the connection is empty
79
     */
80
    public function getConnection()
81
    {
82
        if (empty($this->connection)) {
83
            throw new RuntimeException('You are not connected to a LDAP server.');
84
        }
85
86
        return $this->connection;
87
    }
88
89
    /**
90
     * Connect to an LDAP server
91
     *
92
     * @param string $host Hostname
93
     * @param int $port Port
94
     * @param array<int, int|bool|string> $options Additional LDAP options
95
     * @return void
96
     * @throws \ErrorException
97
     */
98
    public function connect(string $host, int $port, array $options): void
99
    {
100
        $this->setErrorHandler();
101
        $this->connection = ldap_connect($host, $port) ?: null;
0 ignored issues
show
Documentation Bug introduced by
It seems like ldap_connect($host, $port) ?: null can also be of type resource. However, the property $connection is declared as type LDAP\Connection|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
102
        $this->unsetErrorHandler();
103
104
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
105
            foreach ($options as $option => $value) {
106
                $this->setOption($option, $value);
107
            }
108
        }
109
    }
110
111
    /**
112
     *  Set the value of the given option
113
     *
114
     * @param int $option Option to set
115
     * @param int|bool|string $value The new value for the specified option
116
     * @return void
117
     */
118
    public function setOption(int $option, $value)
119
    {
120
        $this->setErrorHandler();
121
        ldap_set_option($this->getConnection(), $option, $value);
0 ignored issues
show
Bug introduced by
$this->getConnection() of type LDAP\Connection is incompatible with the type resource expected by parameter $ldap of ldap_set_option(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        ldap_set_option(/** @scrutinizer ignore-type */ $this->getConnection(), $option, $value);
Loading history...
122
        $this->unsetErrorHandler();
123
    }
124
125
    /**
126
     * Get the current value for given option
127
     *
128
     * @param int $option Option to get
129
     * @return mixed This will be set to the option value.
130
     */
131
    public function getOption($option)
132
    {
133
        $returnValue = null;
134
        $this->setErrorHandler();
135
        ldap_get_option($this->getConnection(), $option, $returnValue);
0 ignored issues
show
Bug introduced by
$this->getConnection() of type LDAP\Connection is incompatible with the type resource expected by parameter $ldap of ldap_get_option(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        ldap_get_option(/** @scrutinizer ignore-type */ $this->getConnection(), $option, $returnValue);
Loading history...
136
        $this->unsetErrorHandler();
137
138
        return $returnValue;
139
    }
140
141
    /**
142
     * Get the diagnostic message
143
     *
144
     * @return string|null
145
     */
146
    public function getDiagnosticMessage()
147
    {
148
        return $this->getOption(LDAP_OPT_DIAGNOSTIC_MESSAGE);
149
    }
150
151
    /**
152
     * Unbind from LDAP directory
153
     *
154
     * @return void
155
     */
156
    public function unbind()
157
    {
158
        $this->setErrorHandler();
159
        if ($this->connection) {
160
            ldap_unbind($this->connection);
0 ignored issues
show
Bug introduced by
$this->connection of type LDAP\Connection is incompatible with the type resource expected by parameter $ldap of ldap_unbind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
            ldap_unbind(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
161
        }
162
        $this->unsetErrorHandler();
163
164
        $this->connection = null;
165
    }
166
167
    /**
168
     * Set an error handler to turn LDAP errors into exceptions
169
     *
170
     * @return void
171
     * @throws \ErrorException
172
     */
173
    protected function setErrorHandler()
174
    {
175
        set_error_handler(
176
            function ($errorNumber, $errorText) {
177
                 throw new ErrorException($errorText);
178
            },
179
            E_ALL
180
        );
181
    }
182
183
    /**
184
     * Restore the error handler
185
     *
186
     * @return void
187
     */
188
    protected function unsetErrorHandler()
189
    {
190
        restore_error_handler();
191
    }
192
}
193