Completed
Push — master ( 17976d...325209 )
by Portey
07:00
created

Request::addFragments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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