PdoErrorLogger   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 65
ccs 20
cts 20
cp 1
rs 10
c 1
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setPath() 0 3 1
A getPath() 0 3 1
A updateConnParams() 0 9 2
A update() 0 15 4
1
<?php
2
/**
3
 * Class for logging PDO errors. Subscriber for PDO-adapter
4
 *
5
 * @file      PdoErrorLogger.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      2013-12-31 15:44
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\DataBase\Loggers;
17
18
use SplSubject;
19
use Veles\DataBase\Adapters\DbAdapterInterface;
20
21
/**
22
 * Class PdoErrorLogger
23
 *
24
 * @author  Alexander Yancharuk <alex at itvault dot info>
25
 */
26
class PdoErrorLogger implements \SplObserver
27
{
28
	/** @var  string */
29
	protected $path;
30
	/** @var \PDO */
31
	protected $conn;
32
	/** @var \PDOStatement */
33
	protected $stmt;
34
35
	/**
36
	 * Set log path
37
	 *
38
	 * @param string $path
39
	 */
40 5
	public function setPath($path)
41
	{
42 5
		$this->path = $path;
43
	}
44
45
	/**
46
	 * Getting log path
47
	 *
48
	 * @return string
49
	 */
50 3
	public function getPath()
51
	{
52 3
		return $this->path;
53
	}
54
55
	/**
56
	 * Update subscriber
57
	 *
58
	 * @param SplSubject $subject
59
	 */
60 4
	public function update(SplSubject $subject)
61
	{
62 4
		$this->updateConnParams($subject);
63 3
		$conn_err = $this->conn->errorCode();
64 3
		$stmt_err = $this->stmt->errorCode();
65
66 3
		if ('00000' === $conn_err && '00000' === $stmt_err) {
67 1
			return;
68
		}
69
70 2
		$error_info = ('00000' === $conn_err)
71 1
			? implode('; ', $this->stmt->errorInfo()) . PHP_EOL
72 2
			: implode('; ', $this->conn->errorInfo()) . PHP_EOL;
73
74 2
		error_log($error_info, 3, $this->getPath());
75
	}
76
77
	/**
78
	 * Set connection and statement properties
79
	 *
80
	 * @param SplSubject $subject
81
	 */
82 4
	protected function updateConnParams(SplSubject $subject)
83
	{
84 4
		if (!$subject instanceof DbAdapterInterface) {
85 1
			$msg = '$subject must be instance of InvalidArgumentException';
86 1
			throw new \InvalidArgumentException($msg);
87
		}
88
89 3
		$this->conn = $subject->getResource();
90 3
		$this->stmt = $subject->getStmt();
91
	}
92
}
93