LdapObjectChoiceTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 90
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setClosureOrQuery() 0 8 3
A getLdapValuesForChoices() 0 10 2
B getLdapObjectsByQuery() 0 22 5
1
<?php
2
/**
3
 * This file is part of the LdapToolsBundle package.
4
 *
5
 * (c) Chad Sikorra <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LdapTools\Bundle\LdapToolsBundle\Form\ChoiceLoader;
12
13
use LdapTools\LdapManager;
14
use LdapTools\Object\LdapObject;
15
use LdapTools\Query\LdapQueryBuilder;
16
17
/**
18
 * Removes duplication between the Choice List and the Choice Loader.
19
 *
20
 * @author Chad Sikorra <[email protected]>
21
 */
22
trait LdapObjectChoiceTrait
23
{
24
    /**
25
     * @var \Closure|null
26
     */
27
    protected $queryCallback;
28
29
    /**
30
     * @var LdapQueryBuilder|null
31
     */
32
    protected $ldapQueryBuilder;
33
34
    /**
35
     * @var LdapManager
36
     */
37
    protected $ldap;
38
39
    /**
40
     * @var string
41
     */
42
    protected $id;
43
44
    /**
45
     * @var string
46
     */
47
    protected $type;
48
49
    /**
50
     * @var string
51
     */
52
    protected $labelAttribute;
53
54
    /**
55
     * @param LdapQueryBuilder|\Closure|null $query
56
     */
57
    protected function setClosureOrQuery($query)
58
    {
59
        if ($query instanceof \Closure) {
60
            $this->queryCallback = $query;
61
        } elseif ($query instanceof LdapQueryBuilder) {
62
            $this->ldapQueryBuilder = $query;
63
        }
64
    }
65
66
    /**
67
     * Get the values of a set of choices.
68
     *
69
     * @param LdapObject[] $choices
70
     * @return array
71
     */
72
    protected function getLdapValuesForChoices(LdapObject ...$choices)
73
    {
74
        $values = [];
75
76
        foreach ($choices as $i => $ldapObject) {
77
            $values[$i] = (string) $ldapObject->get($this->id);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $ldapObject (of type array<integer,object<Lda...ols\Object\LdapObject>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
78
        }
79
80
        return $values;
81
    }
82
83
    /**
84
     * Get the LDAP objects from LDAP. Optionally only get those specified by the passed values.
85
     *
86
     * @param array $values The values used to narrow the LDAP query.
87
     * @return \LdapTools\Object\LdapObjectCollection
88
     */
89
    protected function getLdapObjectsByQuery($values = [])
90
    {
91
        if (!$this->ldapQueryBuilder) {
92
            $query = $this->ldap->buildLdapQuery()
93
                ->select([$this->id, $this->labelAttribute])
94
                ->from($this->type);
95
        } else {
96
            $query = clone $this->ldapQueryBuilder;
97
        }
98
99
        if (!empty($values)) {
100
            foreach ($values as $value) {
101
                $query->orWhere([$this->id => $value]);
102
            }
103
        }
104
        if ($this->queryCallback) {
105
            $closure = $this->queryCallback;
106
            $closure($query);
107
        }
108
109
        return $query->getLdapQuery()->getResult();
110
    }
111
}
112