Query::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Foundation\Ldap;
13
14
/**
15
 * LDAP Query.
16
 */
17
class Query
18
{
19
    /**
20
     * LDAP client.
21
     *
22
     * @var Client
23
     */
24
    protected $client = null;
25
26
    /**
27
     * Query result.
28
     *
29
     * @var array
30
     */
31
    protected $entries = [];
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param Client $client
37
     */
38
    public function __construct(Client $client)
39
    {
40
        $this->client = $client;
41
    }
42
43
    /**
44
     * Execute query.
45
     *
46
     * @param string $baseDn
47
     * @param string $filter
48
     * @param array  $attributes
49
     *
50
     * @return Query
51
     */
52
    public function execute($baseDn, $filter, array $attributes)
53
    {
54
        if (DEBUG && $this->client->hasLogger()) {
55
            $this->client->getLogger()->debug('BaseDN='.$baseDn);
56
            $this->client->getLogger()->debug('Filter='.$filter);
57
            $this->client->getLogger()->debug('Attributes='.implode(', ', $attributes));
58
        }
59
60
        $sr = ldap_search($this->client->getConnection(), $baseDn, $filter, $attributes);
61
        if ($sr === false) {
62
            return $this;
63
        }
64
65
        $entries = ldap_get_entries($this->client->getConnection(), $sr);
66
        if ($entries === false || count($entries) === 0 || $entries['count'] == 0) {
67
            return $this;
68
        }
69
70
        $this->entries = $entries;
71
72
        if (DEBUG && $this->client->hasLogger()) {
73
            $this->client->getLogger()->debug('NbEntries='.$entries['count']);
74
        }
75
76
        return $this;
77
    }
78
79
    /**
80
     * Return true if the query returned a result.
81
     *
82
     * @return bool
83
     */
84
    public function hasResult()
85
    {
86
        return !empty($this->entries);
87
    }
88
89
    /**
90
     * Get LDAP Entries.
91
     *
92
     * @return Entries
93
     */
94
    public function getEntries()
95
    {
96
        return new Entries($this->entries);
97
    }
98
}
99