Completed
Push — master ( b71ad6...2211ef )
by Ivan
08:51
created

Statement::execute()   C

Complexity

Conditions 14
Paths 24

Size

Total Lines 45
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 45
rs 5.0864
cc 14
eloc 33
nc 24
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace vakata\database\driver\oracle;
4
5
use \vakata\database\DBException;
6
use \vakata\database\DriverInterface;
7
use \vakata\database\StatementInterface;
8
use \vakata\database\ResultInterface;
9
10
class Statement implements StatementInterface
11
{
12
    protected $statement;
13
    protected $driver;
14
15
    public function __construct($statement, Driver $driver)
16
    {
17
        $this->statement = $statement;
18
        $this->driver = $driver;
19
    }
20
    public function execute(array $data = []) : ResultInterface
21
    {
22
        $data = array_values($data);
23
        $lob = null;
24
        $ldt = null;
25
        foreach ($data as $i => $v) {
26
            switch (gettype($v)) {
27
                case 'boolean':
28
                case 'integer':
29
                    $data[$i] = (int) $v;
30
                    \oci_bind_by_name($this->statement, 'f'.$i, $data[$i], -1, \SQLT_INT);
31
                    break;
32
                default:
33
                    // keep in mind oracle needs a transaction when inserting LOBs, aside from the specific syntax:
34
                    // INSERT INTO table (column, lobcolumn) VALUES (?, ?, EMPTY_BLOB()) RETURNING lobcolumn INTO ?
35
                    if (is_resource($v) && get_resource_type($v) === 'stream') {
36
                        $ldt = $v;
37
                        $lob = $this->driver->lob();
38
                        \oci_bind_by_name($this->statement, 'f'.$i, $lob, -1, \OCI_B_BLOB);
39
                        continue;
40
                    }
41
                    if (!is_string($data[$i]) && !is_null($data[$i])) {
42
                        $data[$i] = serialize($data[$i]);
43
                    }
44
                    \oci_bind_by_name($this->statement, 'f'.$i, $data[$i]);
45
                    break;
46
            }
47
        }
48
        $temp = \oci_execute($this->statement, $this->driver->isTransaction() ? \OCI_NO_AUTO_COMMIT : \OCI_COMMIT_ON_SUCCESS);
49
        if (!$temp) {
50
            $err = \oci_error($this->statement);
51
            if (!$err) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $err of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
52
                $err = [];
53
            }
54
            throw new DBException('Could not execute query : '.implode(',', $err));
55
        }
56
        if ($lob) {
57
            while (!feof($ldt) && ($ltmp = fread($ldt, 8192)) !== false) {
58
                $lob->write($ltmp);
59
                $lob->flush();
60
            }
61
            $lob->free();
62
        }
63
        return new Result($this->statement);
64
    }
65
}