MySQLExceptionInterpreter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 9
Bugs 1 Features 0
Metric Value
wmc 3
eloc 11
dl 0
loc 18
rs 10
c 9
b 1
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getMoreConcreteException() 0 13 3
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