InfluxDBHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 36
ccs 0
cts 12
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultFormatter() 0 3 1
A __construct() 0 10 1
A setFormatter() 0 7 2
A write() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\TableSync\Monolog\Handler;
6
7
use InfluxDB\Database;
8
use InvalidArgumentException;
9
use Monolog\Formatter\FormatterInterface;
10
use Monolog\Handler\AbstractProcessingHandler;
11
use Monolog\Handler\HandlerInterface;
12
use Monolog\Logger;
13
use Umbrellio\TableSync\Monolog\Formatter\InfluxDBFormatter;
14
use Umbrellio\TableSync\Monolog\Formatter\TableSyncFormatter;
15
16
class InfluxDBHandler extends AbstractProcessingHandler
17
{
18
    public const POINT_MEASUREMENT = 'table_sync';
19
20
    private $database;
21
    private $measurement;
22
23
    public function __construct(
24
        Database $database,
25
        ?string $measurement,
26
        int $level = Logger::INFO,
27
        bool $bubble = true
28
    ) {
29
        parent::__construct($level, $bubble);
30
31
        $this->database = $database;
32
        $this->measurement = $measurement ?? self::POINT_MEASUREMENT;
33
    }
34
35
    public function setFormatter(FormatterInterface $formatter): HandlerInterface
36
    {
37
        if ($formatter instanceof TableSyncFormatter) {
38
            return parent::setFormatter($formatter);
39
        }
40
41
        throw new InvalidArgumentException('InfluxDBHandler is only compatible with TableSyncFormatter');
42
    }
43
44
    protected function write(array $record): void
45
    {
46
        $this->database->writePoints($record['formatted']);
47
    }
48
49
    protected function getDefaultFormatter(): FormatterInterface
50
    {
51
        return new InfluxDBFormatter($this->measurement);
52
    }
53
}
54