Completed
Pull Request — master (#13)
by Rafael
06:46
created

ContextBuilder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 16
eloc 40
dl 0
loc 163
ccs 39
cts 42
cp 0.9286
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setResolveInfo() 0 5 1
A build() 0 13 1
A create() 0 3 1
A __construct() 0 3 1
A setMetas() 0 5 1
A setRoot() 0 5 1
B setDefinition() 0 20 8
A setMeta() 0 5 1
A setArgs() 0 5 1
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Resolver;
12
13
use GraphQL\Type\Definition\ResolveInfo;
14
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface;
15
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
16
use Ynlo\GraphQLBundle\Definition\NodeAwareDefinitionInterface;
17
use Ynlo\GraphQLBundle\Definition\ObjectDefinition;
18
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
19
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
20
21
final class ContextBuilder
22
{
23
    /**
24
     * @var Endpoint
25
     */
26
    private $endpoint;
27
28
    /**
29
     * @var ExecutableDefinitionInterface
30
     */
31
    private $definition;
32
33
    /**
34
     * @var mixed
35
     */
36
    private $root;
37
38
    /**
39
     * @var ObjectDefinitionInterface
40
     */
41
    private $node;
42
43
    /**
44
     * Array of arguments given
45
     *
46
     * @var array
47
     */
48
    private $args = [];
49
50
    /**
51
     * @var ResolveInfo
52
     */
53
    private $resolveInfo;
54
55
    /**
56
     * @var array
57
     */
58
    private $metas = [];
59
60
    /**
61
     * ContextBuilder constructor.
62
     *
63
     * @param Endpoint $endpoint
64
     */
65 3
    private function __construct(Endpoint $endpoint)
66
    {
67 3
        $this->endpoint = $endpoint;
68 3
    }
69
70
    /**
71
     * @param Endpoint $endpoint
72
     *
73
     * @return ContextBuilder
74
     */
75 3
    public static function create(Endpoint $endpoint)
76
    {
77 3
        return new self($endpoint);
78
    }
79
80
    /**
81
     * @return ResolverContext
82
     */
83 3
    public function build(): ResolverContext
84
    {
85 3
        $context = new ResolverContext(
86 3
            $this->endpoint,
87 3
            $this->definition,
88 3
            $this->root,
89 3
            $this->node,
90 3
            $this->args,
91 3
            $this->resolveInfo,
92 3
            $this->metas
93
        );
94
95 3
        return $context;
96
    }
97
98
    /**
99
     * @param array $metas
100
     *
101
     * @return ContextBuilder
102
     */
103 1
    public function setMetas(array $metas): ContextBuilder
104
    {
105 1
        $this->metas = $metas;
106
107 1
        return $this;
108
    }
109
110
    /**
111
     * @param string $key
112
     * @param mixed  $value
113
     *
114
     * @return ContextBuilder
115
     */
116
    public function setMeta(string $key, $value): ContextBuilder
117
    {
118
        $this->metas[$key] = $value;
119
120
        return $this;
121
    }
122
123
    /**
124
     * @param ExecutableDefinitionInterface $definition
125
     *
126
     * @return ContextBuilder
127
     */
128 3
    public function setDefinition(ExecutableDefinitionInterface $definition): ContextBuilder
129
    {
130 3
        $this->definition = $definition;
131
132 3
        $type = null;
133 3
        if ($definition instanceof NodeAwareDefinitionInterface && $definition->getNode()) {
134 1
            $type = $definition->getNode();
135
        }
136
137 3
        if (!$type) {
138 2
            $type = $definition->getType();
139
        }
140
141 3
        if ($this->endpoint->hasType($type)
142 3
            && ($nodeDefinition = $this->endpoint->getType($type))
143 3
            && ($nodeDefinition instanceof ObjectDefinition || $nodeDefinition instanceof InterfaceDefinition)) {
144 1
            $this->node = $nodeDefinition;
145
        }
146
147 3
        return $this;
148
    }
149
150
    /**
151
     * @param mixed $root
152
     *
153
     * @return ContextBuilder
154
     */
155 1
    public function setRoot($root): ContextBuilder
156
    {
157 1
        $this->root = $root;
158
159 1
        return $this;
160
    }
161
162
    /**
163
     * @param array $args
164
     *
165
     * @return ContextBuilder
166
     */
167 1
    public function setArgs(array $args): ContextBuilder
168
    {
169 1
        $this->args = $args;
170
171 1
        return $this;
172
    }
173
174
    /**
175
     * @param ResolveInfo $resolveInfo
176
     *
177
     * @return ContextBuilder
178
     */
179 1
    public function setResolveInfo(ResolveInfo $resolveInfo): ContextBuilder
180
    {
181 1
        $this->resolveInfo = $resolveInfo;
182
183 1
        return $this;
184
    }
185
}
186