Completed
Push — 1.x ( 0b668e...1b7ebe )
by Yuu
01:46
created

PsrSqlLogger::startQuery()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 9.4285
cc 3
eloc 12
nc 4
nop 3
crap 3
1
<?php
2
/**
3
 * This file is part of the Ray.DoctrineOrmModule package
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\DoctrineOrmModule\Logger;
8
9
use Doctrine\DBAL\Logging\SQLLogger;
10
use Psr\Log\LoggerInterface;
11
12
class PsrSqlLogger implements SQLLogger
13
{
14
    /**
15
     * @var LoggerInterface
16
     */
17
    private $logger;
18
19
    /**
20
     * @var string
21
     */
22
    private $sql;
23
24
    /**
25
     * @var array
26
     */
27
    private $params;
28
29
    /**
30
     * @var array
31
     */
32
    private $types;
33
34
    /**
35
     * @var float
36
     */
37
    private $start;
38
39
    /**
40
     * constructor.
41
     *
42
     * @param LoggerInterface $logger
43
     */
44 5
    public function __construct(LoggerInterface $logger)
45
    {
46 5
        $this->logger = $logger;
47 5
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function startQuery($sql, array $params = null, array $types = null)
53
    {
54 1
        $sql = trim($sql, '"');
55 1
        $params = $params ?: [];
56 1
        $types = $types ?: [];
57
58 1
        $this->logger->debug('start  query', [
59 1
            'sql' => $sql,
60 1
            'params' => $params,
61
            'types' => $types
62 1
        ]);
63
64 1
        $this->sql = $sql;
65 1
        $this->params = $params;
66 1
        $this->types = $types;
67
68 1
        $this->start = microtime(true);
69 1
    }
70
71
    /**
72
     * [@inheritdoc}
73
     */
74 1
    public function stopQuery()
75
    {
76 1
        $time = round(microtime(true) - $this->start, 3);
77
78 1
        $this->logger->debug('finish query', [
79 1
            'sql' => $this->sql,
80 1
            'params' => $this->params,
81 1
            'types' => $this->types,
82
            'time' => $time
83 1
        ]);
84 1
    }
85
}
86