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

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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