| Conditions | 17 |
| Paths | 390 |
| Total Lines | 153 |
| Code Lines | 101 |
| 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 |
||
| 72 | protected function login(string $username, string $password): array |
||
| 73 | { |
||
| 74 | $encryption = $this->ldapConfig->getString('encryption', 'ssl'); |
||
| 75 | Assert::oneOf($encryption, ['none', 'ssl', 'tls']); |
||
| 76 | |||
| 77 | $ldapServers = []; |
||
| 78 | foreach (explode(' ', $this->ldapConfig->getString('connection_string')) as $connection_string) { |
||
| 79 | Assert::regex($connection_string, '#^ldap[s]?:\/\/#'); |
||
| 80 | |||
| 81 | $ldap_servers[] = LdapObject::create( |
||
| 82 | $this->ldapConfig->getString('extension', 'ext_ldap'), |
||
| 83 | [ |
||
| 84 | 'connection_string' => $connection_string, |
||
| 85 | 'encryption' => 'ssl', |
||
| 86 | ] |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | $searchScope = $this->ldapConfig->getString('search.scope', Query::SCOPE_SUB); |
||
| 91 | Assert::oneOf($searchScope, [Query::SCOPE_BASE, Query::SCOPE_ONE, Query::SCOPE_SUB]); |
||
| 92 | |||
| 93 | $referrals = $this->ldapConfig->getValue('referrals', Query::DEREF_NEVER); |
||
| 94 | Assert::oneOf($referrals, [Query::DEREF_ALWAYS, Query::DEREF_NEVER, Query::DEREF_FINDING, Query::DEREF_SEARCHING]); |
||
| 95 | |||
| 96 | $timeout = $this->ldapConfig->getString('timeout', 3); |
||
| 97 | $searchBase = $this->ldapConfig->getArray('search.base'); |
||
| 98 | $options = [ |
||
| 99 | 'scope' => $searchScope, |
||
| 100 | 'timeout' => $timeout, |
||
| 101 | 'deref' => $referrals, |
||
| 102 | ]; |
||
| 103 | |||
| 104 | $searchEnable = $this->ldapConfig->getBoolean('search.enable', false); |
||
| 105 | if ($searchEnable === false) { |
||
| 106 | $dnPattern = $this->ldapConfig->getString('dnpattern'); |
||
| 107 | $dn = str_replace('%username%', $username, $dnPattern); |
||
| 108 | |||
| 109 | $filter = ''; |
||
| 110 | } else { |
||
| 111 | $searchUsername = $this->ldapConfig->getString('search.username'); |
||
| 112 | Assert::notWhitespaceOnly($searchUsername); |
||
| 113 | |||
| 114 | $searchPassword = $this->ldapConfig->getString('search.password', null); |
||
| 115 | Assert::nullOrnotWhitespaceOnly($searchPassword); |
||
| 116 | |||
| 117 | $searchAttributes = $this->ldapConfig->getArray('search.attributes'); |
||
| 118 | $searchFilter = $this->ldapConfig->getString('search.filter', null); |
||
| 119 | |||
| 120 | $ldap = $this->bind($ldapServers, $searchUsername, $searchPassword); |
||
| 121 | |||
| 122 | $filter = ''; |
||
| 123 | foreach ($searchAttributes as $attr) { |
||
| 124 | $filter .= '(' . $attr . '=' . $username . ')'; |
||
| 125 | } |
||
| 126 | $filter = '(|' . $filter . ')'; |
||
| 127 | |||
| 128 | // Append LDAP filters if defined |
||
| 129 | if ($searchFilter !== null) { |
||
| 130 | $filter = "(&" . $filter . "" . $searchFilter . ")"; |
||
| 131 | } |
||
| 132 | |||
| 133 | $entry = null; |
||
| 134 | foreach ($searchBase as $base) { |
||
| 135 | $query = $ldap->query($base, $filter, $options); |
||
| 136 | $result = $query->execute(); |
||
| 137 | $result = is_array($result) ? $result : $result->toArray(); |
||
| 138 | |||
| 139 | if (count($result) > 1) { |
||
| 140 | throw new Error\Exception( |
||
| 141 | sprintf( |
||
| 142 | "Library - LDAP search(): Found %d entries searching base '%s' for '%s'", |
||
| 143 | count($result), |
||
| 144 | $base, |
||
| 145 | $filter, |
||
| 146 | ) |
||
| 147 | ); |
||
| 148 | } elseif (count($result) === 1) { |
||
| 149 | $entry = array_pop($result); |
||
| 150 | break; |
||
| 151 | } else { |
||
| 152 | Logger::debug( |
||
| 153 | sprintf( |
||
| 154 | "Library - LDAP search(): Found no entries searching base '%s' for '%s'", |
||
| 155 | count($result), |
||
| 156 | $base, |
||
| 157 | $filter, |
||
| 158 | ) |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($entry === null) { |
||
| 164 | throw new Error\UserNotFound("User not found"); |
||
| 165 | } |
||
| 166 | |||
| 167 | $dn = $entry->getDn(); |
||
| 168 | } |
||
| 169 | |||
| 170 | $ldap = $this->bind($ldapServers, $dn, $password); |
||
| 171 | |||
| 172 | $entry = null; |
||
| 173 | foreach ($searchBase as $base) { |
||
| 174 | $query = $ldap->query($base, sprintf('(distinguishedName=%s)', $dn), $options); |
||
| 175 | $result = $query->execute(); |
||
| 176 | $result = is_array($result) ? $result : $result->toArray(); |
||
| 177 | |||
| 178 | if (count($result) > 1) { |
||
| 179 | throw new Error\Exception( |
||
| 180 | sprintf( |
||
| 181 | "Library - LDAP search(): Found %d entries searching base '%s' for '%s'", |
||
| 182 | count($result), |
||
| 183 | $base, |
||
| 184 | $filter, |
||
| 185 | ) |
||
| 186 | ); |
||
| 187 | } elseif (count($result) === 1) { |
||
| 188 | $entry = array_pop($result); |
||
| 189 | break; |
||
| 190 | } else { |
||
| 191 | Logger::debug( |
||
| 192 | sprintf( |
||
| 193 | "Library - LDAP search(): Found no entries searching base '%s' for '%s'", |
||
| 194 | count($result), |
||
| 195 | $base, |
||
| 196 | $filter, |
||
| 197 | ) |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | if ($entry === null) { |
||
| 203 | throw new Error\UserNotFound("User not found"); |
||
| 204 | } |
||
| 205 | |||
| 206 | $attributes = $this->ldapConfig->getArray('attributes', []); |
||
| 207 | if ($attributes === ['*']) { |
||
| 208 | $result = $entry->getAttributes(); |
||
| 209 | } else { |
||
| 210 | $result = array_intersect_key( |
||
| 211 | $entry->getAttributes(), |
||
| 212 | array_fill_keys(array_values($attributes), null) |
||
| 213 | ); |
||
| 214 | } |
||
| 215 | |||
| 216 | $binaries = array_intersect( |
||
| 217 | array_keys($result), |
||
| 218 | $this->ldapConfig->getArray('attributes.binary', []), |
||
| 219 | ); |
||
| 220 | foreach ($binaries as $binary) { |
||
| 221 | $result[$binary] = array_map('base64_encode', $result[$binary]); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $result; |
||
| 225 | } |
||
| 245 |