Completed
Push — h/multiple-servers ( 562c3a )
by Christian
06:27
created

Server::setHttpBasicAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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
28
29
class Server implements ServerInterface {
30
31
    const PROTOCOL = 'http';
32
    const SQL_PATH = '/_sql';
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
        $uri = self::computeURI($uri);
46
        $this->client = new HttpClient(['base_url' => $uri] + $options);
47
    }
48
49
    public function setTimeout($timeout)
50
    {
51
        $this->client->setDefaultOption('timeout', (float) $timeout);
52
    }
53
54
    public function setHttpBasicAuth($username, $passwd)
55
    {
56
        $this->client->setDefaultOption('auth', [$username, $passwd]);
57
    }
58
59
    public function setHttpHeader($name, $value)
60
    {
61
        $this->client->setDefaultOption('headers/'.$name, $value);
62
    }
63
64
    public function doRequest(array $body)
65
    {
66
        return $this->client->post(null, ['json' => $body]);
67
    }
68
69
    public function getServerInfo()
70
    {
71
        // TODO: Implement getServerInfo() method.
72
        throw new UnsupportedException('Not yet implemented');
73
    }
74
75
    public function getServerVersion()
76
    {
77
        // TODO: Implement getServerVersion() method.
78
        throw new UnsupportedException('Not yet implemented');
79
    }
80
81
    /**
82
     * Compute a URI for usage with the HTTP client
83
     *
84
     * @param string $server A host:port string
85
     *
86
     * @return string An URI which can be used by the HTTP client
87
     */
88
    private static function computeURI($server)
89
    {
90
        return self::PROTOCOL .'://' . $server . self::SQL_PATH;
91
    }
92
}
93