Failed Conditions
Pull Request — 3.0.x (#3980)
by Guilherme
06:55
created

SQLSrvException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 32
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromSqlSrvErrors() 0 25 5
1
<?php
2
3
namespace Doctrine\DBAL\Driver\SQLSrv;
4
5
use Doctrine\DBAL\Driver\AbstractDriverException;
6
use const SQLSRV_ERR_ERRORS;
7
use function rtrim;
8
use function sqlsrv_errors;
9
10
/**
11
 * @psalm-immutable
12
 */
13
class SQLSrvException extends AbstractDriverException
14
{
15
    /**
16
     * Helper method to turn sql server errors into exception.
17
     *
18
     * @return \Doctrine\DBAL\Driver\SQLSrv\SQLSrvException
19
     */
20
    public static function fromSqlSrvErrors()
21
    {
22
        $message   = '';
23
        $sqlState  = null;
24
        $errorCode = null;
25
26
        foreach ((array) sqlsrv_errors(SQLSRV_ERR_ERRORS) as $error) {
27
            $message .= 'SQLSTATE [' . $error['SQLSTATE'] . ', ' . $error['code'] . ']: ' . $error['message'] . "\n";
28
29
            if ($sqlState === null) {
30
                $sqlState = $error['SQLSTATE'];
31
            }
32
33
            if ($errorCode !== null) {
34
                continue;
35
            }
36
37
            $errorCode = $error['code'];
38
        }
39
40
        if ($message === '') {
41
            $message = 'SQL Server error occurred but no error message was retrieved from driver.';
42
        }
43
44
        return new self(rtrim($message), $sqlState, $errorCode);
45
    }
46
}
47