Issues (85)

src/QueryLogger/QueryLoggers.php (3 issues)

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