Completed
Pull Request — master (#8)
by
unknown
10:29
created

Client   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 33
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A query() 0 17 2
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 6
    public function __construct(ClientInterface $httpClient, ResponseBuilder $responseBuilder)
14
    {
15 6
        $this->httpClient = $httpClient;
16 6
        $this->responseBuilder = $responseBuilder;
17 6
    }
18
19
    /**
20
     * @throws \UnexpectedValueException When response body is not a valid json
21
     * @throws \RuntimeException         When there are transfer errors
22
     */
23 2
    public function query(string $query, array $variables = []): Response
24
    {
25
        $options = [
26
            'json' => [
27 2
                'query' => $query,
28 2
                'variables' => json_encode($variables),
29
            ],
30
        ];
31
32
        try {
33 2
            $response = $this->httpClient->request('POST', '', $options);
34 2
        } catch (TransferException $e) {
35 2
            throw new \RuntimeException('Network Error.');
36
        }
37
38
        return $this->responseBuilder->build($response);
39
    }
40
}
41