Failed Conditions
Push — master ( 5e6761...398dce )
by Adrien
04:14 queued 01:57
created

ForwardSQLLogger::stopQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
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
nc 2
nop 0
dl 0
loc 6
cc 2
rs 10
ccs 0
cts 5
cp 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\Logging;
6
7
use Doctrine\DBAL\Logging\DebugStack;
8
use Doctrine\DBAL\Logging\SQLLogger;
9
10
/**
11
 * A SQL logger that forward logs to Laminas Log
12
 */
13
class ForwardSQLLogger extends DebugStack implements SQLLogger
14
{
15
    public function stopQuery(): void
16
    {
17
        if ($this->enabled) {
18
            parent::stopQuery();
19
20
            $this->forwardLog($this->queries[$this->currentQuery]);
21
        }
22
    }
23
24
    /**
25
     * Forward query to file logger
26
     *
27
     * @param array $query
28
     */
29
    private function forwardLog(array $query): void
30
    {
31
        $extra = [
32
            'params' => $query['params'],
33
            'time' => number_format($query['executionMS'], 6),
34
        ];
35
36
        // Here we cannot inject the logger via DI, or it would be created too early and
37
        // break unit tests by creating two parallel connection to DB and thus timeout
38
        // when a tests's transaction is pending but a log is trying to be written on the other connection
39
        _log()->debug($query['sql'], $extra);
40
    }
41
}
42