Passed
Push — master ( b953c9...de8e3d )
by Ron
02:53
created

throwMoreConcreteException()   C

Complexity

Conditions 13
Paths 44

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 1 Features 0
Metric Value
eloc 15
c 8
b 1
f 0
dl 0
loc 18
rs 6.6166
cc 13
nc 44
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Kir\MySQL\Databases\MySQL;
3
4
use Kir\MySQL\Exceptions\DatabaseHasGoneAwayException;
5
use Kir\MySQL\Exceptions\IntegrityConstraintViolationException;
6
use Kir\MySQL\Exceptions\SqlException;
7
use PDOException;
8
use Kir\MySQL\Exceptions\SqlDeadLockException;
9
use Kir\MySQL\Exceptions\DuplicateUniqueKeyException;
10
use Kir\MySQL\Exceptions\LockWaitTimeoutExceededException;
11
12
class MySQLExceptionInterpreter {
13
	/**
14
	 * @param PDOException $exception
15
	 * @return SqlException
16
	 */
17
	public function getMoreConcreteException(PDOException $exception): SqlException {
18
		$errorInfo = $exception->errorInfo;
19
		/** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */
20
		$code = is_array($errorInfo) && isset($errorInfo[1]) ? ((int) $errorInfo[1]) : ((int) $exception->getCode());
21
		$message = $exception->getMessage();
22
		switch($code) {
23
			case 2006: return new DatabaseHasGoneAwayException($message, $code, $exception);
24
			case 1213: return new SqlDeadLockException($message, $code, $exception);
25
			case 1205: return new LockWaitTimeoutExceededException($message, $code, $exception);
26
			case 1022:
27
			case 1062:
28
			case 1169:
29
			case 1586: return new DuplicateUniqueKeyException($message, $code, $exception);
30
			case 1216:
31
			case 1217:
32
			case 1452: return new IntegrityConstraintViolationException($message, $code, $exception);
33
		}
34
		return new SqlException($message, $code, $exception);
35
	}
36
}
37