Completed
Push — develop ( 3b8e4a...656576 )
by Arjay
03:44 queued 02:00
created

OracleProcessor::saveLob()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 15

Duplication

Lines 5
Ratio 17.86 %

Importance

Changes 4
Bugs 4 Features 0
Metric Value
c 4
b 4
f 0
dl 5
loc 28
rs 8.5806
cc 4
eloc 15
nc 8
nop 4
1
<?php
2
3
namespace Yajra\Oci8\Query\Processors;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Database\Query\Processors\Processor;
7
use PDO;
8
9
class OracleProcessor extends Processor
10
{
11
    /**
12
     * Process an "insert get ID" query.
13
     *
14
     * @param  Builder $query
15
     * @param  string $sql
16
     * @param  array $values
17
     * @param  string $sequence
18
     * @return int
19
     */
20
    public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
21
    {
22
        $id        = 0;
23
        $parameter = 0;
24
        $statement = $this->prepareStatement($query, $sql);
25
26 View Code Duplication
        for ($i = 0; $i < count($values); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
            $type = $this->getPdoType($values[$i]);
28
            $statement->bindParam($parameter, $values[$i], $type);
29
            $parameter++;
30
        }
31
        $statement->bindParam($parameter, $id, PDO::PARAM_INT, 10);
32
        $statement->execute();
33
34
        return (int) $id;
35
    }
36
37
    /**
38
     * Get prepared statement.
39
     *
40
     * @param Builder $query
41
     * @param string $sql
42
     * @return \PDOStatement | \Yajra\Pdo\Oci8
43
     */
44
    private function prepareStatement(Builder $query, $sql)
45
    {
46
        $pdo = $query->getConnection()->getPdo();
47
48
        return $pdo->prepare($sql);
49
    }
50
51
    /**
52
     * Get PDO Type depending on value.
53
     *
54
     * @param mixed $value
55
     * @return int
56
     */
57
    private function getPdoType($value)
58
    {
59
        if (is_int($value)) {
60
            return PDO::PARAM_INT;
61
        } elseif (is_bool($value)) {
62
            return PDO::PARAM_BOOL;
63
        } elseif (is_null($value)) {
64
            return PDO::PARAM_NULL;
65
        } else {
66
            return PDO::PARAM_STR;
67
        }
68
    }
69
70
    /**
71
     * save Query with Blob returning primary key value
72
     *
73
     * @param  Builder $query
74
     * @param  string $sql
75
     * @param  array $values
76
     * @param  array $binaries
77
     * @return int
78
     */
79
    public function saveLob(Builder $query, $sql, array $values, array $binaries)
80
    {
81
        $id        = 0;
82
        $parameter = 0;
83
        $statement = $this->prepareStatement($query, $sql);
84
85
        // bind values.
86 View Code Duplication
        for ($i = 0; $i < count($values); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
            $type = $this->getPdoType($values[$i]);
88
            $statement->bindParam($parameter, $values[$i], $type);
89
            $parameter++;
90
        }
91
92
        // bind blob fields.
93
        for ($i = 0; $i < count($binaries); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
94
            $statement->bindParam($parameter, $binaries[$i], PDO::PARAM_LOB, -1);
95
            $parameter++;
96
        }
97
98
        // bind output param for the returning clause.
99
        $statement->bindParam($parameter, $id, PDO::PARAM_INT, 10);
100
101
        if (! $statement->execute()) {
102
            return false;
103
        }
104
105
        return (int) $id;
106
    }
107
}
108