| Conditions | 6 |
| Paths | 1 |
| Total Lines | 32 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 42 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 17 | public function dispatch(callable $controller, array $vars, Route $route = null) |
||
| 18 | { |
||
| 19 | $middleware = function ( |
||
| 20 | ServerRequestInterface $request, ResponseInterface $response, callable $next |
||
| 21 | ) use ( |
||
| 22 | $controller, $vars |
||
| 23 | ) { |
||
| 24 | try { |
||
| 25 | $result = call_user_func_array($controller, [$request, $response, $vars]); |
||
| 26 | |||
| 27 | if (is_array($result) || $result instanceof ArrayObject) { |
||
| 28 | $body = json_encode($result); |
||
| 29 | $response = $this->getResponse(); |
||
| 30 | |||
| 31 | if ($response->getBody()->isWritable()) { |
||
| 32 | $response->getBody()->write($body); |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | if ($response instanceof ResponseInterface) { |
||
| 37 | $response = $response->withAddedHeader('content-type', 'application/json'); |
||
| 38 | return $next($request, $response); |
||
| 39 | } |
||
| 40 | } catch (HttpException $e) { |
||
| 41 | return $e->buildJsonResponse($this->getResponse()); |
||
| 42 | } |
||
| 43 | |||
| 44 | throw new RuntimeException('Unable to build a json response from controller return value.'); |
||
| 45 | }; |
||
| 46 | |||
| 47 | return $route->getMiddlewareRunner()->run($middleware, $this->getRequest(), $this->getResponse()); |
||
|
|
|||
| 48 | } |
||
| 49 | } |
||
| 50 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: