FakePDO::getAvailableDrivers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Kir\FakePDO;
3
4
use PDO;
5
use Kir\FakePDO\Tools\MethodNameGenerator;
6
use Kir\FakePDO\EventHandlers\EventHandler;
7
use Kir\FakePDO\EventHandlers\EventHandlerTrait;
8
9
class FakePDO extends PDO {
10
	use EventHandlerTrait;
11
12
	/** @var array */
13
	private $attributes = [];
14
	/** @var bool */
15
	private $inTransaction = false;
16
	/** @var MethodNameGenerator */
17
	private $methodNameGeneratorA = null;
18
19
	/**
20
	 * @return array
21
	 */
22
	public static function getAvailableDrivers() {
23
		return PDO::getAvailableDrivers();
24
	}
25
26
	/**
27
	 * @param EventHandler $eventHandler
28
	 */
29
	public function __construct(EventHandler $eventHandler = null) {
30
		$this->methodNameGeneratorA = new MethodNameGenerator('PDO');
31
		$this->setEventHandler($eventHandler);
32
	}
33
34
	/**
35
	 * @param string $statement
36
	 * @param array|null $options
37
	 * @return FakePDOStatement
38
	 */
39
	public function prepare($statement, $options = NULL) {
40
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
41
		return $this->invokeEventHandler($methodName, array($statement, $options), function () {
42
			return new FakePDOStatement($this->getEventHandler());
43
		});
44
	}
45
46
	/**
47
	 * @return bool
48
	 */
49
	public function beginTransaction() {
50
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
51
		return $this->invokeEventHandler($methodName, array(), function () {
52
			$inTransaction = $this->inTransaction();
53
			if($inTransaction) {
54
				return false;
55
			} else {
56
				$this->inTransaction = true;
57
				return true;
58
			}
59
		});
60
	}
61
62
	/**
63
	 * @return bool
64
	 */
65
	public function commit() {
66
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
67
		return $this->invokeEventHandler($methodName, array(), function () {
68
			$inTransaction = $this->inTransaction();
69
			if($inTransaction) {
70
				$this->inTransaction = false;
71
				return true;
72
			} else {
73
				return false;
74
			}
75
		});
76
	}
77
78
	/**
79
	 * @return bool
80
	 */
81
	public function rollback() {
82
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
83
		return $this->invokeEventHandler($methodName, array(), function () {
84
			$inTransaction = $this->inTransaction();
85
			if($inTransaction) {
86
				$this->inTransaction = false;
87
				return true;
88
			} else {
89
				return false;
90
			}
91
		});
92
	}
93
94
	/**
95
	 * @return bool
96
	 */
97
	public function inTransaction() {
98
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
99
		return $this->invokeEventHandler($methodName, array(), function () {
100
			return $this->inTransaction;
101
		});
102
	}
103
104
	/**
105
	 * @param int $attribute
106
	 * @return mixed
107
	 */
108
	public function getAttribute($attribute) {
109
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
110
		return $this->invokeEventHandler($methodName, array($attribute), function ($attribute) {
111
			$attribute = json_encode($attribute);
112
			if(array_key_exists($attribute, $this->attributes)) {
113
				return $this->attributes[$attribute];
114
			}
115
			return null;
116
		});
117
	}
118
119
	/**
120
	 * @param int $attribute
121
	 * @param mixed $value
122
	 * @return bool
123
	 */
124
	public function setAttribute($attribute, $value) {
125
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
126
		return $this->invokeEventHandler($methodName, array($attribute, $value), function ($attribute, $value) {
127
			$attribute = json_encode($attribute);
128
			$this->attributes[$attribute] = $value;
129
			return true;
130
		});
131
	}
132
133
	/**
134
	 * @param string $statement
135
	 * @return int
136
	 */
137
	public function exec($statement) {
138
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
139
		return $this->invokeEventHandler($methodName, array($statement), function () {
140
			return 1;
141
		});
142
	}
143
144
	/**
145
	 * @param string $statement
146
	 * @return FakePDOStatement
147
	 */
148
	public function query($statement, ?int $fetchMode = null, ...$fetchModeArgs) {
149
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
150
		return $this->invokeEventHandler($methodName, array($statement), function () {
151
			return new FakePDOStatement($this->getEventHandler());
152
		});
153
	}
154
155
	/**
156
	 * @param string|null $name
157
	 * @return string|null
158
	 */
159
	public function lastInsertId($name = null) {
160
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
161
		return $this->invokeEventHandler($methodName, array($name), function () {
162
			return null;
163
		});
164
	}
165
166
	/**
167
	 * @return mixed
168
	 */
169
	public function errorCode() {
170
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
171
		return $this->invokeEventHandler($methodName, array(), function () {
172
			return null;
173
		});
174
	}
175
176
	/**
177
	 * @return mixed
178
	 */
179
	public function errorInfo() {
180
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
181
		return $this->invokeEventHandler($methodName, array(), function () {
182
			return [0, 0, 0];
183
		});
184
	}
185
186
	/**
187
	 * @param string $string
188
	 * @param int $parameter_type
189
	 * @return string|void
190
	 */
191
	public function quote($string, $parameter_type = PDO::PARAM_STR) {
192
		$methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__);
193
		return $this->invokeEventHandler($methodName, array($string, $parameter_type), function ($string) {
194
			return $string;
195
		});
196
	}
197
}
198