QueryLoggers::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Kir\MySQL\QueryLogger;
4
5
use Throwable;
6
7
class QueryLoggers {
8
	/** @var QueryLogger[] */
9
	private array $queryLoggers = [];
10
11
	public function add(QueryLogger $queryLogger): void {
12
		$this->queryLoggers[] = $queryLogger;
13
	}
14
15
	/**
16
	 * @template T
17
	 * @param string $query
18
	 * @param callable(): T $fn
19
	 * @return T
0 ignored issues
show
Bug introduced by
The type Kir\MySQL\QueryLogger\T 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...
20
	 */
21
	public function logRegion(string $query, $fn) {
22
		$exception = null;
23
		$timer = microtime(true);
24
		try {
25
			return $fn();
26
		} catch(Throwable $e) {
27
			$exception = $e;
28
			throw $e;
29
		} finally {
30
			$finalTimer = microtime(true) - $timer;
31
			if($exception === null) {
32
				$this->log($query, $finalTimer);
33
			} else {
34
				$this->logError($query, $exception, $finalTimer);
35
			}
36
		}
37
	}
38
39
	/**
40
	 * @param string $query
41
	 * @param float $duration
42
	 */
43
	public function log(string $query, float $duration): void {
44
		foreach($this->queryLoggers as $queryLogger) {
45
			try {
46
				$queryLogger->log($query, $duration);
47
			} catch(Throwable) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
48
			}
49
		}
50
	}
51
52
	/**
53
	 * @param string $query
54
	 * @param Throwable $exception
55
	 * @param float $duration
56
	 */
57
	public function logError(string $query, Throwable $exception, float $duration): void {
58
		foreach($this->queryLoggers as $queryLogger) {
59
			try {
60
				$queryLogger->logError($query, $exception, $duration);
61
			} catch(Throwable) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
62
			}
63
		}
64
	}
65
}
66