Skip to content

Commit

Permalink
Merge 5a37584 into c3797b7
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Oct 3, 2018
2 parents c3797b7 + 5a37584 commit ee43ad4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -19,7 +19,7 @@
"require": {
"php": ">= 7.1",
"apitte/core": "^0.4.0",
"contributte/middlewares": "^0.6.0|^0.7.0"
"contributte/middlewares": "^0.6.0|^0.7.0|^0.8.0"
},
"require-dev": {
"ninjify/qa": "^0.8.0",
Expand Down
25 changes: 3 additions & 22 deletions src/ApiMiddleware.php
Expand Up @@ -3,12 +3,11 @@
namespace Apitte\Middlewares;

use Apitte\Core\Dispatcher\IDispatcher;
use Apitte\Core\Exception\Logical\InvalidStateException;
use Contributte\Middlewares\IMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;

class ApiMiddleware
class ApiMiddleware implements IMiddleware
{

/** @var IDispatcher */
Expand All @@ -22,30 +21,12 @@ public function __construct(IDispatcher $dispatcher)
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
{
// Pass this API request/response objects to API dispatcher
$response = $this->dispatch($request, $response);
$response = $this->dispatcher->dispatch($request, $response);

// Pass response to next middleware
$response = $next($request, $response);

return $response;
}

protected function dispatch(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
try {
// Pass to dispatcher, find handler, process some logic and return response.
$response = $this->dispatcher->dispatch($request, $response);

// Validate returned api response
if (!($response instanceof ResponseInterface)) {
throw new InvalidStateException(sprintf('Returned response must be type of %s', ResponseInterface::class));
}

return $response;
} catch (Throwable $e) {
// Just throw this out
throw $e;
}
}

}

0 comments on commit ee43ad4

Please sign in to comment.