GraphQLClient::getServerParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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
use Ynlo\GraphQLBundle\Behat\Deprecation\DeprecationAdviser;
18
19
/**
20
 * Client to communicate between behat tests and symfony kernel
21
 * using GraphQL Operations.
22
 *
23
 * Bring access to the symfony kernel in order to get services etc.
24
 */
25
class GraphQLClient extends Client
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\FrameworkBundle\Client has been deprecated: since Symfony 4.3, use KernelBrowser instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

25
class GraphQLClient extends /** @scrutinizer ignore-deprecated */ Client
Loading history...
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $endpoint = '';
31
32
    /**
33
     * @var array
34
     */
35
    protected $serverParameters = [];
36
37
    /**
38
     * @var array
39
     */
40
    protected $requestsParameters = [];
41
42
    /**
43
     * @var string|null
44
     */
45
    protected $graphQL;
46
47
    /**
48
     * @var string|null
49
     */
50
    protected $operationName;
51
52
    /**
53
     * @var array
54
     */
55
    protected $variables = [];
56
57
    /**
58
     * @var Response|null
59
     */
60
    protected $response;
61
62
    /**
63
     * @var array
64
     */
65
    protected $config = [];
66
67
    /**
68
     * @var DeprecationAdviser
69
     */
70
    protected $deprecationAdviser = [];
71
72
    public function __construct(KernelInterface $kernel, DeprecationAdviser $deprecationAdviser = null, $config = [])
73
    {
74
        parent::__construct($kernel);
75
76
        $this->config = $config;
77
        $this->deprecationAdviser = $deprecationAdviser;
78
    }
79
80
    public function restart()
81
    {
82
        parent::restart();
83
84
        $this->endpoint = '';
85
        $this->graphQL = null;
86
        $this->operationName = null;
87
        $this->variables = [];
88
        $this->response = null;
89
        $this->server = [];
90
        $this->requestsParameters = [];
91
    }
92
93
    /**
94
     * @return null|string
95
     */
96
    public function getGraphQL(): ?string
97
    {
98
        return $this->graphQL;
99
    }
100
101
    /**
102
     * @param null|string $graphQL
103
     *
104
     * @return GraphQLClient
105
     */
106
    public function setGraphQL(?string $graphQL): GraphQLClient
107
    {
108
        $this->graphQL = $graphQL;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getEndpoint(): string
117
    {
118
        return $this->endpoint;
119
    }
120
121
    /**
122
     * @param string $endpoint
123
     *
124
     * @return GraphQLClient
125
     */
126
    public function setEndpoint(string $endpoint): GraphQLClient
127
    {
128
        $this->endpoint = $endpoint;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    public function getServerParameters(): array
137
    {
138
        return $this->server;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getRequestsParameters(): array
145
    {
146
        return $this->requestsParameters;
147
    }
148
149
    /**
150
     * @param array $requestsParameters
151
     *
152
     * @return GraphQLClient
153
     */
154
    public function setRequestsParameters(array $requestsParameters): GraphQLClient
155
    {
156
        $this->requestsParameters = $requestsParameters;
157
158
        return $this;
159
    }
160
161
    /**
162
     * @return null|string
163
     */
164
    public function getOperationName(): ?string
165
    {
166
        return $this->operationName;
167
    }
168
169
    /**
170
     * @param null|string $operationName
171
     *
172
     * @return GraphQLClient
173
     */
174
    public function setOperationName(?string $operationName): GraphQLClient
175
    {
176
        $this->operationName = $operationName;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @return array
183
     */
184
    public function getVariables(): array
185
    {
186
        return $this->variables;
187
    }
188
189
    /**
190
     * @param array $variables
191
     *
192
     * @return GraphQLClient
193
     */
194
    public function setVariables(array $variables): GraphQLClient
195
    {
196
        $this->variables = $variables;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Send the configured query or mutation with given variables
203
     *
204
     * @return Response
205
     */
206
    public function sendQuery(): Response
207
    {
208
        $data = [
209
            'query' => $this->getGraphQL(),
210
            'variables' => $this->getVariables(),
211
        ];
212
        if ($this->operationName) {
213
            $data['operationName'] = $this->operationName;
214
        }
215
216
        $content = json_encode($data);
217
        $this->insulated = $this->config['insulated'] ?? false;
218
219
        $this->sendRequest(Request::METHOD_POST, $this->getEndpoint(), $this->getRequestsParameters(), [], $this->getServerParameters(), $content);
220
221
        return $this->response = $this->getResponse();
222
    }
223
224
    /**
225
     * This method works like `request` in the parent class, but has a error handler to catch all deprecation notices.
226
     * Can`t be named `request` to override the parent because in projects using symfony4 the signature for this method has been changed
227
     * using strict types on each argument.
228
     *
229
     * @param string $method
230
     * @param string $uri
231
     * @param array  $parameters
232
     * @param array  $files
233
     * @param array  $server
234
     * @param null   $content
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $content is correct as it would always require null to be passed?
Loading history...
235
     * @param bool   $changeHistory
236
     *
237
     * @return \Symfony\Component\DomCrawler\Crawler
238
     */
239
    public function sendRequest($method, $uri, array $parameters = [], array $files = [], array $server = [], $content = null, $changeHistory = true)
240
    {
241
        set_error_handler(
242
            function ($level, $message, $errFile, $errLine) {
243
                if ($this->deprecationAdviser) {
244
                    $this->deprecationAdviser->addWarning($message, $errFile, $errLine);
245
                }
246
            },
247
            E_USER_DEPRECATED
248
        );
249
250
        $result = parent::request($method, $uri, $parameters, $files, $server, $content, $changeHistory);
251
252
        restore_error_handler();
253
254
        return $result;
255
    }
256
}
257