Test Setup Failed
Push — master ( 31905c...c65f95 )
by Kirill
02:41
created

Adapter::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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\GraphQL;
13
use GraphQL\Schema;
14
use Railt\Adapters\AdapterInterface;
15
use Railt\Adapters\TypeLoaderInterface;
16
use Railt\Adapters\Webonyx\Builder\SchemaTypeBuilder;
17
use Railt\Container\ContainerInterface;
18
use Railt\Events\DispatcherInterface;
19
use Railt\Http\RequestInterface;
20
use Railt\Http\Response;
21
use Railt\Http\ResponseInterface;
22
use Railt\Reflection\Abstraction\DocumentTypeInterface;
23
use Railt\Routing\Router;
24
use Railt\Support\Debuggable;
25
use Railt\Support\DebuggableInterface;
26
27
/**
28
 * Class Adapter
29
 *
30
 * @package Railt\Adapters\Webonyx
31
 */
32
class Adapter implements AdapterInterface, DebuggableInterface
33
{
34
    use Debuggable;
35
36
    /**
37
     * @var DocumentTypeInterface
38
     */
39
    private $document;
40
41
    /**
42
     * @var DispatcherInterface
43
     */
44
    private $events;
45
46
    /**
47
     * @var Router
48
     */
49
    private $router;
50
51
    /**
52
     * @var Loader|TypeLoaderInterface
53
     */
54
    private $loader;
55
56
    /**
57
     * Webonyx constructor.
58
     *
59
     * @param DocumentTypeInterface $document
60
     * @param DispatcherInterface $events
61
     * @param Router $router
62
     */
63
    public function __construct(DocumentTypeInterface $document, DispatcherInterface $events, Router $router)
64
    {
65
        $this->document = $document;
66
        $this->events = $events;
67
        $this->router = $router;
68
    }
69
70
    /**
71
     * @return ContainerInterface
72
     */
73
    public function getContainer(): ContainerInterface
74
    {
75
        // TODO: Implement the receipt of the container directly; not through the Router.
76
        return $this->router->getContainer();
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public static function isSupported(): bool
83
    {
84
        return class_exists(GraphQL::class);
85
    }
86
87
    /**
88
     * @return Router
89
     */
90
    public function getRouter(): Router
91
    {
92
        return $this->router;
93
    }
94
95
    /**
96
     * @return DispatcherInterface
97
     */
98
    public function getEvents(): DispatcherInterface
99
    {
100
        return $this->events;
101
    }
102
103
    /**
104
     * @param RequestInterface $request
105
     * @return Response|ResponseInterface
106
     * @throws \LogicException
107
     * @throws \GraphQL\Error\InvariantViolation
108
     */
109
    public function request(RequestInterface $request): ResponseInterface
110
    {
111
        $schema = $this->buildSchema();
112
113
        $value = $this->executeSchema($request, $schema);
114
115
        [$data, $errors] = [
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $errors does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
116
            (array)($value['data'] ?? []),
117
            (array)($value['errors'] ?? []),
118
        ];
119
120
        $response = new Response($data, $errors);
121
        $response->debugMode($this->debug);
122
123
        return $response;
124
    }
125
126
    /**
127
     * @return Schema
128
     * @throws \LogicException
129
     */
130
    private function buildSchema(): Schema
131
    {
132
        $schema = $this->document->getSchema();
133
134
        return $this->getLoader()->make($schema, SchemaTypeBuilder::class);
0 ignored issues
show
Bug introduced by
It seems like $schema defined by $this->document->getSchema() on line 132 can be null; however, Railt\Adapters\TypeLoaderInterface::make() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
135
    }
136
137
    /**
138
     * @return TypeLoaderInterface
139
     */
140
    public function getLoader(): TypeLoaderInterface
141
    {
142
        if ($this->loader === null) {
143
            $this->loader = new Loader($this->document, $this);
144
        }
145
146
        return $this->loader;
147
    }
148
149
    /**
150
     * @param RequestInterface $request
151
     * @param Schema $schema
152
     * @return array
153
     * @throws \GraphQL\Error\InvariantViolation
154
     */
155
    private function executeSchema(RequestInterface $request, Schema $schema): array
156
    {
157
        return GraphQL::execute(
158
            $schema,
159
            $request->getQuery(),
160
            null,
161
            null,
162
            $request->getVariables(),
163
            $request->getOperation()
164
        );
165
    }
166
}
167