Skip to content

Commit 5a591ba

Browse files
committed
Switching "decider" key to "http_code_decider"
Refactoring code to remove setter.
1 parent 3969452 commit 5a591ba

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

config/graphqlite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
'middleware' => ['web'],
2525

2626
// Sets the status code in the HTTP request where operations have errors.
27-
'decider' => HttpCodeDecider::class,
27+
'http_code_decider' => HttpCodeDecider::class,
2828
];

src/Controllers/GraphQLiteController.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class GraphQLiteController
3434
/** @var bool|int */
3535
private $debug;
3636
/** @var HttpCodeDeciderInterface */
37-
private $codeDecider;
37+
private $httpCodeDecider;
3838

39-
public function __construct(StandardServer $standardServer, HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = DebugFlag::RETHROW_UNSAFE_EXCEPTIONS)
39+
public function __construct(StandardServer $standardServer, HttpCodeDeciderInterface $httpCodeDecider, HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = DebugFlag::RETHROW_UNSAFE_EXCEPTIONS)
4040
{
4141
$this->standardServer = $standardServer;
42+
$this->httpCodeDecider = $httpCodeDecider;
4243
$this->httpMessageFactory = $httpMessageFactory ?: new DiactorosFactory();
4344
$this->debug = $debug === null ? false : $debug;
44-
$this->codeDecider = new HttpCodeDecider();
4545
}
4646

4747
/**
@@ -77,7 +77,7 @@ private function handlePsr7Request(ServerRequestInterface $request): JsonRespons
7777
{
7878
$result = $this->standardServer->executePsrRequest($request);
7979

80-
$httpCodeDecider = $this->codeDecider;
80+
$httpCodeDecider = $this->httpCodeDecider;
8181
if ($result instanceof ExecutionResult) {
8282
return new JsonResponse($result->toArray($this->debug), $httpCodeDecider->decideHttpStatusCode($result));
8383
}
@@ -95,9 +95,4 @@ private function handlePsr7Request(ServerRequestInterface $request): JsonRespons
9595
}
9696
throw new RuntimeException('Unexpected response from StandardServer::executePsrRequest'); // @codeCoverageIgnore
9797
}
98-
99-
public function setCodeDecider(HttpCodeDeciderInterface $decider): void
100-
{
101-
$this->codeDecider = $decider;
102-
}
10398
}

src/Providers/GraphQLiteServiceProvider.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use TheCodingMachine\GraphQLite\Context\Context;
2121
use TheCodingMachine\GraphQLite\Exceptions\WebonyxErrorHandler;
2222
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
23+
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
2324
use TheCodingMachine\GraphQLite\Laravel\Listeners\CachePurger;
2425
use TheCodingMachine\GraphQLite\Laravel\Mappers\Parameters\ValidateFieldMiddleware;
2526
use TheCodingMachine\GraphQLite\Laravel\Mappers\PaginatorTypeMapper;
@@ -84,19 +85,22 @@ public function register()
8485
if (!$this->app->has(ResponseFactoryInterface::class)) {
8586
$this->app->bind(ResponseFactoryInterface::class, ResponseFactory::class);
8687
}
88+
if (!$this->app->has(HttpCodeDeciderInterface::class)) {
89+
$this->app->bind(HttpCodeDeciderInterface::class, HttpCodeDecider::class);
90+
}
8791

8892
$this->app->bind(HttpMessageFactoryInterface::class, PsrHttpFactory::class);
8993

9094
$this->app->singleton(GraphQLiteController::class, function (Application $app) {
9195
$debug = config('graphqlite.debug', DebugFlag::RETHROW_UNSAFE_EXCEPTIONS);
92-
$controller = new GraphQLiteController($app[StandardServer::class], $app[HttpMessageFactoryInterface::class], $debug);
93-
$decider = config('graphqlite.decider');
94-
95-
if (!empty($decider)) {
96-
$controller->setCodeDecider($app[$decider]);
96+
$decider = config('graphqlite.http_code_decider');
97+
if (!$decider) {
98+
$httpCodeDecider = $app[HttpCodeDeciderInterface::class];
99+
} else {
100+
$httpCodeDecider = $app[$decider];
97101
}
98102

99-
return $controller;
103+
return new GraphQLiteController($app[StandardServer::class], $httpCodeDecider, $app[HttpMessageFactoryInterface::class], $debug);
100104
});
101105

102106
$this->app->singleton(StandardServer::class, static function (Application $app) {

tests/Providers/GraphQLiteServiceProviderTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace TheCodingMachine\GraphQLite\Laravel\Providers;
44

55

6-
use GraphQL\Error\Debug;
6+
use GraphQL\Error\DebugFlag;
77
use GraphQL\Executor\ExecutionResult;
88
use GraphQL\Server\StandardServer;
99
use Orchestra\Testbench\TestCase;
@@ -180,8 +180,9 @@ public function testCachePurger(): void
180180
*/
181181
public function testChangeTheCodeDecider()
182182
{
183+
$this->app->instance(HttpCodeDeciderInterface::class, $this->newCodeDecider(418));
184+
183185
$controller = $this->newGraphQLiteController();
184-
$controller->setCodeDecider($this->newCodeDecider(418));
185186

186187
$response = $controller->index($this->newRequest());
187188

@@ -201,8 +202,9 @@ public function decideHttpStatusCode(ExecutionResult $result): int
201202
private function newGraphQLiteController(): GraphQLiteController
202203
{
203204
$server = $this->app->make(StandardServer::class);
205+
$httpCodeDecider = $this->app->make(HttpCodeDeciderInterface::class);
204206
$messageFactory = $this->app->make(PsrHttpFactory::class);
205-
return new GraphQLiteController($server, $messageFactory, Debug::RETHROW_UNSAFE_EXCEPTIONS);
207+
return new GraphQLiteController($server, $httpCodeDecider, $messageFactory, DebugFlag::RETHROW_UNSAFE_EXCEPTIONS);
206208
}
207209

208210
private function newRequest(): Request

0 commit comments

Comments
 (0)