Completed
Pull Request — master (#112)
by Phil
02:21
created

Request::getFragment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Date: 23.11.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Execution;
9
10
use Youshido\GraphQL\Exception\Parser\InvalidRequestException;
11
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Literal;
12
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable;
13
use Youshido\GraphQL\Parser\Ast\ArgumentValue\VariableReference;
14
use Youshido\GraphQL\Parser\Ast\Fragment;
15
use Youshido\GraphQL\Parser\Ast\FragmentReference;
16
use Youshido\GraphQL\Parser\Ast\Mutation;
17
use Youshido\GraphQL\Parser\Ast\Query;
18
use Youshido\GraphQL\Parser\Location;
19
20
class Request
21
{
22
23
    /** @var  Query[] */
24
    private $queries = [];
25
26
    /** @var Fragment[] */
27
    private $fragments = [];
28
29
    /** @var Mutation[] */
30
    private $mutations = [];
31
32
    /** @var array */
33
    private $variables = [];
34
35
    /** @var VariableReference[] */
36
    private $variableReferences = [];
37
38
    /** @var  array */
39
    private $queryVariables = [];
40
41
    /** @var array */
42
    private $fragmentReferences = [];
43
44 62
    public function __construct($data = [], $variables = [])
45
    {
46 62
        if (array_key_exists('queries', $data)) {
47 61
            $this->addQueries($data['queries']);
48
        }
49
50 62
        if (array_key_exists('mutations', $data)) {
51 61
            $this->addMutations($data['mutations']);
52
        }
53
54 62
        if (array_key_exists('fragments', $data)) {
55 61
            $this->addFragments($data['fragments']);
56
        }
57
58 62
        if (array_key_exists('fragmentReferences', $data)) {
59 60
            $this->addFragmentReferences($data['fragmentReferences']);
60
        }
61
62 62
        if (array_key_exists('variables', $data)) {
63 60
            $this->addQueryVariables($data['variables']);
64
        }
65
66 62
        if (array_key_exists('variableReferences', $data)) {
67 60
            foreach ($data['variableReferences'] as $ref) {
68 9
                if (!array_key_exists($ref->getName(), $variables)) {
69
                    /** @var Variable $variable */
70
                    $variable = $ref->getVariable();
71
                    if ($variable->hasDefaultValue()) {
72
                        $variables[$variable->getName()] = $variable->getDefaultValue()->getValue();
73
                        continue;
74
                    }
75
                    throw new InvalidRequestException(sprintf("Variable %s hasn't been submitted", $ref->getName()), $ref->getLocation());
76
                }
77
            }
78
79 60
            $this->addVariableReferences($data['variableReferences']);
80
        }
81
82 62
        $this->setVariables($variables);
83 62
    }
84
85 61
    public function addQueries($queries)
86
    {
87 61
        foreach ($queries as $query) {
88 53
            $this->queries[] = $query;
89
        }
90 61
    }
91
92 61
    public function addMutations($mutations)
93
    {
94 61
        foreach ($mutations as $mutation) {
95 10
            $this->mutations[] = $mutation;
96
        }
97 61
    }
98
99 60
    public function addQueryVariables($queryVariables)
100
    {
101 60
        foreach ($queryVariables as $queryVariable) {
102 10
            $this->queryVariables[] = $queryVariable;
103
        }
104 60
    }
105
106 60
    public function addVariableReferences($variableReferences)
107
    {
108 60
        foreach ($variableReferences as $variableReference) {
109 9
            $this->variableReferences[] = $variableReference;
110
        }
111 60
    }
112
113 60
    public function addFragmentReferences($fragmentReferences)
114
    {
115 60
        foreach ($fragmentReferences as $fragmentReference) {
116 5
            $this->fragmentReferences[] = $fragmentReference;
117
        }
118 60
    }
119
120 61
    public function addFragments($fragments)
121
    {
122 61
        foreach ($fragments as $fragment) {
123 5
            $this->addFragment($fragment);
124
        }
125 61
    }
126
127
    /**
128
     * @return Query[]
129
     */
130 59
    public function getAllOperations()
131
    {
132 59
        return array_merge($this->mutations, $this->queries);
133
    }
134
135
    /**
136
     * @return Query[]
137
     */
138 1
    public function getQueries()
139
    {
140 1
        return $this->queries;
141
    }
142
143
    /**
144
     * @return Fragment[]
145
     */
146 63
    public function getFragments()
147
    {
148 63
        return $this->fragments;
149
    }
150
151 5
    public function addFragment(Fragment $fragment)
152
    {
153 5
        $this->fragments[] = $fragment;
154 5
    }
155
156
    /**
157
     * @param $name
158
     *
159
     * @return null|Fragment
160
     */
161 9
    public function getFragment($name)
162
    {
163 9
        foreach ($this->fragments as $fragment) {
164 7
            if ($fragment->getName() == $name) {
165 7
                return $fragment;
166
            }
167
        }
168
169 4
        return null;
170
    }
171
172
    /**
173
     * @return Mutation[]
174
     */
175 1
    public function getMutations()
176
    {
177 1
        return $this->mutations;
178
    }
179
180
    /**
181
     * @return bool
182
     */
183 1
    public function hasQueries()
184
    {
185 1
        return (bool)count($this->queries);
186
    }
187
188
    /**
189
     * @return bool
190
     */
191 1
    public function hasMutations()
192
    {
193 1
        return (bool)count($this->mutations);
194
    }
195
196
    /**
197
     * @return bool
198
     */
199 1
    public function hasFragments()
200
    {
201 1
        return (bool)count($this->fragments);
202
    }
203
204
    /**
205
     * @return array
206
     */
207 2
    public function getVariables()
208
    {
209 2
        return $this->variables;
210
    }
211
212
    /**
213
     * @param array $variables
214
     *
215
     * @return $this
216
     */
217 62
    public function setVariables($variables)
218
    {
219 62
        if (!is_array($variables)) {
220 1
            $variables = json_decode($variables, true);
221
        }
222
223 62
        $this->variables = $variables;
0 ignored issues
show
Documentation Bug introduced by
It seems like $variables of type * is incompatible with the declared type array of property $variables.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
224 62
        foreach ($this->variableReferences as $reference) {
225
            /** invalid request with no variable */
226 9
            if (!$reference->getVariable()) continue;
227 9
            $variableName = $reference->getVariable()->getName();
228
229
            /** no variable was set at the time */
230 9
            if (!array_key_exists($variableName, $variables)) continue;
231
232 9
            $reference->getVariable()->setValue($variables[$variableName]);
233 9
            $reference->setValue($variables[$variableName]);
234
        }
235
236 62
        return $this;
237
    }
238
239 9
    public function getVariable($name)
240
    {
241 9
        return $this->hasVariable($name) ? $this->variables[$name] : null;
242
    }
243
244 9
    public function hasVariable($name)
245
    {
246 9
        return array_key_exists($name, $this->variables);
247
    }
248
249
    /**
250
     * @return array|Variable[]
251
     */
252 60
    public function getQueryVariables()
253
    {
254 60
        return $this->queryVariables;
255
    }
256
257
    /**
258
     * @param array $queryVariables
259
     */
260
    public function setQueryVariables($queryVariables)
261
    {
262
        $this->queryVariables = $queryVariables;
263
    }
264
265
    /**
266
     * @return array|FragmentReference[]
267
     */
268 65
    public function getFragmentReferences()
269
    {
270 65
        return $this->fragmentReferences;
271
    }
272
273
    /**
274
     * @param array $fragmentReferences
275
     */
276
    public function setFragmentReferences($fragmentReferences)
277
    {
278
        $this->fragmentReferences = $fragmentReferences;
279
    }
280
281
    /**
282
     * @return array|VariableReference[]
283
     */
284 61
    public function getVariableReferences()
285
    {
286 61
        return $this->variableReferences;
287
    }
288
}
289