Completed
Push — b/update_guzzle ( c7c71f...884993 )
by
unknown
06:21 queued 01:24
created

Server   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 81
ccs 12
cts 24
cp 0.5
rs 10

8 Methods

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