GraphQLMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 4
dl 0
loc 6
ccs 1
cts 1
cp 1
crap 1
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