|
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 |
@@ -102,16 +108,15 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym |
102 | 108 | $standardService = new StandardServer($serverConfig); |
103 | 109 | $result = $standardService->executePsrRequest($request); |
104 | 110 |
|
105 | | - $httpCodeDecider = new HttpCodeDecider(); |
106 | 111 | if ($result instanceof ExecutionResult) { |
107 | | - return new JsonResponse($result->toArray($this->debug), $httpCodeDecider->decideHttpStatusCode($result)); |
| 112 | + return new JsonResponse($result->toArray($this->debug), $this->httpCodeDecider->decideHttpStatusCode($result)); |
108 | 113 | } |
109 | 114 | if (is_array($result)) { |
110 | 115 | $finalResult = array_map(function (ExecutionResult $executionResult): array { |
111 | 116 | return $executionResult->toArray($this->debug); |
112 | 117 | }, $result); |
113 | 118 | // Let's return the highest result. |
114 | | - $statuses = array_map([$httpCodeDecider, 'decideHttpStatusCode'], $result); |
| 119 | + $statuses = array_map([$this->httpCodeDecider, 'decideHttpStatusCode'], $result); |
115 | 120 | $status = empty($statuses) ? 500 : max($statuses); |
116 | 121 |
|
117 | 122 | return new JsonResponse($finalResult, $status); |
|
0 commit comments