TelegrafHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 9
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultFormatter() 0 3 1
A write() 0 4 2
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\TableSync\Monolog\Handler;
6
7
use InfluxDB\Driver\UDP;
8
use Monolog\Formatter\FormatterInterface;
9
use Monolog\Handler\AbstractProcessingHandler;
10
use Monolog\Logger;
11
use Umbrellio\TableSync\Monolog\Formatter\InfluxDBFormatter;
12
13
class TelegrafHandler extends AbstractProcessingHandler
14
{
15
    private $measurement;
16
    private $socket;
17
18
    public function __construct($host, $port, ?string $measurement, $level = Logger::INFO, $bubble = true)
19
    {
20
        parent::__construct($level, $bubble);
21
22
        $this->measurement = $measurement ?? InfluxDBHandler::POINT_MEASUREMENT;
23
        $this->socket = new UDP($host, $port);
24
    }
25
26
    protected function write(array $record): void
27
    {
28
        foreach ($record['formatted'] as $record) {
29
            $this->socket->write((string) $record);
30
        }
31
    }
32
33
    protected function getDefaultFormatter(): FormatterInterface
34
    {
35
        return new InfluxDBFormatter($this->measurement);
36
    }
37
}
38