Passed
Push — master ( 8fe57b...ddc32f )
by Ron
10:31
created

getMoreConcreteException()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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