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