Completed
Push — master ( 76f2cb...2aa2ac )
by Alexander
07:56 queued 03:04
created

PdoErrorLogger   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setPath() 0 3 1
A update() 0 19 4
A getPath() 0 3 1
1
<?php
2
/**
3
 * Class for logging PDO errors. Subscriber for PDO-adapter
4
 *
5
 * @file      PdoErrorLogger.php
6
 *
7
 * PHP version 7.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2018 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
	private $path;
30
31
	/**
32
	 * Set log path
33
	 *
34
	 * @param string $path  Путь к логу
35
	 */
36 2
	public function setPath($path)
37
	{
38 2
		$this->path = $path;
39 2
	}
40
41
	/**
42
	 * Getting log path
43
	 *
44
	 * @return string
45
	 */
46 2
	public function getPath()
47
	{
48 2
		return $this->path;
49
	}
50
51
	/**
52
	 * Update subscriber
53
	 *
54
	 * @param SplSubject $subject
55
	 */
56 6
	public function update(SplSubject $subject)
57
	{
58
		/** @var \PDO $conn */
59
		/** @var DbAdapterInterface $subject */
60
		/** @var \PdoStatement $stmt */
61 6
		$conn     = $subject->getResource();
0 ignored issues
show
Bug introduced by
The method getResource() does not exist on SplSubject. It seems like you code against a sub-type of SplSubject such as Veles\Tests\DataBase\Loggers\FakeSubject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
		/** @scrutinizer ignore-call */ 
62
  $conn     = $subject->getResource();
Loading history...
62 6
		$stmt     = $subject->getStmt();
0 ignored issues
show
Bug introduced by
The method getStmt() does not exist on SplSubject. It seems like you code against a sub-type of SplSubject such as Veles\Tests\DataBase\Loggers\FakeSubject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
		/** @scrutinizer ignore-call */ 
63
  $stmt     = $subject->getStmt();
Loading history...
63 6
		$conn_err = $conn->errorCode();
64 6
		$stmt_err = $stmt->errorCode();
65
66 6
		if ('00000' === $conn_err && '00000' === $stmt_err) {
67 2
			return;
68
		}
69
70 4
		$error_info = ('00000' === $conn_err)
71 2
			? implode('; ', $stmt->errorInfo()) . PHP_EOL
72 4
			: implode('; ', $conn->errorInfo()) . PHP_EOL;
73
74 4
		error_log($error_info, 3, $this->getPath());
75 4
	}
76
}
77