Completed
Push — develop ( c8a7a8...b27583 )
by Bart
03:01
created

LdapService::bind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Kuleuven\AuthenticationBundle\Service;
4
5
use Symfony\Component\Ldap\Ldap;
6
7
class LdapService
8
{
9
    /**
10
     * @var Ldap
11
     */
12
    protected $ldap;
13
14
    /**
15
     * @var string
16
     */
17
    protected $rdn;
18
19
    /**
20
     * @var string
21
     */
22
    protected $password;
23
    
24
    /**
25
     * @var string
26
     */
27
    protected $base;
28
29
    /**
30
     * @param Ldap $ldap
31
     * @param string $rdn
32
     * @param string $password
33
     * @param string $base
34
     */
35
    public function __construct(
36
        Ldap $ldap,
37
        $rdn = '',
38
        $password = '',
39
        $base = 'ou=people,dc=kuleuven,dc=be'
40
    )
41
    {
42
        $this->ldap = $ldap;
43
        $this->rdn = $rdn;
44
        $this->password = $password;
45
        $this->base = $base;
46
    }
47
48
    /**
49
     *
50
     */
51
    protected function bind()
52
    {
53
        $this->ldap->bind($this->rdn, $this->password);
54
    }
55
56
    /**
57
     * @param array $data
58
     * @param string $type
59
     * @param string $format
60
     * @return string
61
     */
62
    public function createFilter(array $data = [], $type = '&', $format = '%s')
63
    {
64
        $string = '';
65
        foreach ($data as $key => $value) {
66
            if (is_array($value)) {
67
                if (0 === count(array_filter(array_keys($value), 'is_numeric'))) {
68
                    $string .= $this->createFilter($value, $key, $format);
69
                } else {
70
                    $string .= '(|(' . $key . '=' . implode(')(' . $key . '=', $value) . '))';
71
                }
72
            } else {
73
                $value = sprintf($format, $value);
74
                $string .= "(${key}=${value})";
75
            }
76
        }
77
        return "(${type}${string})";
78
    }
79
80
    /**
81
     * @param $filter
82
     * @param array $attributes
83
     * @param bool $attrsOnly
84
     * @param int $sizeLimit
85
     * @param int $timeLimit
86
     * @return mixed
87
     */
88
    public function search($filter, $attributes = [], $attrsOnly = false, $sizeLimit = 0, $timeLimit = 0)
89
    {
90
        // Create filter
91
        if (is_array($filter)) {
92
            $filter = $this->createFilter($filter, '&', '%s');
93
        }
94
95
        // Catch empty attributes
96
        if (empty($attributes)) {
97
            $attributes = [];
98
        }
99
100
        // Bind
101
        $this->bind();
102
103
        // Search
104
        $results = $this->ldap->query($this->base, $filter, [
105
            'attrsOnly' => $attrsOnly,
106
            'filter'    => $attributes,
107
//            'maxItems'  => $maxItems,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
108
            'sizeLimit' => $sizeLimit,
109
            'timeout'   => $timeLimit,
110
        ])->execute();
111
112
        // Return
113
        return $results;
114
    }
115
116
    /**
117
     * @param $filter
118
     * @param array $attributes
119
     * @param bool $attrsOnly
120
     * @param int $sizeLimit
121
     * @param int $timeLimit
122
     * @return mixed
123
     */
124
    public function fuzzy($filter, $attributes = [], $attrsOnly = false, $sizeLimit = 0, $timeLimit = 0)
125
    {
126
        if (is_array($filter)) {
127
            $filter = $this->createFilter($filter, '&', '*%s*');
128
        }
129
        return $this->search($filter, $attributes, $attrsOnly, $sizeLimit, $timeLimit);
130
    }
131
}
132