| Conditions | 8 |
| Paths | 17 |
| Total Lines | 85 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 | $timeout = $this->ldapConfig->getOptionalInteger('timeout', 3); |
||
| 75 | Assert::positiveInteger($timeout); |
||
| 76 | |||
| 77 | $searchScope = $this->ldapConfig->getOptionalString('search.scope', Query::SCOPE_SUB); |
||
| 78 | Assert::oneOf($searchScope, [Query::SCOPE_BASE, Query::SCOPE_ONE, Query::SCOPE_SUB]); |
||
| 79 | |||
| 80 | $timeout = $this->ldapConfig->getOptionalInteger('timeout', 3); |
||
| 81 | $searchBase = $this->ldapConfig->getArray('search.base'); |
||
| 82 | $options = [ |
||
| 83 | 'scope' => $searchScope, |
||
| 84 | 'timeout' => $timeout, |
||
| 85 | ]; |
||
| 86 | |||
| 87 | $connector = $this->resolveConnector(); |
||
|
|
|||
| 88 | |||
| 89 | $searchEnable = $this->ldapConfig->getOptionalBoolean('search.enable', false); |
||
| 90 | if ($searchEnable === false) { |
||
| 91 | $dnPattern = $this->ldapConfig->getString('dnpattern'); |
||
| 92 | $dn = str_replace('%username%', $username, $dnPattern); |
||
| 93 | } else { |
||
| 94 | $searchUsername = $this->ldapConfig->getString('search.username'); |
||
| 95 | Assert::notWhitespaceOnly($searchUsername); |
||
| 96 | |||
| 97 | $searchPassword = $this->ldapConfig->getOptionalString('search.password', null); |
||
| 98 | Assert::nullOrnotWhitespaceOnly($searchPassword); |
||
| 99 | |||
| 100 | $searchAttributes = $this->ldapConfig->getArray('search.attributes'); |
||
| 101 | $searchFilter = $this->ldapConfig->getOptionalString('search.filter', null); |
||
| 102 | |||
| 103 | try { |
||
| 104 | $this->connector->bind($searchUsername, $searchPassword); |
||
| 105 | } catch (Error\Error $e) { |
||
| 106 | throw new Error\Exception("Unable to bind using the configured search.username and search.password."); |
||
| 107 | } |
||
| 108 | |||
| 109 | $filter = ''; |
||
| 110 | foreach ($searchAttributes as $attr) { |
||
| 111 | $filter .= '(' . $attr . '=' . $username . ')'; |
||
| 112 | } |
||
| 113 | $filter = '(|' . $filter . ')'; |
||
| 114 | |||
| 115 | // Append LDAP filters if defined |
||
| 116 | if ($searchFilter !== null) { |
||
| 117 | $filter = "(&" . $filter . $searchFilter . ")"; |
||
| 118 | } |
||
| 119 | |||
| 120 | try { |
||
| 121 | /** @psalm-var \Symfony\Component\Ldap\Entry $entry */ |
||
| 122 | $entry = $this->connector->search($searchBase, $filter, $options, false); |
||
| 123 | $dn = $entry->getDn(); |
||
| 124 | } catch (Error\Exception $e) { |
||
| 125 | throw new Error\Error('WRONGUSERPASS'); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | $this->connector->bind($dn, $password); |
||
| 130 | |||
| 131 | $options['scope'] = Query::SCOPE_BASE; |
||
| 132 | $filter = '(objectClass=*)'; |
||
| 133 | |||
| 134 | /** @psalm-var \Symfony\Component\Ldap\Entry $entry */ |
||
| 135 | $entry = $this->connector->search([$dn], $filter, $options, false); |
||
| 136 | |||
| 137 | $attributes = $this->ldapConfig->getOptionalValue('attributes', []); |
||
| 138 | if ($attributes === null) { |
||
| 139 | $result = $entry->getAttributes(); |
||
| 140 | } else { |
||
| 141 | Assert::isArray($attributes); |
||
| 142 | $result = array_intersect_key( |
||
| 143 | $entry->getAttributes(), |
||
| 144 | array_fill_keys(array_values($attributes), null) |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | $binaries = array_intersect( |
||
| 149 | array_keys($result), |
||
| 150 | $this->ldapConfig->getOptionalArray('attributes.binary', []), |
||
| 151 | ); |
||
| 152 | foreach ($binaries as $binary) { |
||
| 153 | $result[$binary] = array_map('base64_encode', $result[$binary]); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $result; |
||
| 157 | } |
||
| 191 |