1 | <?php |
||
33 | class Client implements ClientInterface |
||
34 | { |
||
35 | |||
36 | const DEFAULT_SERVER = "localhost:4200"; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $availableServers = []; |
||
42 | |||
43 | 1 | /** |
|
44 | * @var array |
||
45 | 1 | */ |
|
46 | 1 | private $serverPool = []; |
|
47 | |||
48 | /** |
||
49 | * Client constructor. |
||
50 | * @param array $servers |
||
51 | 2 | * @param array $options |
|
52 | */ |
||
53 | public function __construct(array $servers, array $options) |
||
65 | 1 | ||
66 | 1 | /** |
|
67 | * {@Inheritdoc} |
||
68 | */ |
||
69 | 1 | public function execute($query, array $parameters) |
|
78 | 1 | ||
79 | /** |
||
80 | 1 | * @param string $server |
|
81 | * @param array $body |
||
82 | * @return Collection |
||
83 | */ |
||
84 | private function sqlRequest($server, array $body) |
||
85 | { |
||
86 | while (true) { |
||
87 | $nextServer = $server == null ? $this->nextServer() : $server; |
||
88 | /** @var Server $s */ |
||
89 | 1 | $s = $this->serverPool[$nextServer]; |
|
90 | |||
91 | 1 | try { |
|
92 | |||
93 | $response = $s->doRequest($body); |
||
94 | $responseBody = $response->json(); |
||
95 | |||
96 | return new Collection( |
||
97 | 1 | $responseBody['rows'], |
|
98 | $responseBody['cols'], |
||
99 | 1 | $responseBody['duration'], |
|
100 | $responseBody['rowcount'] |
||
101 | ); |
||
102 | |||
103 | } catch (ConnectException $exception) { |
||
104 | |||
105 | 1 | if ($server != null) { |
|
106 | throw $exception; |
||
107 | 1 | } else { |
|
108 | 1 | $this->dropServer($server, $exception); |
|
109 | } |
||
110 | |||
111 | } catch (BadResponseException $exception) { |
||
112 | |||
113 | try { |
||
114 | |||
115 | $json = $exception->getResponse()->json(); |
||
116 | |||
117 | $errorCode = $json['error']['code']; |
||
118 | $errorMessage = $json['error']['message']; |
||
119 | |||
120 | throw new RuntimeException($errorMessage, $errorCode, $exception); |
||
121 | 1 | ||
122 | } catch (ParseException $e) { |
||
123 | 1 | throw new RuntimeException('Server returned non-JSON response.', 0, $exception); |
|
124 | 1 | } |
|
125 | |||
126 | } |
||
127 | } |
||
128 | return null; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * {@Inheritdoc} |
||
133 | */ |
||
134 | public function getServerInfo() |
||
138 | |||
139 | /** |
||
140 | * {@Inheritdoc} |
||
141 | */ |
||
142 | public function getServerVersion() |
||
146 | |||
147 | /** |
||
148 | * {@Inheritdoc} |
||
149 | */ |
||
150 | public function setTimeout($timeout) |
||
159 | |||
160 | /** |
||
161 | * {@Inheritdoc} |
||
162 | */ |
||
163 | public function setHttpBasicAuth($username, $passwd) |
||
172 | |||
173 | /** |
||
174 | * {@Inheritdoc} |
||
175 | */ |
||
176 | public function setHttpHeader($name, $value) |
||
185 | |||
186 | /** |
||
187 | * @return Server The next available server instance |
||
188 | */ |
||
189 | private function nextServer() |
||
195 | |||
196 | /** |
||
197 | * Very simple round-robin implementation |
||
198 | * Pops the first item of the availableServers array and appends it at the end. |
||
199 | * |
||
200 | * @return void |
||
201 | */ |
||
202 | private function roundRobin() |
||
212 | |||
213 | /** |
||
214 | * @param string $server |
||
215 | * @param ConnectException $exception |
||
216 | */ |
||
217 | private function dropServer($server, $exception) |
||
227 | |||
228 | } |
||
229 |