| Conditions | 16 |
| Paths | 129 |
| Total Lines | 88 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 88 | public function process(array &$state): void |
||
| 89 | { |
||
| 90 | Assert::keyExists($state, 'Attributes'); |
||
| 91 | $attributes = &$state['Attributes']; |
||
| 92 | |||
| 93 | $ldapUtils = new LdapUtils(); |
||
| 94 | |||
| 95 | // perform a merge on the ldap_search_filter |
||
| 96 | // loop over the attributes and build the search and replace arrays |
||
| 97 | $arrSearch = $arrReplace = []; |
||
| 98 | foreach ($attributes as $attr => $val) { |
||
| 99 | $arrSearch[] = '%' . $attr . '%'; |
||
| 100 | |||
| 101 | if (strlen($val[0]) > 0) { |
||
| 102 | $arrReplace[] = $ldapUtils->escapeFilterValue($val[0]); |
||
| 103 | } else { |
||
| 104 | $arrReplace[] = ''; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | // merge the attributes into the ldap_search_filter |
||
| 109 | $filter = str_replace($arrSearch, $arrReplace, $this->searchFilter); |
||
| 110 | if (strpos($filter, '%') !== false) { |
||
| 111 | Logger::info(sprintf( |
||
| 112 | '%s: AttributeAddFromLDAP: There are non-existing attributes in the search filter. (%s)' |
||
| 113 | $this->title, |
||
|
|
|||
| 114 | $filter |
||
| 115 | )); |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | $ldap = $ldapUtils->bind($this->ldapServers, $this->searchUsername, $this->searchPassword); |
||
| 120 | |||
| 121 | $options = [ |
||
| 122 | 'scope' => $this->config->getString('search.scope', QUERY::SCOPE_SUB), |
||
| 123 | 'timeout' => $this->config->getInteger('timeout', 3), |
||
| 124 | ]; |
||
| 125 | |||
| 126 | $entries = $ldapUtils->searchForMultiple( |
||
| 127 | $ldap, |
||
| 128 | $this->searchBase, |
||
| 129 | $filter, |
||
| 130 | $options, |
||
| 131 | true |
||
| 132 | ); |
||
| 133 | |||
| 134 | $results = []; |
||
| 135 | foreach ($entries as $entry) { |
||
| 136 | $tmp = array_intersect_key( |
||
| 137 | $entry->getAttributes(), |
||
| 138 | array_fill_keys(array_values($this->searchAttributes), null) |
||
| 139 | ); |
||
| 140 | |||
| 141 | $binaries = array_intersect( |
||
| 142 | array_keys($tmp), |
||
| 143 | $this->binaryAttributes, |
||
| 144 | ); |
||
| 145 | foreach ($binaries as $binary) { |
||
| 146 | $tmp[$binary] = array_map('base64_encode', $entry->getAttribute($binary)); |
||
| 147 | } |
||
| 148 | |||
| 149 | $results[] = $tmp; |
||
| 150 | } |
||
| 151 | |||
| 152 | // handle [multiple] values |
||
| 153 | foreach ($results as $entry) { |
||
| 154 | foreach ($this->searchAttributes as $target => $name) { |
||
| 155 | // If there is no mapping defined, just use the name of the LDAP-attribute as a target |
||
| 156 | if (is_int($target)) { |
||
| 157 | $target = $name; |
||
| 158 | } |
||
| 159 | |||
| 160 | if (isset($attributes[$target]) && $this->attrPolicy === 'replace') { |
||
| 161 | unset($attributes[$target]); |
||
| 162 | } |
||
| 163 | |||
| 164 | if (isset($entry[$name])) { |
||
| 165 | if (isset($attributes[$target])) { |
||
| 166 | foreach (array_values($entry[$name]) as $value) { |
||
| 167 | if ($this->attrPolicy === 'merge') { |
||
| 168 | if (!in_array($value, $attributes[$target], true)) { |
||
| 169 | $attributes[$target][] = $value; |
||
| 170 | } |
||
| 171 | } else { |
||
| 172 | $attributes[$target][] = $value; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } else { |
||
| 176 | $attributes[$target] = array_values($entry[$name]); |
||
| 183 |