Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

Request::hasVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Fragment;
12
use Youshido\GraphQL\Parser\Ast\Mutation;
13
use Youshido\GraphQL\Parser\Ast\Query;
14
15
class Request
16
{
17
18
    /** @var  Query[] */
19
    private $queries = [];
20
21
    /** @var Fragment[] */
22
    private $fragments = [];
23
24
    /** @var Mutation[] */
25
    private $mutations = [];
26
27
    /** @var array */
28
    private $variables = [];
29
30 23
    public function __construct($data = [], $variables = [])
31
    {
32 23
        if (array_key_exists('queries', $data)) {
33 22
            $this->addQueries($data['queries']);
34 22
        }
35
36 23
        if (array_key_exists('mutations', $data)) {
37 22
            $this->addMutations($data['mutations']);
38 22
        }
39
40 23
        if (array_key_exists('fragments', $data)) {
41 22
            $this->addFragments($data['fragments']);
42 22
        }
43 23
        $this->setVariables($variables);
44 23
    }
45
46 22
    public function addQueries($queries)
47
    {
48 22
        foreach ($queries as $query) {
49 22
            $this->queries[] = $query;
50 22
        }
51 22
    }
52
53 22
    public function addMutations($mutations)
54
    {
55 22
        foreach ($mutations as $mutation) {
56 2
            $this->mutations[] = $mutation;
57 22
        }
58 22
    }
59
60 22
    public function addFragments($fragments)
61
    {
62 22
        foreach ($fragments as $fragment) {
63 3
            $this->fragments[] = $fragment;
64 22
        }
65 22
    }
66
67 21
    public function getOperationsInOrder()
68
    {
69 21
        return array_merge($this->mutations, $this->queries);
70
    }
71
72
    /**
73
     * @return Query[]
74
     */
75 1
    public function getQueries()
76
    {
77 1
        return $this->queries;
78
    }
79
80
    /**
81
     * @return Fragment[]
82
     */
83 1
    public function getFragments()
84
    {
85 1
        return $this->fragments;
86
    }
87
88 1
    public function addFragment(Fragment $fragment)
89
    {
90 1
        $this->fragments[] = $fragment;
91 1
    }
92
93
    /**
94
     * @param $name
95
     *
96
     * @return null|Fragment
97
     */
98 3
    public function getFragment($name)
99
    {
100 3
        foreach ($this->fragments as $fragment) {
101 3
            if ($fragment->getName() == $name) {
102 3
                return $fragment;
103
            }
104 3
        }
105
106 1
        return null;
107
    }
108
109
    /**
110
     * @return Mutation[]
111
     */
112 1
    public function getMutations()
113
    {
114 1
        return $this->mutations;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120 1
    public function hasQueries()
121
    {
122 1
        return (bool)count($this->queries);
123
    }
124
125
    /**
126
     * @return bool
127
     */
128 1
    public function hasMutations()
129
    {
130 1
        return (bool)count($this->mutations);
131
    }
132
133
    /**
134
     * @return bool
135
     */
136 1
    public function hasFragments()
137
    {
138 1
        return (bool)count($this->fragments);
139
    }
140
141
    /**
142
     * @return array
143
     */
144 1
    public function getVariables()
145
    {
146 1
        return $this->variables;
147
    }
148
149
    /**
150
     * @param array $variables
151
     * @return $this
152
     */
153 23
    public function setVariables($variables)
154
    {
155 23
        $this->variables = $variables;
156
157 23
        return $this;
158
    }
159
160 3
    public function getVariable($name)
161
    {
162 3
        return $this->hasVariable($name) ? $this->variables[$name] : null;
163
    }
164
165 3
    public function hasVariable($name)
166
    {
167 3
        return array_key_exists($name, $this->variables);
168
    }
169
170
}
171