Completed
Push — m/dsn-default-schema ( 74c43f )
by
unknown
64:15 queued 54:09
created

Client   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
c 4
b 0
f 0
lcom 1
cbo 6
dl 0
loc 99
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B execute() 0 34 3
A getServerInfo() 0 4 1
A getServerVersion() 0 4 1
A setTimeout() 0 4 1
A setHttpBasicAuth() 0 4 1
A setHttpHeader() 0 4 1
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
23
namespace Crate\PDO\Http;
24
25
use GuzzleHttp\Client as HttpClient;
26
use Crate\PDO\Exception\RuntimeException;
27
use Crate\PDO\Exception\UnsupportedException;
28
use Crate\Stdlib\Collection;
29
use GuzzleHttp\Exception\BadResponseException;
30
use GuzzleHttp\Exception\ParseException;
31
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
    public function __construct($uri, array $options)
44
    {
45
        $this->client = new HttpClient(['base_url' => $uri] + $options);
46
    }
47
48
    /**
49
     * {@Inheritdoc}
50
     */
51
    public function execute($query, array $parameters)
52
    {
53
        $body = [
54
            'stmt' => $query,
55
            'args' => $parameters
56
        ];
57
58
        try {
59
            $response     = $this->client->post(null, ['json' => $body]);
60
            $responseBody = $response->json();
61
62
            return new Collection(
63
                $responseBody['rows'],
64
                $responseBody['cols'],
65
                $responseBody['duration'],
66
                $responseBody['rowcount']
67
            );
68
69
        } catch (BadResponseException $exception) {
70
71
            try {
72
73
                $json = $exception->getResponse()->json();
74
75
                $errorCode    = $json['error']['code'];
76
                $errorMessage = $json['error']['message'];
77
78
                throw new RuntimeException($errorMessage, $errorCode);
79
80
            } catch (ParseException $e) {
81
                throw new RuntimeException('Unparsable response from server', 0, $exception);
82
            }
83
        }
84
    }
85
86
    /**
87
     * {@Inheritdoc}
88
     */
89
    public function getServerInfo()
90
    {
91
        throw new UnsupportedException('Not yet implemented');
92
    }
93
94
    /**
95
     * {@Inheritdoc}
96
     */
97
    public function getServerVersion()
98
    {
99
        throw new UnsupportedException('Not yet implemented');
100
    }
101
102
    /**
103
     * {@Inheritdoc}
104
     */
105
    public function setTimeout($timeout)
106
    {
107
        $this->client->setDefaultOption('timeout', (float) $timeout);
108
    }
109
110
    /**
111
     * {@Inheritdoc}
112
     */
113
    public function setHttpBasicAuth($username, $passwd)
114
    {
115
        $this->client->setDefaultOption('auth', [$username, $passwd]);
116
    }
117
118
    /**
119
     * Set HTTP header for client requests
120
     *
121
     * @param string       $name
122
     * @param string       $value
123
     *
124
     * @return void
125
     */
126
    public function setHttpHeader($name, $value)
127
    {
128
        $this->client->setDefaultOption('headers/'.$name, $value);
129
    }
130
}
131