Skip to content

Commit

Permalink
Split MiddlewareHandler's create() method into prepend() and append()
Browse files Browse the repository at this point in the history
  • Loading branch information
rybakit committed Mar 17, 2022
1 parent b0cf56a commit bbf452e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ $pureMsgpackUnpacker = new BufferUnpacker();
$packer = new PurePacker($pureMsgpackPacker, $pureMsgpackUnpacker);

$handler = new DefaultHandler($connection, $packer);
$handler = MiddlewareHandler::create($handler, [
$handler = MiddlewareHandler::append($handler, [
RetryMiddleware::exponential(3),
new AuthenticationMiddleware('<username>', '<password>'),
// ...
Expand Down
8 changes: 4 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static function fromOptions(array $options, ?Packer $packer = null) : sel
$handler = new DefaultHandler($connection, $packer ?? PackerFactory::create());

return $middleware
? new self(MiddlewareHandler::create($handler, $middleware))
? new self(MiddlewareHandler::append($handler, $middleware))
: new self($handler);
}

Expand Down Expand Up @@ -120,22 +120,22 @@ public static function fromDsn(string $dsn, ?Packer $packer = null) : self
$handler = new DefaultHandler($connection, $packer ?? PackerFactory::create());

return $middleware
? new self(MiddlewareHandler::create($handler, $middleware))
? new self(MiddlewareHandler::append($handler, $middleware))
: new self($handler);
}

public function withMiddleware(Middleware ...$middleware) : self
{
$new = clone $this;
$new->handler = MiddlewareHandler::create($new->handler, $middleware);
$new->handler = MiddlewareHandler::append($new->handler, $middleware);

return $new;
}

public function withPrependedMiddleware(Middleware ...$middleware) : self
{
$new = clone $this;
$new->handler = MiddlewareHandler::create($new->handler, $middleware, true);
$new->handler = MiddlewareHandler::prepend($new->handler, $middleware);

return $new;
}
Expand Down
21 changes: 17 additions & 4 deletions src/Handler/MiddlewareHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,29 @@ private function __construct($handler, $middleware)
/**
* @param Middleware[] $middleware
*/
public static function create(Handler $handler, array $middleware, bool $prepend = false) : Handler
public static function append(Handler $handler, array $middleware) : Handler
{
if (!$handler instanceof self) {
return new self($handler, $middleware);
}

$handler = clone $handler;
$handler->middleware = $prepend
? \array_merge($middleware, $handler->middleware)
: \array_merge($handler->middleware, $middleware);
$handler->middleware = \array_merge($handler->middleware, $middleware);

return $handler;
}

/**
* @param Middleware[] $middleware
*/
public static function prepend(Handler $handler, array $middleware) : Handler
{
if (!$handler instanceof self) {
return new self($handler, $middleware);
}

$handler = clone $handler;
$handler->middleware = \array_merge($middleware, $handler->middleware);

return $handler;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function build() : Client
$middlewares[] = new AuthenticationMiddleware($this->options['username'], $this->options['password'] ?? '');
}
if ($middlewares) {
$handler = MiddlewareHandler::create($handler, $middlewares);
$handler = MiddlewareHandler::append($handler, $middlewares);
}

return new Client($handler);
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/Handler/MiddlewareHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,39 @@ protected function setUp() : void
$this->handler = $this->createDummyClient()->getHandler();
}

public function testCreateMiddlewareExecutesInFifoOrder() : void
public function testAppendedMiddlewareAreExecutedInFifoOrder() : void
{
$trace = new \ArrayObject();
$middleware1 = SpyMiddleware::fromTraceId(1, $trace);
$middleware2 = SpyMiddleware::fromTraceId(2, $trace);

$handler = MiddlewareHandler::create($this->handler, [$middleware1, $middleware2]);
$handler = MiddlewareHandler::append($this->handler, [$middleware1, $middleware2]);
$handler->handle($this->request);

self::assertSame([1, 2], $trace->getArrayCopy());
}

public function testCreateAppendsMiddleware() : void
public function testAppendAppendsMiddleware() : void
{
$trace = new \ArrayObject();
$middleware1 = SpyMiddleware::fromTraceId(1, $trace);
$middleware2 = SpyMiddleware::fromTraceId(2, $trace);

$handler = MiddlewareHandler::create($this->handler, [$middleware1]);
$handler = MiddlewareHandler::create($handler, [$middleware2]);
$handler = MiddlewareHandler::append($this->handler, [$middleware1]);
$handler = MiddlewareHandler::append($handler, [$middleware2]);
$handler->handle($this->request);

self::assertSame([1, 2], $trace->getArrayCopy());
}

public function testCreatePrependsMiddleware() : void
public function testPrependPrependsMiddleware() : void
{
$trace = new \ArrayObject();
$middleware1 = SpyMiddleware::fromTraceId(1, $trace);
$middleware2 = SpyMiddleware::fromTraceId(2, $trace);

$handler = MiddlewareHandler::create($this->handler, [$middleware1]);
$handler = MiddlewareHandler::create($handler, [$middleware2], true);
$handler = MiddlewareHandler::append($this->handler, [$middleware1]);
$handler = MiddlewareHandler::prepend($handler, [$middleware2]);
$handler->handle($this->request);

self::assertSame([2, 1], $trace->getArrayCopy());
Expand All @@ -82,7 +82,7 @@ public function testMiddlewareRemainsAfterExecution() : void
{
$middleware = SpyMiddleware::fromTraceId(1);

$handler = MiddlewareHandler::create($this->handler, [$middleware]);
$handler = MiddlewareHandler::append($this->handler, [$middleware]);
$handler->handle($this->request);
$handler->handle($this->request);

Expand Down

0 comments on commit bbf452e

Please sign in to comment.