Passed
Push — master ( 4ecf0e...fb8fa9 )
by Radu
03:55
created

DatabaseException::getSqlState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace WebServCo\Framework\Exceptions;
3
4
final class DatabaseException extends ApplicationException
5
{
6
    const CODE = 0;
7
8
    protected string $sqlState;
9
10
    public function __construct($message, \Exception $previous = null)
11
    {
12
        $code = self::CODE;
13
        if ($previous instanceof \PDOException) {
14
            if (!empty($previous->errorInfo[1])) {
15
                $code = $previous->errorInfo[1];
16
                $this->sqlState = $previous->errorInfo[0];
17
            }
18
            if (!empty($previous->errorInfo[2])) {
19
                // cleaner error message without all the codes.
20
                $message = $previous->errorInfo[2];
21
            }
22
        }
23
        if ($previous instanceof \mysqli_sql_exception) {
24
            // https://stackoverflow.com/a/21081034
25
            $array = (array) $previous;
26
            if (!empty($array['\0*\0sqlstate'])) {
27
                $this->sqlState = $array['\0*\0sqlstate'];
28
            }
29
        }
30
31
        parent::__construct($message, $code, $previous);
32
    }
33
34
    public function getSqlState()
35
    {
36
        return $this->sqlState;
37
    }
38
}
39