GraphQLMiddleware::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 2
Metric Value
cc 2
eloc 7
c 5
b 0
f 2
nc 2
nop 2
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\Spiral\Middleware;
6
7
use Andi\GraphQL\Spiral\Config\GraphQLConfig;
8
use GraphQL\Server\StandardServer;
9
use Psr\Http\Message\ResponseFactoryInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Message\StreamFactoryInterface;
13
use Psr\Http\Server\MiddlewareInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
use Spiral\Core\Container\SingletonInterface;
16
17
final class GraphQLMiddleware implements MiddlewareInterface, SingletonInterface
18
{
19 3
    public function __construct(
20
        private readonly GraphQLConfig $config,
21
        private readonly StandardServer $server,
22
        private readonly ResponseFactoryInterface $responseFactory,
23
        private readonly StreamFactoryInterface $streamFactory,
24
    ) {
25 3
    }
26
27 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
28
    {
29 2
        if ($request->getUri()->getPath() === $this->config->getUrl()) {
30 1
            $response = $this->responseFactory->createResponse();
31 1
            $stream = $this->streamFactory->createStream();
32
33 1
            $result = $this->server->processPsrRequest($request, $response, $stream);
34 1
            \assert($result instanceof ResponseInterface);
35 1
            return $result;
36
        }
37
38 1
        return $handler->handle($request);
39
    }
40
}
41