InfluxDBHandler::setFormatter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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