Completed
Push — feature-set-edition ( 0d4ba0 )
by Josué
07:38
created

AbstractOci8Base::getErrorHandler()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0488

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 21
ccs 14
cts 16
cp 0.875
rs 8.7624
cc 5
eloc 12
nc 2
nop 0
crap 5.0488
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