DbalLogger   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B startQuery() 0 16 5
A fixParams() 0 8 2
C fixParam() 0 24 7
A stopQuery() 0 8 2
A log() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Saxulum\SaxulumWebProfiler\Logger;
13
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\Stopwatch\Stopwatch;
16
use Doctrine\DBAL\Logging\DebugStack;
17
18
/**
19
 * DbalLogger.
20
 *
21
 * @author Fabien Potencier <[email protected]>
22
 */
23
class DbalLogger extends DebugStack
24
{
25
    const MAX_STRING_LENGTH = 32;
26
    const BINARY_DATA_VALUE = '(binary value)';
27
28
    protected $logger;
29
    protected $stopwatch;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param LoggerInterface $logger    A LoggerInterface instance
35
     * @param Stopwatch       $stopwatch A Stopwatch instance
36
     */
37
    public function __construct(LoggerInterface $logger = null, Stopwatch $stopwatch = null)
38
    {
39
        $this->logger = $logger;
40
        $this->stopwatch = $stopwatch;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function startQuery($sql, array $params = null, array $types = null)
47
    {
48
        parent::startQuery($sql, $params, $types);
49
50
        if (null !== $this->stopwatch) {
51
            $this->stopwatch->start('doctrine', 'doctrine');
52
        }
53
54
        if (is_array($params)) {
55
            $params = $this->fixParams($params);
56
        }
57
58
        if (null !== $this->logger) {
59
            $this->log($sql, null === $params ? array() : $params);
60
        }
61
    }
62
63
    /**
64
     * @param  array $params
65
     * @return array
66
     */
67
    protected function fixParams(array $params)
68
    {
69
        foreach ($params as $index => $param) {
70
            $params[$index] = $this->fixParam($param);
71
        }
72
73
        return $params;
74
    }
75
76
    /**
77
     * @param  mixed $param
78
     * @return mixed
79
     */
80
    protected function fixParam($param)
81
    {
82
        if (!is_string($param)) {
83
            return $param;
84
        }
85
86
        // non utf-8 strings break json encoding
87
        if (!preg_match('#[\p{L}\p{N} ]#u', $param)) {
88
            return self::BINARY_DATA_VALUE;
89
        }
90
91
        // detect if the too long string must be shorten
92
        if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($param)) {
93
            if (self::MAX_STRING_LENGTH < mb_strlen($param, $encoding)) {
94
                return mb_substr($param, 0, self::MAX_STRING_LENGTH - 6, $encoding).' [...]';
95
            }
96
        } else {
97
            if (self::MAX_STRING_LENGTH < strlen($param)) {
98
                return substr($param, 0, self::MAX_STRING_LENGTH - 6).' [...]';
99
            }
100
        }
101
102
        return $param;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function stopQuery()
109
    {
110
        parent::stopQuery();
111
112
        if (null !== $this->stopwatch) {
113
            $this->stopwatch->stop('doctrine');
114
        }
115
    }
116
117
    /**
118
     * Logs a message.
119
     *
120
     * @param string $message A message to log
121
     * @param array  $params  The context
122
     */
123
    protected function log($message, array $params)
124
    {
125
        $this->logger->debug($message, $params);
126
    }
127
}
128