Skip to content

Commit

Permalink
Implementing connection based events
Browse files Browse the repository at this point in the history
Related: #236. This will allow us to listen for a ConnectionFailed event which will be executed when all hosts have been attempted against, instead of a BindFailure event, which will be fired each time a host is attempted.
  • Loading branch information
stevebauman committed Jan 22, 2021
1 parent 56700e0 commit 29e5a95
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 6 deletions.
59 changes: 53 additions & 6 deletions src/Connection.php
Expand Up @@ -9,6 +9,7 @@
use LdapRecord\Query\Cache;
use LdapRecord\Query\Builder;
use Psr\SimpleCache\CacheInterface;
use LdapRecord\Events\DispatcherInterface;
use LdapRecord\Configuration\DomainConfiguration;

class Connection
Expand Down Expand Up @@ -36,6 +37,13 @@ class Connection
*/
protected $cache;

/**
* The event dispatcher;
*
* @var DispatcherInterface|null
*/
protected $dispatcher;

/**
* The current host connected to.
*
Expand Down Expand Up @@ -104,6 +112,20 @@ public function setLdapConnection(Ldap $ldap)
return $this;
}

/**
* Set the event dispatcher.
*
* @param DispatcherInterface $dispatcher
*
* @return $this
*/
public function setDispatcher(DispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;

return $this;
}

/**
* Initializes the LDAP connection.
*
Expand Down Expand Up @@ -204,10 +226,16 @@ public function connect($username = null, $password = null)
: $this->auth()->bind($username, $password);
};

$this->dispatch(new Events\Connecting($this));

try {
$this->runOperationCallback($attempt);

$this->dispatch(new Events\Connected($this));
} catch (LdapRecordException $e) {
$this->retryOnNextHost($e, $attempt);
$this->retryOnNextHost($e, $attempt, $failed = function () {
$this->dispatch(new Events\ConnectionFailed($this));
});
}

return $this;
Expand Down Expand Up @@ -238,6 +266,20 @@ public function disconnect()
$this->ldap->close();
}

/**
* Dispatch an event.
*
* @param object $event
*
* @return void
*/
public function dispatch($event)
{
if (isset($this->dispatcher)) {
$this->dispatcher->dispatch($event);
}
}

/**
* Get the attempted hosts that failed connecting to.
*
Expand Down Expand Up @@ -407,23 +449,28 @@ protected function retry(Closure $operation)
*
* @param LdapRecordException $e
* @param Closure $operation
* @param Closure|null $failed
*
* @return mixed
*
* @throws LdapRecordException
*/
protected function retryOnNextHost(LdapRecordException $e, Closure $operation)
protected function retryOnNextHost(LdapRecordException $e, Closure $operation, Closure $failed = null)
{
if (($key = array_search($this->host, $this->hosts)) !== false) {
unset($this->hosts[$key]);
}

if (! $next = reset($this->hosts)) {
throw $e;
if ($next = reset($this->hosts)) {
$this->host = $next;

return $this->tryAgainIfCausedByLostConnection($e, $operation);
}

$this->host = $next;
if ($failed) {
$failed($this->ldap);
}

return $this->tryAgainIfCausedByLostConnection($e, $operation);
throw $e;
}
}
4 changes: 4 additions & 0 deletions src/Container.php
Expand Up @@ -270,6 +270,10 @@ public function add(Connection $connection, $name = null)
{
$this->connections[$name ?? $this->default] = $connection;

if ($this->dispatcher) {
$connection->setDispatcher($this->dispatcher);
}

return $this;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Events/Connected.php
@@ -0,0 +1,8 @@
<?php

namespace LdapRecord\Events;

class Connected extends ConnectionEvent
{
//
}
8 changes: 8 additions & 0 deletions src/Events/Connecting.php
@@ -0,0 +1,8 @@
<?php

namespace LdapRecord\Events;

class Connecting extends ConnectionEvent
{
//
}
25 changes: 25 additions & 0 deletions src/Events/ConnectionEvent.php
@@ -0,0 +1,25 @@
<?php

namespace LdapRecord\Events;

use LdapRecord\Connection;

abstract class ConnectionEvent
{
/**
* The LDAP connection.
*
* @var Connection
*/
public $connection;

/**
* Constructor.
*
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
}
8 changes: 8 additions & 0 deletions src/Events/ConnectionFailed.php
@@ -0,0 +1,8 @@
<?php

namespace LdapRecord\Events;

class ConnectionFailed extends ConnectionEvent
{
//
}

0 comments on commit 29e5a95

Please sign in to comment.