Completed
Push — master ( 9cfab5...3e7612 )
by Ron
02:59
created

throwMoreConcreteException()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 15
rs 8.2222
cc 7
eloc 11
nc 6
nop 1
1
<?php
2
namespace Kir\MySQL\Databases\MySQL;
3
4
use Kir\MySQL\Exceptions\DatabaseHasGoneAwayException;
5
use Kir\MySQL\Exceptions\SqlException;
6
use PDOException;
7
use Kir\MySQL\Exceptions\SqlDeadLockException;
8
use Kir\MySQL\Exceptions\DuplicateUniqueKeyException;
9
use Kir\MySQL\Exceptions\LockWaitTimeoutExceededException;
10
11
class MySQLExceptionInterpreter {
12
	/**
13
	 * @param PDOException $exception
14
	 * @throw PDOException
15
	 */
16
	public function throwMoreConcreteException(PDOException $exception) {
17
		$code = $exception->errorInfo[1];
18
		$message = (string) $exception->errorInfo[2];
19
		switch($code) {
20
			case 2006: throw new DatabaseHasGoneAwayException($message, $code, $exception);
21
			case 1213: throw new SqlDeadLockException($message, $code, $exception);
22
			case 1205: throw new LockWaitTimeoutExceededException($message, $code, $exception);
23
			case 1062: throw new DuplicateUniqueKeyException($message, $code, $exception);
24
		}
25
		/** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */
26
		if(!is_string($message) || !is_int($code)) {
27
			throw new SqlException((string) $message, (int) $code, $exception);
28
		}
29
		throw $exception;
30
	}
31
}
32