Completed
Branch master (d0f689)
by Josué
02:20
created

AbstractOci8Base   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getError() 0 7 1
B getErrorHandler() 0 21 5
1
<?php
2
3
namespace Jpina\Oci8;
4
5
abstract class AbstractOci8Base
6
{
7
    private $errorHandler;
8
    protected $resource;
9
10
    public function getError()
11
    {
12
        set_error_handler($this->getErrorHandler());
13
        $error = oci_error($this->resource);
14
        restore_error_handler();
15
        return $error;
16
    }
17
18
    /**
19
     * @return callable
20
     */
21
    protected function getErrorHandler()
22
    {
23
        if (!$this->errorHandler) {
24
            $this->errorHandler = function ($severity, $message, $file = '', $line = 0) {
25
                restore_error_handler();
26
                $code = 0;
27
28
                $patterns = array('/ORA-(\d+)/', '/OCI-(\d+)/');
29
                foreach ($patterns as $pattern) {
30
                    preg_match($pattern, $message, $matches);
31
                    if (is_array($matches) && array_key_exists(1, $matches)) {
32
                        $code = (int) $matches[1];
33
                    }
34
                }
35
36
                throw new Oci8Exception($message, $code, $severity, $file, $line);
37
            };
38
        }
39
40
        return $this->errorHandler;
41
    }
42
}
43