Writer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 52
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOptions() 0 4 1
A getHttpSeriesEndpoint() 0 4 1
A getHttpEndpoint() 0 13 1
A send() 0 14 1
1
<?php
2
namespace InfluxDB\Adapter\Http;
3
4
use GuzzleHttp\Client;
5
use InfluxDB\Adapter\WriterTrait;
6
use InfluxDB\Adapter\Http\Options;
7
use InfluxDB\Adapter\WritableInterface;
8
9
class Writer implements WritableInterface
10
{
11
    use WriterTrait;
12
13
    private $httpClient;
14
    private $options;
15
16 27
    public function __construct(Client $httpClient, Options $options)
17
    {
18 27
        $this->httpClient = $httpClient;
19 27
        $this->options = $options;
20 27
    }
21
22 19
    public function getOptions()
23
    {
24 19
        return $this->options;
25
    }
26
27 16
    public function send(array $message)
28
    {
29
        $httpMessage = [
30 16
            "auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()],
31
            'query' => [
32 16
                "db" => $this->getOptions()->getDatabase(),
33 16
                "retentionPolicy" => $this->getOptions()->getRetentionPolicy(),
34
            ],
35 16
            "body" => $this->messageToLineProtocol($message, $this->getOptions()->getTags())
36
        ];
37
38 16
        $endpoint = $this->getHttpSeriesEndpoint();
39 16
        return $this->httpClient->post($endpoint, $httpMessage);
40
    }
41
42 19
    protected function getHttpSeriesEndpoint()
43
    {
44 19
        return $this->getHttpEndpoint("write");
45
    }
46
47 19
    private function getHttpEndpoint($operation)
48
    {
49 19
        $url = sprintf(
50 19
            "%s://%s:%d%s/%s",
51 19
            $this->getOptions()->getProtocol(),
52 19
            $this->getOptions()->getHost(),
53 19
            $this->getOptions()->getPort(),
54 19
            $this->getOptions()->getPrefix(),
55 19
            $operation
56
        );
57
58 19
        return $url;
59
    }
60
}
61