Client   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 35
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A query() 0 19 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