Passed
Pull Request — master (#9)
by Rafael
03:29
created

GraphQLClient::sendQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 6
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\Behat\Client;
12
13
use Symfony\Bundle\FrameworkBundle\Client;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\KernelInterface;
17
18
/**
19
 * Client to communicate between behat tests and symfony kernel
20
 * using GraphQL Operations.
21
 *
22
 * Bring access to the symfony kernel in order to get services etc.
23
 */
24
class GraphQLClient extends Client
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $endpoint = '';
30
31
    /**
32
     * @var array
33
     */
34
    protected $serverParameters = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $requestsParameters = [];
40
41
    /**
42
     * @var string|null
43
     */
44
    protected $graphQL;
45
46
    /**
47
     * @var string|null
48
     */
49
    protected $operationName;
50
51
    /**
52
     * @var array
53
     */
54
    protected $variables = [];
55
56
    /**
57
     * @var Response|null
58
     */
59
    protected $response;
60
61
    /**
62
     * @var array
63
     */
64
    protected $config = [];
65
66
    public function __construct(KernelInterface $kernel, $config = [])
67
    {
68
        parent::__construct($kernel);
69
70
        $this->config = $config;
71
    }
72
73
    public function restart()
74
    {
75
        parent::restart();
76
77
        $this->endpoint = '';
78
        $this->graphQL = null;
79
        $this->operationName = null;
80
        $this->variables = [];
81
        $this->response = null;
82
        $this->server = [];
83
        $this->requestsParameters = [];
84
    }
85
86
    /**
87
     * @return null|string
88
     */
89
    public function getGraphQL(): ?string
90
    {
91
        return $this->graphQL;
92
    }
93
94
    /**
95
     * @param null|string $graphQL
96
     *
97
     * @return GraphQLClient
98
     */
99
    public function setGraphQL(?string $graphQL): GraphQLClient
100
    {
101
        $this->graphQL = $graphQL;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getEndpoint(): string
110
    {
111
        return $this->endpoint;
112
    }
113
114
    /**
115
     * @param string $endpoint
116
     *
117
     * @return GraphQLClient
118
     */
119
    public function setEndpoint(string $endpoint): GraphQLClient
120
    {
121
        $this->endpoint = $endpoint;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function getServerParameters(): array
130
    {
131
        return $this->server;
132
    }
133
134
    /**
135
     * @return array
136
     */
137
    public function getRequestsParameters(): array
138
    {
139
        return $this->requestsParameters;
140
    }
141
142
    /**
143
     * @param array $requestsParameters
144
     *
145
     * @return GraphQLClient
146
     */
147
    public function setRequestsParameters(array $requestsParameters): GraphQLClient
148
    {
149
        $this->requestsParameters = $requestsParameters;
150
151
        return $this;
152
    }
153
154
    /**
155
     * @return null|string
156
     */
157
    public function getOperationName(): ?string
158
    {
159
        return $this->operationName;
160
    }
161
162
    /**
163
     * @param null|string $operationName
164
     *
165
     * @return GraphQLClient
166
     */
167
    public function setOperationName(?string $operationName): GraphQLClient
168
    {
169
        $this->operationName = $operationName;
170
171
        return $this;
172
    }
173
174
    /**
175
     * @return array
176
     */
177
    public function getVariables(): array
178
    {
179
        return $this->variables;
180
    }
181
182
    /**
183
     * @param array $variables
184
     *
185
     * @return GraphQLClient
186
     */
187
    public function setVariables(array $variables): GraphQLClient
188
    {
189
        $this->variables = $variables;
190
191
        return $this;
192
    }
193
194
    /**
195
     * Send the configured query or mutation with given variables
196
     *
197
     * @return Response
198
     */
199
    public function sendQuery(): Response
200
    {
201
        $data = [
202
            'query' => $this->getGraphQL(),
203
            'variables' => $this->getVariables(),
204
        ];
205
        if ($this->operationName) {
206
            $data['operationName'] = $this->operationName;
207
        }
208
209
        $content = json_encode($data);
210
        $this->insulated = $this->config['insulated'] ?? false;
211
212
        $this->request(Request::METHOD_POST, $this->getEndpoint(), $this->getRequestsParameters(), [], $this->getServerParameters(), $content);
213
214
        return $this->response = $this->getResponse();
215
    }
216
}
217