DoctrineDBALHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Norsys\LogsBundle\Handler;
4
5
use Monolog\Handler\AbstractProcessingHandler;
6
use Monolog\Logger;
7
use Monolog\Processor\WebProcessor;
8
9
use Doctrine\DBAL\Connection;
10
11
use Norsys\LogsBundle\Processor\WebExtendedProcessor;
12
use Norsys\LogsBundle\Formatter\NormalizerFormatter;
13
14
/**
15
 * Handler to send messages to a database through Doctrine DBAL.
16
 */
17
class DoctrineDBALHandler extends AbstractProcessingHandler
18
{
19
    /**
20
     * @var Connection $connection
21
     */
22
    private $connection;
23
24
    /**
25
     * @var string $tableName
26
     */
27
    private $tableName;
28
29
    /**
30
     * @param Connection $connection
31
     * @param string     $tableName
32
     * @param integer    $level
33
     * @param boolean    $bubble
34
     */
35
    public function __construct(
36
        Connection $connection,
37
        string $tableName,
38
        int $level = Logger::DEBUG,
39
        bool $bubble = true
40
    ) {
41 1
        $this->connection = $connection;
42 1
        $this->tableName  = $tableName;
43
44 1
        parent::__construct($level, $bubble);
45
46 1
        $this->pushProcessor(new WebProcessor());
47 1
        $this->pushProcessor(new WebExtendedProcessor());
48 1
    }
49
50
    /**
51
     * @param array $record
52
     */
53
    protected function write(array $record)
54
    {
55 1
        $record = $record['formatted'];
56
57
        try {
58 1
            $this->connection->insert($this->tableName, $record);
59
        } catch (\Exception $e) {
60
            // Not fatal error on bad error
61
        }
62 1
    }
63
64
    /**
65
     * @return NormalizerFormatter
66
     */
67
    protected function getDefaultFormatter()
68
    {
69 1
        return new NormalizerFormatter();
70
    }
71
}
72