PsrSqlLogger::stopQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Abacaphiliac\Doctrine;
4
5
use Doctrine\DBAL\Logging\SQLLogger;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LogLevel;
8
9
class PsrSqlLogger implements SQLLogger
10
{
11
    /** @var callable */
12
    private $logger;
13
14
    /** @var float */
15
    private $start;
16
17
    /** @var string */
18
    private $queryId;
19
20
    /**
21
     * PsrSqlLogger constructor.
22
     * @param LoggerInterface $logger
23
     * @param string $level
24
     */
25 6
    public function __construct(LoggerInterface $logger, $level = LogLevel::INFO)
26
    {
27 6
        $callable = [$logger, $level];
28
29 6
        if (!\is_callable($callable)) {
30 1
            throw new \InvalidArgumentException(sprintf(
31 1
                '%s::%s" is not callable',
32 1
                LoggerInterface::class,
33
                $level
34
            ));
35
        }
36
37 6
        $this->logger = $callable;
38 6
    }
39
40 5
    public function startQuery($sql, array $params = null, array $types = null)
41
    {
42 5
        $this->queryId = \uniqid('', true);
43
44 5
        $this->start = \microtime(true);
45
46 5
        \call_user_func($this->logger, 'Query started', \array_merge(
47 5
            $this->getStartQueryContext($sql, $params, $types),
48
            [
49 5
                'query_id' => $this->queryId,
50
            ]
51
        ));
52 5
    }
53
54 5
    protected function getStartQueryContext($sql, array $params = null, array $types = null)
55
    {
56
        return [
57 5
            'sql' => $sql,
58 5
            'types' => $types,
59
        ];
60
    }
61
62 3
    public function stopQuery()
63
    {
64 3
        $stop = \microtime(true);
65
66 3
        \call_user_func($this->logger, 'Query finished', [
67 3
            'query_id' => $this->queryId,
68 3
            'start' => $this->start,
69 3
            'stop' => $stop,
70 3
            'duration_s' => $stop - $this->start,
71
        ]);
72 3
    }
73
}
74