AbstractOci8Base::getError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Jpina\Oci8;
4
5
abstract class AbstractOci8Base
6
{
7
    protected static $errorHandler;
8
    protected $resource;
9
10 4
    public function getError()
11
    {
12 4
        set_error_handler($this->getErrorHandler());
13 4
        $error = oci_error($this->resource);
14 4
        restore_error_handler();
15 4
        return $error;
16
    }
17
18
    /**
19
     * @return callable
20
     */
21 103
    protected static function getErrorHandler()
22
    {
23 103
        if (!static::$errorHandler) {
24 40
            static::$errorHandler = function ($severity, $message, $file = '', $line = 0) {
25 40
                restore_error_handler();
26 40
                $code = 0;
27
28 40
                $patterns = array('/ORA-(\d+)/', '/OCI-(\d+)/');
29 40
                foreach ($patterns as $pattern) {
30 40
                    preg_match($pattern, $message, $matches);
31 40
                    if (is_array($matches) && array_key_exists(1, $matches)) {
32 10
                        $code = (int) $matches[1];
33 10
                    }
34 40
                }
35
36 40
                throw new Oci8Exception($message, $code, $severity, $file, $line);
37
            };
38
        }
39
40 103
        return static::$errorHandler;
41
    }
42
}
43