Reader::query()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
namespace InfluxDB\Adapter\Http;
3
4
use InfluxDB\Adapter\Http\Options;
5
use GuzzleHttp\Client;
6
use InfluxDB\Adapter\QueryableInterface;
7
8
class Reader implements QueryableInterface
9
{
10
    private $httpClient;
11
    private $options;
12
13 15
    public function __construct(Client $httpClient, Options $options)
14
    {
15 15
        $this->httpClient = $httpClient;
16 15
        $this->options = $options;
17 15
    }
18
19 15
    public function getOptions()
20
    {
21 15
        return $this->options;
22
    }
23
24 12
    public function query($query)
25
    {
26
        $options = [
27 12
            "auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()],
28
            'query' => [
29 12
                "q" => $query,
30 12
                "db" => $this->getOptions()->getDatabase(),
31
            ]
32
        ];
33
34 12
        return $this->get($options);
35
    }
36
37 12
    private function get(array $httpMessage)
38
    {
39 12
        $endpoint = $this->getHttpQueryEndpoint();
40 12
        return json_decode($this->httpClient->get($endpoint, $httpMessage)->getBody(), true);
41
    }
42
43 15
    protected function getHttpQueryEndpoint()
44
    {
45 15
        return $this->getHttpEndpoint("query");
46
    }
47
48 15
    private function getHttpEndpoint($operation)
49
    {
50 15
        $url = sprintf(
51 15
            "%s://%s:%d%s/%s",
52 15
            $this->getOptions()->getProtocol(),
53 15
            $this->getOptions()->getHost(),
54 15
            $this->getOptions()->getPort(),
55 15
            $this->getOptions()->getPrefix(),
56 15
            $operation
57
        );
58
59 15
        return $url;
60
    }
61
}
62