Conditions | 5 |
Paths | 2 |
Total Lines | 40 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 30 |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
18 | public function getExecutionChain(Route $route, array $vars) |
||
19 | { |
||
20 | $middleware = function ( |
||
21 | ServerRequestInterface $request, ResponseInterface $response, callable $next |
||
22 | ) use ( |
||
23 | $route, $vars |
||
24 | ) { |
||
25 | try { |
||
26 | $response = call_user_func_array($route->getCallable(), [$request, $response, $vars]); |
||
27 | |||
28 | if (! $response instanceof ResponseInterface) { |
||
29 | throw new RuntimeException( |
||
30 | 'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)' |
||
31 | ); |
||
32 | } |
||
33 | |||
34 | $response = $next($request, $response); |
||
35 | } catch (HttpException $e) { |
||
36 | $response = $e->buildJsonResponse($response); |
||
37 | } catch (Exception $e) { |
||
38 | $body = [ |
||
39 | 'code' => 500, |
||
40 | 'message' => $e->getMessage() |
||
41 | ]; |
||
42 | |||
43 | $response->getBody()->write(json_encode($body)); |
||
44 | $response = $response->withStatus(500); |
||
45 | } |
||
46 | |||
47 | return $response->withAddedHeader('content-type', 'application/json'); |
||
48 | }; |
||
49 | |||
50 | $execChain = (new ExecutionChain)->middleware($middleware); |
||
51 | |||
52 | foreach ($route->getMiddlewareStack() as $middleware) { |
||
53 | $execChain->middleware($middleware); |
||
54 | } |
||
55 | |||
56 | return $execChain; |
||
57 | } |
||
58 | } |
||
59 |