|
10 | 10 | use Laminas\Diactoros\UploadedFileFactory; |
11 | 11 | use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
12 | 12 | use TheCodingMachine\GraphQLite\Http\HttpCodeDecider; |
| 13 | +use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface; |
13 | 14 | use function array_map; |
14 | 15 | use GraphQL\Executor\ExecutionResult; |
15 | 16 | use GraphQL\Server\ServerConfig; |
@@ -42,12 +43,17 @@ class GraphQLiteController |
42 | 43 | * @var ServerConfig |
43 | 44 | */ |
44 | 45 | private $serverConfig; |
| 46 | + /** |
| 47 | + * @var HttpCodeDeciderInterface |
| 48 | + */ |
| 49 | + private $httpCodeDecider; |
45 | 50 |
|
46 | | - public function __construct(ServerConfig $serverConfig, HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = null) |
| 51 | + public function __construct(ServerConfig $serverConfig, HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = null, ?HttpCodeDeciderInterface $httpCodeDecider = null) |
47 | 52 | { |
48 | 53 | $this->serverConfig = $serverConfig; |
49 | 54 | $this->httpMessageFactory = $httpMessageFactory ?: new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory()); |
50 | 55 | $this->debug = $debug ?? $serverConfig->getDebugFlag(); |
| 56 | + $this->httpCodeDecider = $httpCodeDecider ?? new HttpCodeDecider(); |
51 | 57 | } |
52 | 58 |
|
53 | 59 | public function loadRoutes(): RouteCollection |
@@ -99,16 +105,15 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym |
99 | 105 | $standardService = new StandardServer($serverConfig); |
100 | 106 | $result = $standardService->executePsrRequest($request); |
101 | 107 |
|
102 | | - $httpCodeDecider = new HttpCodeDecider(); |
103 | 108 | if ($result instanceof ExecutionResult) { |
104 | | - return new JsonResponse($result->toArray($this->debug), $httpCodeDecider->decideHttpStatusCode($result)); |
| 109 | + return new JsonResponse($result->toArray($this->debug), $this->httpCodeDecider->decideHttpStatusCode($result)); |
105 | 110 | } |
106 | 111 | if (is_array($result)) { |
107 | 112 | $finalResult = array_map(function (ExecutionResult $executionResult): array { |
108 | 113 | return $executionResult->toArray($this->debug); |
109 | 114 | }, $result); |
110 | 115 | // Let's return the highest result. |
111 | | - $statuses = array_map([$httpCodeDecider, 'decideHttpStatusCode'], $result); |
| 116 | + $statuses = array_map([$this->httpCodeDecider, 'decideHttpStatusCode'], $result); |
112 | 117 | $status = empty($statuses) ? 500 : max($statuses); |
113 | 118 |
|
114 | 119 | return new JsonResponse($finalResult, $status); |
|
0 commit comments