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

MySQLExceptionInterpreter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 7
c 7
b 1
f 1
lcom 0
cbo 5
dl 0
loc 21
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B throwMoreConcreteException() 0 15 7
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