1 | <?php |
||
32 | class Client implements ClientInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var HttpClient |
||
36 | */ |
||
37 | private $client; |
||
38 | |||
39 | /** |
||
40 | * @param string $uri |
||
41 | * @param array $options |
||
42 | */ |
||
43 | 1 | public function __construct($uri, array $options) |
|
44 | { |
||
45 | 1 | $this->client = new HttpClient(['base_url' => $uri] + $options); |
|
46 | 1 | } |
|
47 | |||
48 | /** |
||
49 | * {@Inheritdoc} |
||
50 | */ |
||
51 | 2 | public function execute($query, array $parameters) |
|
52 | { |
||
53 | $body = [ |
||
54 | 2 | 'stmt' => $query, |
|
55 | 2 | 'args' => $parameters |
|
56 | ]; |
||
57 | |||
58 | try { |
||
59 | 2 | $response = $this->client->post(null, ['json' => $body]); |
|
60 | 1 | $responseBody = $response->json(); |
|
61 | |||
62 | 1 | return new Collection( |
|
63 | 1 | $responseBody['rows'], |
|
64 | 1 | $responseBody['cols'], |
|
65 | 1 | $responseBody['duration'], |
|
66 | 1 | $responseBody['rowcount'] |
|
67 | ); |
||
68 | |||
69 | 1 | } catch (BadResponseException $exception) { |
|
70 | |||
71 | try { |
||
72 | |||
73 | 1 | $json = $exception->getResponse()->json(); |
|
74 | |||
75 | 1 | $errorCode = $json['error']['code']; |
|
76 | 1 | $errorMessage = $json['error']['message']; |
|
77 | |||
78 | 1 | throw new RuntimeException($errorMessage, $errorCode); |
|
79 | |||
80 | 1 | } catch (ParseException $e) { |
|
81 | throw new RuntimeException('Unparsable response from server', 0, $exception); |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@Inheritdoc} |
||
88 | */ |
||
89 | 1 | public function getServerInfo() |
|
90 | { |
||
91 | 1 | throw new UnsupportedException('Not yet implemented'); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@Inheritdoc} |
||
96 | */ |
||
97 | 1 | public function getServerVersion() |
|
101 | |||
102 | /** |
||
103 | * {@Inheritdoc} |
||
104 | */ |
||
105 | 1 | public function setTimeout($timeout) |
|
109 | |||
110 | /** |
||
111 | * {@Inheritdoc} |
||
112 | */ |
||
113 | public function setHttpBasicAuth($username, $passwd) |
||
117 | |||
118 | /** |
||
119 | * {@Inheritdoc} |
||
120 | */ |
||
121 | 1 | public function setHttpHeader($name, $value) |
|
125 | } |
||
126 |