Completed
Push — master ( f183e1...4cf3c5 )
by Jitendra
14s queued 11s
created

Logger::getParsedSql()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace PhalconExt\Db;
4
5
use Phalcon\Db\Adapter;
0 ignored issues
show
Bug introduced by
The type Phalcon\Db\Adapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Phalcon\Events\Event;
0 ignored issues
show
Bug introduced by
The type Phalcon\Events\Event was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use PhalconExt\Logger\LogsToFile;
8
9
/**
10
 * SQL logger implemented as db event listener.
11
 *
12
 * @author  Jitendra Adhikari <[email protected]>
13
 * @license MIT
14
 *
15
 * @link    https://github.com/adhocore/phalcon-ext
16
 */
17
class Logger
18
{
19
    use LogsToFile;
20
21
    /** @var array */
22
    protected $config = [];
23
24
    /** @var string */
25
    protected $logFormat = "%message%;\n";
26
27
    /** @var string */
28
    protected $fileExtension = '.sql';
29
30
    public function __construct(array $config)
31
    {
32
        $this->config = $config + ['skipFirst' => 0, 'addHeader' => false, 'backtraceLevel' => 0];
33
34
        if (!empty($this->config['enabled']) && !empty($this->config['logPath'])) {
35
            $this->activate($this->config['logPath']);
36
        }
37
    }
38
39
    /**
40
     * Run before firing query.
41
     *
42
     * @param Event   $event
43
     * @param Adapter $connection
44
     *
45
     * @return void
46
     */
47
    public function beforeQuery(Event $event, Adapter $connection)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
    public function beforeQuery(/** @scrutinizer ignore-unused */ Event $event, Adapter $connection)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        if (!$this->activated) {
50
            return;
51
        }
52
53
        static $index = -1;
54
55
        if (++$index < $this->config['skipFirst']) {
56
            return;
57
        }
58
59
        $this->log($this->getHeader($index) . $this->getBacktrace() . $this->getParsedSql($connection));
60
    }
61
62
    /**
63
     * Get log header like request uri and datetime.
64
     *
65
     * @param int $index The current position count of query.
66
     *
67
     * @return string
68
     */
69
    protected function getHeader(int $index): string
70
    {
71
        if ($index !== $this->config['skipFirst'] || !$this->config['addHeader']) {
72
            return '';
73
        }
74
75
        $header = '-- ' . ($_SERVER['REQUEST_URI'] ?? '') . \date(' [Y-m-d H:i:s]') . "\n";
76
77
        return $header
78
            . \str_pad('-- -', \strlen($header), '-', STR_PAD_RIGHT)
79
            . "\n";
80
    }
81
82
    /**
83
     * Get the backtrace of paths leading to logged query.
84
     *
85
     * @return string
86
     */
87
    protected function getBacktrace(): string
88
    {
89
        if ($this->config['backtraceLevel'] < 1) {
90
            return '';
91
        }
92
93
        $trace = '';
94
        foreach (\array_slice(\debug_backtrace(), 2, $this->config['backtraceLevel']) as $tr) {
95
            $tr += ['function' => 'n/a', 'line' => 0, 'file' => 'n/a'];
96
97
            $trace .= "  -- {$tr['file']}:{$tr['line']}#{$tr['function']}()\n";
98
        }
99
100
        return $trace;
101
    }
102
103
    /**
104
     * Get the properly interpolated sql.
105
     *
106
     * @param Adapter $connection
107
     *
108
     * @return string
109
     */
110
    protected function getParsedSql($connection): string
111
    {
112
        $parts = $connection->convertBoundParams(
113
            $connection->getSqlStatement(),
114
            $connection->getSQLVariables() ?: []
115
        );
116
117
        $binds = $parts['params'] ?: $connection->getSQLVariables();
118
119
        return \vsprintf(\str_replace('?', "'%s'", $parts['sql']), $binds);
120
    }
121
122
    /**
123
     * Run after firing query.
124
     *
125
     * @param Event   $event
126
     * @param Adapter $connection
127
     *
128
     * @return void
129
     */
130
    public function afterQuery(Event $event, $connection)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

130
    public function afterQuery(/** @scrutinizer ignore-unused */ Event $event, $connection)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $connection is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

130
    public function afterQuery(Event $event, /** @scrutinizer ignore-unused */ $connection)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        // Do nothing.
133
    }
134
}
135