Completed
Pull Request — master (#74)
by
unknown
04:13
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace InfluxDB;
4
5
use InfluxDB\Adapter\WritableInterface as Writer;
6
use InfluxDB\Adapter\QueryableInterface as Reader;
7
8
/**
9
 * Client to manage request at InfluxDB
10
 */
11
class Client
12
{
13
    private $reader;
14
    private $writer;
15
16 15
    public function __construct(Reader $reader, Writer $writer)
17
    {
18 15
        $this->reader = $reader;
19 15
        $this->writer = $writer;
20 15
    }
21
22 12
    public function getReader()
23
    {
24 12
        return $this->reader;
25
    }
26
27 8
    public function getWriter()
28
    {
29 8
        return $this->writer;
30
    }
31
32 8
    public function mark($name, array $values = [])
33
    {
34 8
        $data = $name;
35 8
        if (!is_array($name)) {
36 5
            $data                             = [];
37 5
            $data['points'][0]['measurement'] = $name;
38 5
            $data['points'][0]['fields']      = $values;
39 5
        }
40
41 8
        if (isset($data['tags'])) {
42 2
            $tags = [];
43 2
            foreach ($data['tags'] as $key => $value) {
44 2
                $tags[$this->addSlashes($key)] = $this->addSlashes($value);
45 2
            }
46 2
            $data['tags'] = $tags;
47 2
        }
48
49 8
        if (isset($data['points'])) {
50 8
            $points = [];
51 8
            foreach ($data['points'] as $point) {
52 8
                $fields = [];
53 8
                foreach ($point['fields'] as $key => $value) {
54 8
                    $fields[$this->addSlashes($key)] = $value;
55 8
                }
56 8
                $point['fields'] = $fields;
57 8
                array_push($points, $point);
58 8
            }
59 8
            $data['points'] = $points;
60 8
        }
61
62 8
        return $this->getWriter()->send($data);
63
    }
64
65 12
    public function query($query)
66
    {
67 12
        return $this->getReader()->query($query);
68
    }
69
70
    /**
71
     * Returns strings with space, comma, or equals sign characters backslashed per Influx write protocol syntax
72
     *
73
     * @param string $value
74
     * @return string
75
     */
76 8
    private function addSlashes($value)
77
    {
78 8
        return str_replace([
79 8
            ' ',
80 8
            ',',
81
            '='
82 8
        ], [
83 8
            '\ ',
84 8
            '\,',
85
            '\='
86 8
        ], $value);
87
    }
88
}
89