FileLogger   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A startQuery() 0 4 1
A stopQuery() 0 11 1
A __construct() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace Rostenkowski\Doctrine\Logger;
4
5
6
use Doctrine\DBAL\Logging\SQLLogger;
7
8
class FileLogger implements SQLLogger
9
{
10
11
	/**
12
	 * @var string
13
	 */
14
	private $file;
15
16
	/**
17
	 * @var float
18
	 */
19
	private $start;
20
21
	/**
22
	 * @var int
23
	 */
24
	private $current;
25
26
	/**
27
	 * @var array
28
	 */
29
	private $queries = [];
30
31
32
	public function __construct(string $file)
33
	{
34
		$this->file = $file;
35
36
		register_shutdown_function(function () {
37
			file_put_contents($this->file, str_repeat('-', 100) . PHP_EOL . PHP_EOL, FILE_APPEND);
38
		});
39
	}
40
41
42
	public function startQuery($sql, array $params = NULL, array $types = NULL)
43
	{
44
		$this->start = (float) microtime(true);
45
		$this->queries[++$this->current] = ['sql' => $sql, 'params' => $params, 'types' => $types, 'dur' => 0];
46
	}
47
48
49
	public function stopQuery()
50
	{
51
		$q = $this->queries[$this->current];
52
		$q['dur'] = (float) microtime(true) - $this->start;
53
		$log = sprintf("%s \n\n%s \n\n%s ms \n\n%s \n\n",
54
			"#$this->current:",
55
			$q['sql'],
56
			round($q['dur'] * 1000, 3),
57
			var_export($q['params'], true)
58
		);
59
		file_put_contents($this->file, $log, FILE_APPEND);
60
	}
61
62
}
63