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 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | private $serverPool = []; |
||
47 | |||
48 | /** |
||
49 | * Client constructor. |
||
50 | * @param array $servers |
||
51 | * @param array $options |
||
52 | */ |
||
53 | 1 | public function __construct(array $servers, array $options) |
|
54 | { |
||
55 | 1 | if ($servers == null || count($servers) == 0) { |
|
56 | $this->serverPool[self::DEFAULT_SERVER] = new Server(self::DEFAULT_SERVER, $options); |
||
57 | $this->availableServers[] = self::DEFAULT_SERVER; |
||
58 | } else { |
||
59 | 1 | foreach ($servers as &$server) { |
|
60 | 1 | $this->serverPool[$server] = new Server($server, $options); |
|
61 | 1 | $this->availableServers[] = $server; |
|
62 | 1 | } |
|
63 | } |
||
64 | 1 | } |
|
65 | |||
66 | /** |
||
67 | * {@Inheritdoc} |
||
68 | */ |
||
69 | 2 | public function execute($query, array $parameters) |
|
70 | { |
||
71 | $body = [ |
||
72 | 2 | 'stmt' => $query, |
|
73 | 'args' => $parameters |
||
74 | 2 | ]; |
|
75 | |||
76 | 2 | while (true) { |
|
77 | 2 | $nextServer = $this->nextServer(); |
|
78 | /** |
||
79 | * @var Server $s |
||
80 | */ |
||
81 | 2 | $s = $this->serverPool[$nextServer]; |
|
82 | |||
83 | try { |
||
84 | |||
85 | 2 | $response = $s->doRequest($body); |
|
86 | 1 | $responseBody = $response->json(); |
|
87 | |||
88 | 1 | return new Collection( |
|
89 | 1 | $responseBody['rows'], |
|
90 | 1 | $responseBody['cols'], |
|
91 | 1 | $responseBody['duration'], |
|
92 | 1 | $responseBody['rowcount'] |
|
93 | 1 | ); |
|
94 | |||
95 | 1 | } catch (ConnectException $exception) { |
|
96 | // drop the server from the list of available servers |
||
97 | $this->dropServer($nextServer); |
||
98 | // break the loop if no more servers are available |
||
99 | $this->raiseIfNoMoreServers($exception); |
||
100 | 1 | } catch (BadResponseException $exception) { |
|
101 | |||
102 | try { |
||
103 | |||
104 | 1 | $json = $exception->getResponse()->json(); |
|
105 | |||
106 | 1 | $errorCode = $json['error']['code']; |
|
107 | 1 | $errorMessage = $json['error']['message']; |
|
108 | |||
109 | 1 | throw new RuntimeException($errorMessage, $errorCode, $exception); |
|
110 | |||
111 | 1 | } catch (ParseException $e) { |
|
112 | throw new RuntimeException('Server returned non-JSON response.', 0, $exception); |
||
113 | } |
||
114 | |||
115 | } |
||
116 | } |
||
117 | return null; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * {@Inheritdoc} |
||
122 | */ |
||
123 | public function getServerInfo() |
||
127 | |||
128 | /** |
||
129 | * {@Inheritdoc} |
||
130 | */ |
||
131 | public function getServerVersion() |
||
135 | |||
136 | /** |
||
137 | * {@Inheritdoc} |
||
138 | */ |
||
139 | public function setTimeout($timeout) |
||
148 | |||
149 | /** |
||
150 | * {@Inheritdoc} |
||
151 | */ |
||
152 | public function setHttpBasicAuth($username, $passwd) |
||
161 | |||
162 | /** |
||
163 | * {@Inheritdoc} |
||
164 | */ |
||
165 | public function setHttpHeader($name, $value) |
||
174 | |||
175 | /** |
||
176 | * @return string The next available server instance |
||
177 | */ |
||
178 | 3 | private function nextServer() |
|
184 | |||
185 | /** |
||
186 | * Very simple round-robin implementation |
||
187 | * Pops the first item of the availableServers array and appends it at the end. |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | 3 | private function roundRobin() |
|
201 | |||
202 | /** |
||
203 | * @param string $server |
||
204 | */ |
||
205 | 2 | private function dropServer($server) |
|
211 | |||
212 | /** |
||
213 | * @param ConnectException $exception |
||
214 | */ |
||
215 | 1 | private function raiseIfNoMoreServers($exception) |
|
222 | |||
223 | } |
||
224 |