Completed
Pull Request — master (#34)
by Christian
16:03 queued 12:30
created

Server::getServerVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
crap 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 Crate\PDO\Exception\UnsupportedException;
26
use GuzzleHttp\Client as HttpClient;
27
use GuzzleHttp\Exception\RequestException;
28
use GuzzleHttp\Message\ResponseInterface;
29
30
31
class Server implements InternalClientInterface {
32
33
    const PROTOCOL = 'http';
34
    const SQL_PATH = '/_sql';
35
36
    /**
37
     * @var HttpClient
38
     */
39
    private $client;
40
41
    /**
42
     * @param string $uri
43
     * @param array  $options
44
     */
45
    public function __construct($uri, array $options)
46
    {
47
        $uri = self::computeURI($uri);
48
        $this->client = new HttpClient(['base_url' => $uri] + $options);
49
    }
50
51 1
    public function setTimeout($timeout)
52
    {
53 1
        $this->client->setDefaultOption('timeout', (float) $timeout);
54 1
    }
55
56
    public function setHttpBasicAuth($username, $passwd)
57
    {
58
        $this->client->setDefaultOption('auth', [$username, $passwd]);
59
    }
60
61 1
    public function setHttpHeader($name, $value)
62
    {
63 1
        $this->client->setDefaultOption('headers/'.$name, $value);
64 1
    }
65
66 1
    public function getServerInfo()
67
    {
68
        // TODO: Implement getServerInfo() method.
69 1
        throw new UnsupportedException('Not yet implemented');
70
    }
71
72 1
    public function getServerVersion()
73
    {
74
        // TODO: Implement getServerVersion() method.
75 1
        throw new UnsupportedException('Not yet implemented');
76
    }
77
78
    /**
79
     * Execute a HTTP/1.1 POST request with JSON body
80
     *
81
     * @param array $body
82
83
     * @return ResponseInterface
84
     * @throws RequestException When an error is encountered
85
     */
86
    public function doRequest(array $body)
87
    {
88
        return $this->client->post(null, ['json' => $body]);
89
    }
90
91
    /**
92
     * Compute a URI for usage with the HTTP client
93
     *
94
     * @param string $server A host:port string
95
     *
96
     * @return string An URI which can be used by the HTTP client
97
     */
98 4
    private static function computeURI($server)
99
    {
100 4
        return self::PROTOCOL .'://' . $server . self::SQL_PATH;
101
    }
102
}
103