Ldap   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 4 1
A setOption() 0 4 1
A setVersion() 0 4 1
A search() 0 4 1
A getEntries() 0 4 1
A searchAndGetEntries() 0 5 1
A bind() 0 4 1
A explodeDn() 0 4 1
A escape() 0 4 1
1
<?php 
2
3
namespace Integrations\Http\Middleware;
4
5
/**
6
 * Class Ldap
7
 * An object-orientated thin abstraction wrapper for common PHP LDAP functions.
8
 * Allows the standard LDAP functions to be mocked for testing.
9
 *
10
 * @package Integrations\Services
11
 */
12
class Ldap
13
{
14
15
    /**
16
     * Connect to a LDAP server.
17
     *
18
     * @param  string $hostName
19
     * @param  int    $port
20
     * @return resource
21
     */
22
    public function connect($hostName, $port)
23
    {
24
        return ldap_connect($hostName, $port);
25
    }
26
27
    /**
28
     * Set the value of a LDAP option for the given connection.
29
     *
30
     * @param  resource $ldapConnection
31
     * @param  int      $option
32
     * @param  mixed    $value
33
     * @return bool
34
     */
35
    public function setOption($ldapConnection, $option, $value)
36
    {
37
        return ldap_set_option($ldapConnection, $option, $value);
38
    }
39
40
    /**
41
     * Set the version number for the given ldap connection.
42
     *
43
     * @param  $ldapConnection
44
     * @param  $version
45
     * @return bool
46
     */
47
    public function setVersion($ldapConnection, $version)
48
    {
49
        return $this->setOption($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $version);
50
    }
51
52
    /**
53
     * Search LDAP tree using the provided filter.
54
     *
55
     * @param  resource   $ldapConnection
56
     * @param  string     $baseDn
57
     * @param  string     $filter
58
     * @param  array|null $attributes
59
     * @return resource
60
     */
61
    public function search($ldapConnection, $baseDn, $filter, array $attributes = null)
62
    {
63
        return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
64
    }
65
66
    /**
67
     * Get entries from an ldap search result.
68
     *
69
     * @param  resource $ldapConnection
70
     * @param  resource $ldapSearchResult
71
     * @return array
72
     */
73
    public function getEntries($ldapConnection, $ldapSearchResult)
74
    {
75
        return ldap_get_entries($ldapConnection, $ldapSearchResult);
76
    }
77
78
    /**
79
     * Search and get entries immediately.
80
     *
81
     * @param  resource   $ldapConnection
82
     * @param  string     $baseDn
83
     * @param  string     $filter
84
     * @param  array|null $attributes
85
     * @return resource
86
     */
87
    public function searchAndGetEntries($ldapConnection, $baseDn, $filter, array $attributes = null)
88
    {
89
        $search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
90
        return $this->getEntries($ldapConnection, $search);
91
    }
92
93
    /**
94
     * Bind to LDAP directory.
95
     *
96
     * @param  resource $ldapConnection
97
     * @param  string   $bindRdn
98
     * @param  string   $bindPassword
99
     * @return bool
100
     */
101
    public function bind($ldapConnection, $bindRdn = null, $bindPassword = null)
102
    {
103
        return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
104
    }
105
106
    /**
107
     * Explode a LDAP dn string into an array of components.
108
     *
109
     * @param  string $dn
110
     * @param  int    $withAttrib
111
     * @return array
112
     */
113
    public function explodeDn(string $dn, int $withAttrib)
114
    {
115
        return ldap_explode_dn($dn, $withAttrib);
116
    }
117
118
    /**
119
     * Escape a string for use in an LDAP filter.
120
     *
121
     * @param  string $value
122
     * @param  string $ignore
123
     * @param  int    $flags
124
     * @return string
125
     */
126
    public function escape(string $value, string $ignore = "", int $flags = 0)
127
    {
128
        return ldap_escape($value, $ignore, $flags);
129
    }
130
}
131