Skip to content

Commit

Permalink
Refactor configurator (#23)
Browse files Browse the repository at this point in the history
* Refactored configurator middleware handling

The `register` method was introduced to streamline middleware registration,
improving extendability and encapsulation while allowing middlewares
like retry and redirect to be added as closures which are lazily invoked
during connector configuration. This change leads to cleaner code and a
more scalable configuration pattern.

* Refactor type hinting and optimize closure
  • Loading branch information
jenky committed Dec 31, 2023
1 parent 720ccc8 commit 992f1e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
67 changes: 37 additions & 30 deletions src/ConnectorConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,48 @@
use Fansipan\Retry\Delay;
use Fansipan\Retry\GenericRetryStrategy;

/**
* @template T of ConnectorInterface
*/
class ConnectorConfigurator
{
/**
* @var array<array-key, callable(\Psr\Http\Message\RequestInterface, callable): \Psr\Http\Message\ResponseInterface>
* @var \Closure[]
*/
protected $middleware = [];
private $handlers = [];

/**
* Configure the given connector with options for current request.
*
* @template T of ConnectorInterface
* @param T $connector
* @return T
*/
final public function configure(ConnectorInterface $connector): ConnectorInterface
{
if (empty($this->middleware)) {
return $connector;
}

$clone = clone $connector;

foreach ($this->middleware as $name => $middleware) {
$clone->middleware()->unshift($middleware, \is_string($name) ? $name : '');
foreach ($this->handlers as $handler) {
$handler($clone);
}

return $clone;
}

/**
* Register a configuration handler.
*
* @param \Closure(T): void $handler
* @return static
*/
protected function register(\Closure $handler)
{
$clone = clone $this;

$clone->handlers[] = $handler;

return $clone;
}

/**
* Indicate that a failed request should be retried.
*
Expand All @@ -50,17 +63,13 @@ public function retry(
?RetryStrategyInterface $retryStrategy = null,
bool $throw = true
) {
$strategy = $retryStrategy ?? new GenericRetryStrategy(new Delay(1000, 2.0));

$clone = clone $this;

$clone->middleware['retry_requests'] = new RetryRequests(
$strategy,
$maxRetries,
$throw
);

return $clone;
return $this->register(static function (ConnectorInterface $connector) use ($retryStrategy, $maxRetries, $throw) {
$strategy = $retryStrategy ?? new GenericRetryStrategy(new Delay(1000, 2.0));
$middleware = new RetryRequests($strategy, $maxRetries, $throw);
$connector->middleware()->unshift(
$middleware, 'retry_requests'
);
});
}

/**
Expand All @@ -75,15 +84,13 @@ public function followRedirects(
bool $strict = false,
bool $referer = false
) {
$clone = clone $this;

$clone->middleware['follow_redirects'] = new FollowRedirects(
$max,
$protocols,
$strict,
$referer
);

return $clone;
return $this->register(static function (ConnectorInterface $connector) use ($max, $protocols, $strict, $referer) {
$connector->middleware()->unshift(new FollowRedirects(
$max,
$protocols,
$strict,
$referer
), 'follow_redirects');
});
}
}
2 changes: 1 addition & 1 deletion src/Traits/HasClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait HasClient
/**
* The HTTP client instance.
*
* @var \Psr\Http\Client\ClientInterface
* @var ClientInterface
*/
private $client;

Expand Down
4 changes: 2 additions & 2 deletions src/Traits/HasMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait HasMiddleware
/**
* The middleware instance.
*
* @var \Fansipan\Middleware
* @var Middleware
*/
private $middleware;

Expand Down Expand Up @@ -40,7 +40,7 @@ protected function defaultMiddleware(): array
*/
private function gatherMiddleware(): array
{
return array_filter(array_map(function (array $item) {
return array_filter(array_map(static function (array $item) {
return $item[0] ?? null;
}, $this->middleware()->all()));
}
Expand Down

0 comments on commit 992f1e7

Please sign in to comment.