Completed
Push — 2.10 ( 96cda3...d49d68 )
by Gabriel
23:06 queued 19:24
created

SQLSrvException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 68.42%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 32
ccs 13
cts 19
cp 0.6842
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
class SQLSrvException extends AbstractDriverException
11
{
12
    /**
13
     * Helper method to turn sql server errors into exception.
14
     *
15
     * @return \Doctrine\DBAL\Driver\SQLSrv\SQLSrvException
16
     */
17 90
    public static function fromSqlSrvErrors()
18
    {
19 90
        $message   = '';
20 90
        $sqlState  = null;
21 90
        $errorCode = null;
22
23 90
        foreach ((array) sqlsrv_errors(SQLSRV_ERR_ERRORS) as $error) {
24 90
            $message .= 'SQLSTATE [' . $error['SQLSTATE'] . ', ' . $error['code'] . ']: ' . $error['message'] . "\n";
25
26 90
            if ($sqlState === null) {
27 90
                $sqlState = $error['SQLSTATE'];
28
            }
29
30 90
            if ($errorCode !== null) {
31 89
                continue;
32
            }
33
34 90
            $errorCode = $error['code'];
35
        }
36
37 90
        if (! $message) {
38
            $message = 'SQL Server error occurred but no error message was retrieved from driver.';
39
        }
40
41 90
        return new self(rtrim($message), $sqlState, $errorCode);
42
    }
43
}
44