Completed
Push — master ( 29ef50...718f9d )
by Alexandr
03:01
created

Request::setVariables()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 8.7624
cc 5
eloc 11
nc 8
nop 1
crap 5
1
<?php
2
/**
3
 * Date: 23.11.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Execution;
9
10
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
19
class Request
20
{
21
22
    /** @var  Query[] */
23
    private $queries = [];
24
25
    /** @var Fragment[] */
26
    private $fragments = [];
27
28
    /** @var Mutation[] */
29
    private $mutations = [];
30
31
    /** @var array */
32
    private $variables = [];
33
34
    /** @var VariableReference[] */
35
    private $variableReferences = [];
36
37
    /** @var  array */
38
    private $queryVariables = [];
39
40
    /** @var array */
41
    private $fragmentReferences = [];
42
43 56
    public function __construct($data = [], $variables = [])
44
    {
45 56
        if (array_key_exists('queries', $data)) {
46 55
            $this->addQueries($data['queries']);
47
        }
48
49 56
        if (array_key_exists('mutations', $data)) {
50 55
            $this->addMutations($data['mutations']);
51
        }
52
53 56
        if (array_key_exists('fragments', $data)) {
54 55
            $this->addFragments($data['fragments']);
55
        }
56
57 56
        if (array_key_exists('fragmentReferences', $data)) {
58 54
            $this->addFragmentReferences($data['fragmentReferences']);
59
        }
60
61 56
        if (array_key_exists('variables', $data)) {
62 54
            $this->addQueryVariables($data['variables']);
63
        }
64
65 56
        if (array_key_exists('variableReferences', $data)) {
66 54
            $this->addVariableReferences($data['variableReferences']);
67
        }
68
69 56
        $this->setVariables($variables);
70 56
    }
71
72 55
    public function addQueries($queries)
73
    {
74 55
        foreach ($queries as $query) {
75 50
            $this->queries[] = $query;
76
        }
77 55
    }
78
79 55
    public function addMutations($mutations)
80
    {
81 55
        foreach ($mutations as $mutation) {
82 7
            $this->mutations[] = $mutation;
83
        }
84 55
    }
85
86 54
    public function addQueryVariables($queryVariables)
87
    {
88 54
        foreach ($queryVariables as $queryVariable) {
89 7
            $this->queryVariables[] = $queryVariable;
90
        }
91 54
    }
92
93 54
    public function addVariableReferences($variableReferences)
94
    {
95 54
        foreach ($variableReferences as $variableReference) {
96 6
            $this->variableReferences[] = $variableReference;
97
        }
98 54
    }
99
100 54
    public function addFragmentReferences($fragmentReferences)
101
    {
102 54
        foreach ($fragmentReferences as $fragmentReference) {
103 5
            $this->fragmentReferences[] = $fragmentReference;
104
        }
105 54
    }
106
107 55
    public function addFragments($fragments)
108
    {
109 55
        foreach ($fragments as $fragment) {
110 5
            $this->addFragment($fragment);
111
        }
112 55
    }
113
114
    /**
115
     * @return Query[]
116
     */
117 53
    public function getAllOperations()
118
    {
119 53
        return array_merge($this->mutations, $this->queries);
120
    }
121
122
    /**
123
     * @return Query[]
124
     */
125 1
    public function getQueries()
126
    {
127 1
        return $this->queries;
128
    }
129
130
    /**
131
     * @return Fragment[]
132
     */
133 57
    public function getFragments()
134
    {
135 57
        return $this->fragments;
136
    }
137
138 5
    public function addFragment(Fragment $fragment)
139
    {
140 5
        $this->fragments[] = $fragment;
141 5
    }
142
143
    /**
144
     * @param $name
145
     *
146
     * @return null|Fragment
147
     */
148 9
    public function getFragment($name)
149
    {
150 9
        foreach ($this->fragments as $fragment) {
151 7
            if ($fragment->getName() == $name) {
152 7
                return $fragment;
153
            }
154
        }
155
156 4
        return null;
157
    }
158
159
    /**
160
     * @return Mutation[]
161
     */
162 1
    public function getMutations()
163
    {
164 1
        return $this->mutations;
165
    }
166
167
    /**
168
     * @return bool
169
     */
170 1
    public function hasQueries()
171
    {
172 1
        return (bool)count($this->queries);
173
    }
174
175
    /**
176
     * @return bool
177
     */
178 1
    public function hasMutations()
179
    {
180 1
        return (bool)count($this->mutations);
181
    }
182
183
    /**
184
     * @return bool
185
     */
186 1
    public function hasFragments()
187
    {
188 1
        return (bool)count($this->fragments);
189
    }
190
191
    /**
192
     * @return array
193
     */
194 2
    public function getVariables()
195
    {
196 2
        return $this->variables;
197
    }
198
199
    /**
200
     * @param array $variables
201
     *
202
     * @return $this
203
     */
204 56
    public function setVariables($variables)
205
    {
206 56
        if (!is_array($variables)) {
207 1
            $variables = json_decode($variables, true);
208
        }
209
210 56
        $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...
211 56
        foreach($this->variableReferences as $reference) {
212
            /** invalid request with no variable */
213 6
            if (!$reference->getVariable()) continue;
214 6
            $variableName = $reference->getVariable()->getName();
215
216
            /** no variable was set at the time */
217 6
            if (!array_key_exists($variableName, $variables)) continue;
218
219 5
            $reference->getVariable()->setValue($variables[$variableName]);
220 5
            $reference->setValue($variables[$variableName]);
221
        }
222
223 56
        return $this;
224
    }
225
226 6
    public function getVariable($name)
227
    {
228 6
        return $this->hasVariable($name) ? $this->variables[$name] : null;
229
    }
230
231 6
    public function hasVariable($name)
232
    {
233 6
        return array_key_exists($name, $this->variables);
234
    }
235
236
    /**
237
     * @return array|Variable[]
238
     */
239 54
    public function getQueryVariables()
240
    {
241 54
        return $this->queryVariables;
242
    }
243
244
    /**
245
     * @param array $queryVariables
246
     */
247
    public function setQueryVariables($queryVariables)
248
    {
249
        $this->queryVariables = $queryVariables;
250
    }
251
252
    /**
253
     * @return array|FragmentReference[]
254
     */
255 59
    public function getFragmentReferences()
256
    {
257 59
        return $this->fragmentReferences;
258
    }
259
260
    /**
261
     * @param array $fragmentReferences
262
     */
263
    public function setFragmentReferences($fragmentReferences)
264
    {
265
        $this->fragmentReferences = $fragmentReferences;
266
    }
267
268
    /**
269
     * @return array|VariableReference[]
270
     */
271 55
    public function getVariableReferences()
272
    {
273 55
        return $this->variableReferences;
274
    }
275
}
276