Skip to content

Commit

Permalink
Handlers type checks added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmard authored and Ahmard committed Feb 6, 2021
1 parent be569d2 commit f89b13c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Http/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Reaponse\Http;


use InvalidArgumentException;
use Reaponse\ObjectStorage;
use SplQueue;

Expand All @@ -27,6 +28,12 @@ public function __construct(array $handlers, ResponseInterface $response)
$this->response = $response;
$this->handlers = new SplQueue();
foreach ($handlers as $handler) {
//make sure that all handlers implement HandlerInterface
if (!$handler instanceof HandlerInterface) {
$strHandler = get_class($handler);
throw new InvalidArgumentException("Handler {$strHandler} must implement Reaponse\Http\HandlerInterface");
}

$this->handlers->push($handler);
}

Expand Down
14 changes: 13 additions & 1 deletion src/Http/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Reaponse\Http;


use InvalidArgumentException;
use Nette\Utils\JsonException;
use Psr\Http\Message\ServerRequestInterface;
use React\Promise\Deferred;
Expand All @@ -20,6 +21,10 @@ class Middleware
*/
public function __construct(...$handler)
{
if (empty($handler)) {
throw new InvalidArgumentException('Server handlers must be provided.');
}

$this->handlers = $handler;
}

Expand All @@ -34,7 +39,14 @@ public function __invoke(ServerRequestInterface $request): PromiseInterface
$promise = $deferred->promise();
$response = new Response($deferred, $request, $this->handlers);

$this->handlers[0]->handle($response);
$handler = $this->handlers[0];

if (!$handler instanceof HandlerInterface) {
$handlerStr = get_class($handler);
throw new InvalidArgumentException("Handler {$handlerStr} must implement Reaponse\Http\HandlerInterface");
}

$handler->handle($response);
return $promise;
}
}

0 comments on commit f89b13c

Please sign in to comment.