|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArangoClient; |
|
6
|
|
|
|
|
7
|
|
|
use ArangoClient\Exceptions\ArangoDbException; |
|
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
|
|
|
public function __construct(array $config = null, Client $httpClient = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($config !== null) { |
|
43
|
|
|
$this->config = $config; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->config = $this->mapHttpClientConfig(); |
|
47
|
|
|
|
|
48
|
|
|
$this->httpClient = isset($httpClient) ? $httpClient : new Client($this->config); |
|
49
|
|
|
} |
|
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 ArangoDbException|GuzzleException |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
|
|
public function request(string $method, string $uri, array $options = []) |
|
62
|
|
|
{ |
|
63
|
|
|
$response = null; |
|
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
$response = $this->httpClient->request($method, $uri, $options); |
|
66
|
|
|
} catch (RequestException $e) { |
|
67
|
|
|
$response = $e->getResponse(); |
|
68
|
|
|
if (isset($response)) { |
|
69
|
|
|
$decodeResponse = $this->decodeResponse($response); |
|
70
|
|
|
throw( |
|
71
|
|
|
new ArangoDbException( |
|
72
|
|
|
(string) $decodeResponse['errorMessage'], |
|
73
|
|
|
(int) $decodeResponse['code'], |
|
74
|
|
|
$e |
|
75
|
|
|
) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
throw($e); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$decodeResponse = $this->decodeResponse($response); |
|
82
|
|
|
|
|
83
|
|
|
if (isset($decodeResponse['result'])) { |
|
84
|
|
|
return $decodeResponse['result']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $decodeResponse; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return array<string|numeric|null> |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function mapHttpClientConfig(): array |
|
94
|
|
|
{ |
|
95
|
|
|
$this->config['base_uri'] = (string) $this->config['host'] . ':' . (string) $this->config['port']; |
|
96
|
|
|
|
|
97
|
|
|
return $this->config; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return array<string|numeric|null> |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getConfig(): array |
|
104
|
|
|
{ |
|
105
|
|
|
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> |
|
|
|
|
|
|
114
|
|
|
*/ |
|
115
|
|
|
protected function decodeResponse(ResponseInterface $response): array |
|
116
|
|
|
{ |
|
117
|
|
|
$decodedResponse = []; |
|
118
|
|
|
|
|
119
|
|
|
$phpStream = StreamWrapper::getResource($response->getBody()); |
|
120
|
|
|
$decodedStream = JsonMachine::fromStream($phpStream); |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
foreach ($decodedStream as $key => $value) { |
|
124
|
|
|
$decodedResponse[$key] = $value; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $decodedResponse; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|