Passed
Push — master ( b953c9...de8e3d )
by Ron
02:53
created

QueryLoggers::logRegion()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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