Client::query()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 8
cts 8
cp 1
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
1
<?php
2
3
namespace Softonic\GraphQL;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\TransferException;
7
8
class Client
9
{
10
    private $httpClient;
11
    private $responseBuilder;
12
13 18
    public function __construct(ClientInterface $httpClient, ResponseBuilder $responseBuilder)
14
    {
15 18
        $this->httpClient = $httpClient;
16 18
        $this->responseBuilder = $responseBuilder;
17 18
    }
18
19
    /**
20
     * @throws \UnexpectedValueException When response body is not a valid json
21
     * @throws \RuntimeException         When there are transfer errors
22
     */
23 10
    public function query(string $query, array $variables = null): Response
24
    {
25
        $options = [
26
            'json' => [
27 10
                'query' => $query,
28
            ],
29
        ];
30 10
        if (!is_null($variables)) {
31 2
            $options['json']['variables'] = $variables;
32
        }
33
34
        try {
35 10
            $response = $this->httpClient->request('POST', '', $options);
36 4
        } catch (TransferException $e) {
37 4
            throw new \RuntimeException('Network Error.' . $e->getMessage(), 0, $e);
38
        }
39
40 6
        return $this->responseBuilder->build($response);
41
    }
42
}
43