Skip to content

Commit 213bbc8

Browse files
committed
Moved error detection into trait
1 parent 2470b90 commit 213bbc8

File tree

4 files changed

+67
-57
lines changed

4 files changed

+67
-57
lines changed

src/Connection.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
class Connection
1414
{
15+
use DetectsErrors;
16+
1517
/**
1618
* The underlying LDAP connection.
1719
*
@@ -277,24 +279,12 @@ public function isConnected()
277279
*/
278280
protected function tryAgainIfCausedByLostConnection(LdapRecordException $e, Closure $callback)
279281
{
280-
if ($this->causedByLostConnection($e)) {
282+
if ($this->causedByLostConnection($e->getMessage())) {
281283
$this->reconnect();
282284

283285
return $this->runOperationCallback($callback);
284286
}
285287

286288
throw $e;
287289
}
288-
289-
/**
290-
* Determine if the given exception was caused by a lost connection.
291-
*
292-
* @param LdapRecordException $e
293-
*
294-
* @return bool
295-
*/
296-
protected function causedByLostConnection(LdapRecordException $e)
297-
{
298-
return strpos($e->getMessage(), 'contact LDAP server') !== false;
299-
}
300290
}

src/DetectsErrors.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace LdapRecord;
4+
5+
trait DetectsErrors
6+
{
7+
/**
8+
* Determine if the error was caused by a lost connection.
9+
*
10+
* @param string $error
11+
*
12+
* @return bool
13+
*/
14+
protected function causedByLostConnection($error)
15+
{
16+
return $this->errorContainsMessage($error, ["Can't contact LDAP server", 'Operations error']);
17+
}
18+
19+
/**
20+
* Determine if the error was caused by lack of pagination support.
21+
*
22+
* @param string $error
23+
*
24+
* @return bool
25+
*/
26+
protected function causedByPaginationSupport($error)
27+
{
28+
return $this->errorContainsMessage($error, 'No server controls in result');
29+
}
30+
31+
/**
32+
* Determine if the error was caused by a size limit warning.
33+
*
34+
* @param $error
35+
*
36+
* @return bool
37+
*/
38+
protected function causedBySizeLimit($error)
39+
{
40+
return $this->errorContainsMessage($error, ['Partial search results returned', 'Size limit exceeded']);
41+
}
42+
43+
/**
44+
* Determine if the error contains the any of the messages.
45+
*
46+
* @param string $error
47+
* @param string|array $messages
48+
*
49+
* @return bool
50+
*/
51+
protected function errorContainsMessage($error, $messages = [])
52+
{
53+
foreach ((array) $messages as $message) {
54+
if (strpos($error, $message) !== false) {
55+
return true;
56+
}
57+
}
58+
59+
return false;
60+
}
61+
}

src/Ldap.php

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class Ldap
88
{
9+
use DetectsErrors;
10+
911
/**
1012
* The SSL LDAP protocol string.
1113
*
@@ -857,49 +859,6 @@ protected function shouldBypassError($error)
857859
return $this->causedByPaginationSupport($error) || $this->causedBySizeLimit($error);
858860
}
859861

860-
/**
861-
* Determine if the error was caused by lack of pagination support.
862-
*
863-
* @param string $error
864-
*
865-
* @return bool
866-
*/
867-
protected function causedByPaginationSupport($error)
868-
{
869-
return $this->errorContainsMessage($error, 'No server controls in result');
870-
}
871-
872-
/**
873-
* Determine if the error was caused by a size limit warning.
874-
*
875-
* @param $error
876-
*
877-
* @return bool
878-
*/
879-
protected function causedBySizeLimit($error)
880-
{
881-
return $this->errorContainsMessage($error, ['Partial search results returned', 'Size limit exceeded']);
882-
}
883-
884-
/**
885-
* Determine if the given error contains the any of the messages.
886-
*
887-
* @param string $error
888-
* @param string|array $messages
889-
*
890-
* @return bool
891-
*/
892-
protected function errorContainsMessage($error, $messages = [])
893-
{
894-
foreach ((array) $messages as $message) {
895-
if (strpos($error, $message) !== false) {
896-
return true;
897-
}
898-
}
899-
900-
return false;
901-
}
902-
903862
/**
904863
* Generates an LDAP connection string for each host given.
905864
*

tests/ConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function test_ran_ldap_operations_are_retried_when_connection_is_lost()
215215
$called++;
216216

217217
if ($called === 1) {
218-
throw new \Exception('Cannot contact LDAP server');
218+
throw new \Exception("Can't contact LDAP server");
219219
}
220220

221221
return $called === 2;

0 commit comments

Comments
 (0)