Total Complexity | 53 |
Total Lines | 357 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like LDAP often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LDAP, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class LDAP implements ILDAPWrapper { |
||
36 | protected $curFunc = ''; |
||
37 | protected $curArgs = array(); |
||
38 | |||
39 | /** |
||
40 | * @param resource $link |
||
41 | * @param string $dn |
||
42 | * @param string $password |
||
43 | * @return bool|mixed |
||
44 | */ |
||
45 | public function bind($link, $dn, $password) { |
||
46 | return $this->invokeLDAPMethod('bind', $link, $dn, $password); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param string $host |
||
51 | * @param string $port |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function connect($host, $port) { |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param resource $link |
||
67 | * @param resource $result |
||
68 | * @param string $cookie |
||
69 | * @return bool|LDAP |
||
70 | */ |
||
71 | public function controlPagedResultResponse($link, $result, &$cookie) { |
||
72 | $this->preFunctionCall('ldap_control_paged_result_response', |
||
73 | array($link, $result, $cookie)); |
||
74 | $result = ldap_control_paged_result_response($link, $result, $cookie); |
||
75 | $this->postFunctionCall(); |
||
76 | |||
77 | return $result; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param LDAP $link |
||
82 | * @param int $pageSize |
||
83 | * @param bool $isCritical |
||
84 | * @param string $cookie |
||
85 | * @return mixed|true |
||
86 | */ |
||
87 | public function controlPagedResult($link, $pageSize, $isCritical, $cookie) { |
||
88 | return $this->invokeLDAPMethod('control_paged_result', $link, $pageSize, |
||
89 | $isCritical, $cookie); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param LDAP $link |
||
94 | * @param LDAP $result |
||
95 | * @return mixed |
||
96 | */ |
||
97 | public function countEntries($link, $result) { |
||
98 | return $this->invokeLDAPMethod('count_entries', $link, $result); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param LDAP $link |
||
103 | * @return integer |
||
104 | */ |
||
105 | public function errno($link) { |
||
106 | return $this->invokeLDAPMethod('errno', $link); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param LDAP $link |
||
111 | * @return string |
||
112 | */ |
||
113 | public function error($link) { |
||
114 | return $this->invokeLDAPMethod('error', $link); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Splits DN into its component parts |
||
119 | * @param string $dn |
||
120 | * @param int @withAttrib |
||
|
|||
121 | * @return array|false |
||
122 | * @link http://www.php.net/manual/en/function.ldap-explode-dn.php |
||
123 | */ |
||
124 | public function explodeDN($dn, $withAttrib) { |
||
125 | return $this->invokeLDAPMethod('explode_dn', $dn, $withAttrib); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param LDAP $link |
||
130 | * @param LDAP $result |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public function firstEntry($link, $result) { |
||
134 | return $this->invokeLDAPMethod('first_entry', $link, $result); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param LDAP $link |
||
139 | * @param LDAP $result |
||
140 | * @return array|mixed |
||
141 | */ |
||
142 | public function getAttributes($link, $result) { |
||
143 | return $this->invokeLDAPMethod('get_attributes', $link, $result); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param LDAP $link |
||
148 | * @param LDAP $result |
||
149 | * @return mixed|string |
||
150 | */ |
||
151 | public function getDN($link, $result) { |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param LDAP $link |
||
157 | * @param LDAP $result |
||
158 | * @return array|mixed |
||
159 | */ |
||
160 | public function getEntries($link, $result) { |
||
161 | return $this->invokeLDAPMethod('get_entries', $link, $result); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param LDAP $link |
||
166 | * @param resource $result |
||
167 | * @return mixed |
||
168 | */ |
||
169 | public function nextEntry($link, $result) { |
||
170 | return $this->invokeLDAPMethod('next_entry', $link, $result); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param LDAP $link |
||
175 | * @param string $baseDN |
||
176 | * @param string $filter |
||
177 | * @param array $attr |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function read($link, $baseDN, $filter, $attr) { |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param LDAP $link |
||
186 | * @param string $baseDN |
||
187 | * @param string $filter |
||
188 | * @param array $attr |
||
189 | * @param int $attrsOnly |
||
190 | * @param int $limit |
||
191 | * @return mixed |
||
192 | * @throws \Exception |
||
193 | */ |
||
194 | public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) { |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param LDAP $link |
||
214 | * @param string $userDN |
||
215 | * @param string $password |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function modReplace($link, $userDN, $password) { |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param LDAP $link |
||
224 | * @param string $userDN |
||
225 | * @param string $oldPassword |
||
226 | * @param string $password |
||
227 | * @return bool |
||
228 | */ |
||
229 | public function exopPasswd($link, $userDN, $oldPassword, $password) { |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param LDAP $link |
||
235 | * @param string $option |
||
236 | * @param int $value |
||
237 | * @return bool|mixed |
||
238 | */ |
||
239 | public function setOption($link, $option, $value) { |
||
240 | return $this->invokeLDAPMethod('set_option', $link, $option, $value); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @param LDAP $link |
||
245 | * @return mixed|true |
||
246 | */ |
||
247 | public function startTls($link) { |
||
248 | return $this->invokeLDAPMethod('start_tls', $link); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param resource $link |
||
253 | * @return bool|mixed |
||
254 | */ |
||
255 | public function unbind($link) { |
||
256 | return $this->invokeLDAPMethod('unbind', $link); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Checks whether the server supports LDAP |
||
261 | * @return boolean if it the case, false otherwise |
||
262 | * */ |
||
263 | public function areLDAPFunctionsAvailable() { |
||
264 | return function_exists('ldap_connect'); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Checks whether the submitted parameter is a resource |
||
269 | * @param Resource $resource the resource variable to check |
||
270 | * @return bool true if it is a resource, false otherwise |
||
271 | */ |
||
272 | public function isResource($resource) { |
||
273 | return is_resource($resource); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Checks whether the return value from LDAP is wrong or not. |
||
278 | * |
||
279 | * When using ldap_search we provide an array, in case multiple bases are |
||
280 | * configured. Thus, we need to check the array elements. |
||
281 | * |
||
282 | * @param $result |
||
283 | * @return bool |
||
284 | */ |
||
285 | protected function isResultFalse($result) { |
||
286 | if($result === false) { |
||
287 | return true; |
||
288 | } |
||
289 | |||
290 | if($this->curFunc === 'ldap_search' && is_array($result)) { |
||
291 | foreach ($result as $singleResult) { |
||
292 | if($singleResult === false) { |
||
293 | return true; |
||
294 | } |
||
295 | } |
||
296 | } |
||
297 | |||
298 | return false; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @return mixed |
||
303 | */ |
||
304 | protected function invokeLDAPMethod() { |
||
305 | $arguments = func_get_args(); |
||
306 | $func = 'ldap_' . array_shift($arguments); |
||
307 | if(function_exists($func)) { |
||
308 | $this->preFunctionCall($func, $arguments); |
||
309 | $result = call_user_func_array($func, $arguments); |
||
310 | if ($this->isResultFalse($result)) { |
||
311 | $this->postFunctionCall(); |
||
312 | } |
||
313 | return $result; |
||
314 | } |
||
315 | return null; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param string $functionName |
||
320 | * @param array $args |
||
321 | */ |
||
322 | private function preFunctionCall($functionName, $args) { |
||
323 | $this->curFunc = $functionName; |
||
324 | $this->curArgs = $args; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Analyzes the returned LDAP error and acts accordingly if not 0 |
||
329 | * |
||
330 | * @param resource $resource the LDAP Connection resource |
||
331 | * @throws ConstraintViolationException |
||
332 | * @throws ServerNotAvailableException |
||
333 | * @throws \Exception |
||
334 | */ |
||
335 | private function processLDAPError($resource) { |
||
336 | $errorCode = ldap_errno($resource); |
||
337 | if($errorCode === 0) { |
||
338 | return; |
||
339 | } |
||
340 | $errorMsg = ldap_error($resource); |
||
341 | |||
342 | if($this->curFunc === 'ldap_get_entries' |
||
343 | && $errorCode === -4) { |
||
344 | } else if ($errorCode === 32) { |
||
345 | //for now |
||
346 | } else if ($errorCode === 10) { |
||
347 | //referrals, we switch them off, but then there is AD :) |
||
348 | } else if ($errorCode === -1) { |
||
349 | throw new ServerNotAvailableException('Lost connection to LDAP server.'); |
||
350 | } else if ($errorCode === 52) { |
||
351 | throw new ServerNotAvailableException('LDAP server is shutting down.'); |
||
352 | } else if ($errorCode === 48) { |
||
353 | throw new \Exception('LDAP authentication method rejected', $errorCode); |
||
354 | } else if ($errorCode === 1) { |
||
355 | throw new \Exception('LDAP Operations error', $errorCode); |
||
356 | } else if ($errorCode === 19) { |
||
357 | ldap_get_option($this->curArgs[0], LDAP_OPT_ERROR_STRING, $extended_error); |
||
358 | throw new ConstraintViolationException(!empty($extended_error)?$extended_error:$errorMsg, $errorCode); |
||
359 | } else { |
||
360 | \OC::$server->getLogger()->debug('LDAP error {message} ({code}) after calling {func}', [ |
||
361 | 'app' => 'user_ldap', |
||
362 | 'message' => $errorMsg, |
||
363 | 'code' => $errorCode, |
||
364 | 'func' => $this->curFunc, |
||
365 | ]); |
||
366 | } |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Called after an ldap method is run to act on LDAP error if necessary |
||
371 | * @throw \Exception |
||
372 | */ |
||
373 | private function postFunctionCall() { |
||
392 | } |
||
393 | } |
||
394 |