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