Writer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 38
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOptions() 0 4 1
A send() 0 6 1
A write() 0 15 1
1
<?php
2
namespace InfluxDB\Adapter\Udp;
3
4
use InfluxDB\Adapter\WriterTrait;
5
use InfluxDB\Adapter\Udp\Options;
6
use InfluxDB\Adapter\WritableInterface;
7
8
class Writer implements WritableInterface
9
{
10
    use WriterTrait;
11
12
    private $options;
13
14 18
    public function __construct(Options $options)
15
    {
16 18
        $this->options = $options;
17 18
    }
18
19 18
    public function getOptions()
20
    {
21 18
        return $this->options;
22
    }
23 17
    public function send(array $message)
24
    {
25 17
        $message = $this->messageToLineProtocol($message, $this->getOptions()->getTags());
26
27 17
        $this->write($message);
28 17
    }
29
30
    public function write($message)
31
    {
32
        // Create a handler in order to handle the 'Host is down' message
33 5
        set_error_handler(function() {
34
            // Suppress the error, this is the UDP adapter and if we can't send
35
            // it then we shouldn't inturrupt their application.
36 5
        });
37
38 5
        $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
39 5
        socket_sendto($socket, $message, strlen($message), 0, $this->getOptions()->getHost(), $this->getOptions()->getPort());
40 5
        socket_close($socket);
41
42
        // Remove our error handler.
43 5
        restore_error_handler();
44 5
    }
45
}
46