Passed
Push — next ( 86cbfb...d68df8 )
by Bas
03:10
created

Connector   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 116
ccs 33
cts 35
cp 0.9429
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A request() 0 27 4
A getConfig() 0 3 1
A mapHttpClientConfig() 0 5 1
A decodeResponse() 0 12 2
A getUser() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArangoClient;
6
7
use ArangoClient\Exceptions\ArangoException;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Exception\GuzzleException;
10
use GuzzleHttp\Exception\RequestException;
11
use GuzzleHttp\Psr7\StreamWrapper;
12
use JsonMachine\JsonMachine;
13
use Psr\Http\Message\ResponseInterface;
14
15
/*
16
 * The connector handles connections to ArangoDB's HTTP REST API.
17
 * @see https://www.arangodb.com/docs/stable/http/
18
 */
19
class Connector implements ConnectorInterface
20
{
21
    /**
22
     * @var array<string|numeric|null>
23
     */
24
    protected array $config = [
25
        'host' => 'http://localhost',
26
        'port' => '8529',
27
        'AuthUser' => 'root',
28
        'AuthPassword' => null,
29
        'AuthType' => 'basic'
30
    ];
31
32
    protected Client $httpClient;
33
34
    /**
35
     * Connector constructor.
36
     *
37
     * @param  array<string|numeric|null>|null  $config
38
     * @param  Client|null  $httpClient
39
     */
40 43
    public function __construct(array $config = null, Client $httpClient = null)
41
    {
42 43
        if ($config !== null) {
43
            $this->config = $config;
44
        }
45
46 43
        $this->config = $this->mapHttpClientConfig();
47
48 43
        $this->httpClient = isset($httpClient) ? $httpClient : new Client($this->config);
49 43
    }
50
51
    /**
52
     * @psalm-suppress MixedReturnStatement
53
     *
54
     * @param  string  $method
55
     * @param  string  $uri
56
     * @param  array<mixed>  $options
57
     * @return mixed
58
     * @throws ArangoException|GuzzleException
59
     * @throws \Exception
60
     */
61 43
    public function request(string $method, string $uri, array $options = [])
62
    {
63 43
        $response = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
64
        try {
65 43
            $response = $this->httpClient->request($method, $uri, $options);
66 1
        } catch (RequestException $e) {
67 1
            $response = $e->getResponse();
68 1
            if (isset($response)) {
69 1
                $decodeResponse = $this->decodeResponse($response);
70
                throw(
71 1
                    new ArangoException(
72 1
                        (string) $decodeResponse['errorMessage'],
73 1
                        (int) $decodeResponse['code'],
74
                        $e
75
                    )
76
                );
77
            }
78
            throw($e);
79
        }
80
81 43
        $decodeResponse = $this->decodeResponse($response);
82
83 43
        if (isset($decodeResponse['result'])) {
84 43
            return $decodeResponse['result'];
85
        }
86
87 33
        return $decodeResponse;
88
    }
89
90
    /**
91
     * @return array<string|numeric|null>
92
     */
93 43
    protected function mapHttpClientConfig(): array
94
    {
95 43
        $this->config['base_uri'] = (string) $this->config['host'] . ':' . (string) $this->config['port'];
96
97 43
        return $this->config;
98
    }
99
100
    /**
101
     * @return array<string|numeric|null>
102
     */
103 1
    public function getConfig(): array
104
    {
105 1
        return $this->config;
106
    }
107
108
    /**
109
     * @psalm-suppress MixedAssignment, MixedArrayOffset
110
     * @SuppressWarnings(PHPMD.StaticAccess)
111
     *
112
     * @param  ResponseInterface  $response
113
     * @return array<array-key, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
114
     */
115 43
    protected function decodeResponse(ResponseInterface $response): array
116
    {
117 43
        $decodedResponse = [];
118
119 43
        $phpStream = StreamWrapper::getResource($response->getBody());
120 43
        $decodedStream = JsonMachine::fromStream($phpStream);
121
122 43
        foreach ($decodedStream as $key => $value) {
123 43
            $decodedResponse[$key] = $value;
124
        }
125
126 43
        return $decodedResponse;
127
    }
128
129
    /**
130
     * @return string
131
     */
132 43
    public function getUser(): string
133
    {
134 43
        return (string) $this->config['AuthUser'];
135
    }
136
}
137