Completed
Push — master ( 3b79f3...426227 )
by Kirill
03:11
created

Adapter::buildExecutor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Adapters\Webonyx;
11
12
use GraphQL\Executor\ExecutionResult;
13
use GraphQL\GraphQL;
14
use GraphQL\Type\Schema;
0 ignored issues
show
Bug introduced by
The type GraphQL\Type\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Railt\Adapters\AdapterInterface;
0 ignored issues
show
Bug introduced by
The type Railt\Adapters\AdapterInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Railt\Adapters\Webonyx\Builders\SchemaBuilder;
17
use Railt\Container\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Railt\Container\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Railt\Http\RequestInterface;
0 ignored issues
show
Bug introduced by
The type Railt\Http\RequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Railt\Http\Response;
0 ignored issues
show
Bug introduced by
The type Railt\Http\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Railt\Http\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Railt\Http\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Railt\Reflection\Contracts\Definitions\SchemaDefinition;
0 ignored issues
show
Bug introduced by
The type Railt\Reflection\Contrac...itions\SchemaDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
/**
24
 * Class Adapter
25
 */
26
class Adapter implements AdapterInterface
27
{
28
    /**
29
     * @var Registry
30
     */
31
    private $registry;
32
33
    /**
34
     * @var bool
35
     */
36
    private $debug;
37
38
    /**
39
     * Adapter constructor.
40
     * @param ContainerInterface $container
41
     * @param bool $debug
42
     */
43
    public function __construct(ContainerInterface $container, bool $debug = false)
44
    {
45
        $this->debug      = $debug;
46
        $this->registry   = new Registry($container);
47
        $container->instance(FieldResolver::class, new FieldResolver($container));
48
    }
49
50
    /**
51
     * @param SchemaDefinition $schema
52
     * @param RequestInterface $request
53
     * @return ResponseInterface
54
     * @throws \GraphQL\Error\InvariantViolation
55
     * @throws \Throwable
56
     */
57
    public function request(SchemaDefinition $schema, RequestInterface $request): ResponseInterface
58
    {
59
        $this->registry->getContainer()->instance(RequestInterface::class, $request);
60
61
        try {
62
            $executor = $this->buildExecutor($schema, $request);
63
64
            /** @var ExecutionResult $result */
65
            $result = $executor();
66
67
            return new Response((array)$result->data, $result->errors);
68
        } catch (\Throwable $e) {
69
            if ($this->debug) {
70
                throw $e;
71
            }
72
73
            return Response::error($e);
74
        }
75
    }
76
77
    /**
78
     * @param SchemaDefinition $reflection
79
     * @param RequestInterface $request
80
     * @return \Closure
81
     * @throws \GraphQL\Error\InvariantViolation
82
     */
83
    private function buildExecutor(SchemaDefinition $reflection, RequestInterface $request): \Closure
84
    {
85
        $schema = $this->buildSchema($reflection);
86
87
        if ($this->debug) {
88
            $schema->assertValid();
89
        }
90
91
        return function ($rootValue = null, $context = null) use ($reflection, $schema, $request): ExecutionResult {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

91
        return function ($rootValue = null, /** @scrutinizer ignore-unused */ $context = null) use ($reflection, $schema, $request): ExecutionResult {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
            return GraphQL::executeQuery(
0 ignored issues
show
Bug introduced by
The method executeQuery() does not exist on GraphQL\GraphQL. Did you maybe mean execute()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
            return GraphQL::/** @scrutinizer ignore-call */ executeQuery(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
                $schema,
94
                $request->getQuery(),
95
                $rootValue,
96
                $reflection,
97
                $request->getVariables(),
98
                $request->getOperation(),
99
                null /** Validations */
100
            );
101
        };
102
    }
103
104
    /**
105
     * @param SchemaDefinition $schema
106
     * @return Schema
107
     */
108
    protected function buildSchema(SchemaDefinition $schema): Schema
109
    {
110
        $builder = new SchemaBuilder($schema, $this->registry);
111
112
        return $builder->build();
113
    }
114
}
115