Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit e033460

Browse files
committed
Catch bind exceptions on connection failures and log them
- Closes #295 - Binding failures will now be logged instead of being thrown for a graceful fallback to eloquent
1 parent 0cbf57c commit e033460

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/AdldapServiceProvider.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Adldap\Adldap;
66
use Adldap\AdldapInterface;
7+
use Adldap\Auth\BindException;
78
use Adldap\Connections\Provider;
89
use Adldap\Schemas\SchemaInterface;
910
use Adldap\Connections\ConnectionInterface;
1011
use Adldap\Laravel\Exceptions\ConfigurationMissingException;
1112
use Illuminate\Container\Container;
13+
use Illuminate\Support\Facades\Log;
1214
use Illuminate\Support\ServiceProvider;
1315

1416
class AdldapServiceProvider extends ServiceProvider
@@ -93,9 +95,15 @@ protected function addProviders(Adldap $adldap, array $connections = [])
9395
new $settings['schema']
9496
);
9597

96-
// Try connecting to the provider if `auto_connect` is true.
97-
if (isset($settings['auto_connect']) && $settings['auto_connect'] === true) {
98-
$provider->connect();
98+
if ($this->shouldAutoConnect($settings)) {
99+
try {
100+
$provider->connect();
101+
} catch (BindException $e) {
102+
// We'll catch and log bind exceptions so
103+
// any connection issues fail gracefully
104+
// in our application.
105+
Log::error($e);
106+
}
99107
}
100108

101109
// Add the provider to the Adldap container.
@@ -129,6 +137,19 @@ protected function newProvider($configuration = [], ConnectionInterface $connect
129137
return new Provider($configuration, $connection, $schema);
130138
}
131139

140+
/**
141+
* Determine if the given settings is configured for auto-connecting.
142+
*
143+
* @param array $settings
144+
*
145+
* @return bool
146+
*/
147+
protected function shouldAutoConnect(array $settings)
148+
{
149+
return array_key_exists('auto_connect', $settings)
150+
&& $settings['auto_connect'] === true;
151+
}
152+
132153
/**
133154
* Determines if the current application is Lumen.
134155
*

0 commit comments

Comments
 (0)