Completed
Push — master ( 85d2a2...f0e67d )
by Ivan
04:12
created

Driver::begin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace vakata\database\driver\oracle;
4
5
use \vakata\database\DBException;
6
use \vakata\database\DriverInterface;
7
use \vakata\database\DriverAbstract;
8
use \vakata\database\StatementInterface;
9
use \vakata\database\schema\Table;
10
use \vakata\database\schema\TableRelation;
11
use \vakata\collection\Collection;
12
13
class Driver extends DriverAbstract implements DriverInterface
14
{
15
    use Schema;
16
    
17
    protected $lnk = null;
18
    protected $transaction = false;
19
20
    public function __construct(array $connection)
21
    {
22
        $this->connection = $connection;
23
    }
24
    public function __destruct()
25
    {
26
        $this->disconnect();
27
    }
28
    public function connect()
29
    {
30
        if ($this->lnk === null) {
31
            $this->lnk = @call_user_func(
32
                $this->option('persist') ? '\oci_pconnect' : '\oci_connect',
33
                $this->connection['user'],
34
                $this->connection['pass'],
35
                $this->connection['host'],
36
                $this->option('charset', 'utf8')
37
            );
38
            if ($this->lnk === false) {
39
                throw new DBException('Connect error');
40
            }
41
            $this->query("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'");
42
            if ($timezone = $this->option('timezone')) {
43
                $this->query("ALTER session SET time_zone = '".addslashes($timezone)."'");
44
            }
45
        }
46
    }
47
    public function test() : bool
48
    {
49
        if ($this->lnk) {
50
            return true;
51
        }
52
        try {
53
            @$this->connect();
54
            return true;
55
        } catch (\Exception $e) {
56
            $this->lnk = null;
57
            return false;
58
        }
59
    }
60
    public function disconnect()
61
    {
62
        if ($this->lnk !== null && $this->lnk !== false) {
63
            \oci_close($this->lnk);
64
        }
65
    }
66
    public function raw(string $sql)
67
    {
68
        return \oci_execute(\oci_parse($this->lnk, $sql));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \oci_execute(\oci...rse($this->lnk, $sql)); (boolean) is incompatible with the return type of the parent method vakata\database\DriverAbstract::raw of type vakata\database\ResultInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
69
    }
70
    public function prepare(string $sql) : StatementInterface
71
    {
72
        $this->connect();
73
        $binder = '?';
74
        if (strpos($sql, $binder) !== false) {
75
            $tmp = explode($binder, $sql);
76
            $sql = '';
77
            foreach ($tmp as $i => $v) {
78
                $sql .= $v;
79
                if (isset($tmp[($i + 1)])) {
80
                    $sql .= ':f'.$i;
81
                }
82
            }
83
        }
84
        $temp = \oci_parse($this->lnk, $sql);
85
        if (!$temp) {
86
            $err = \oci_error();
87
            if (!is_array($err)) {
88
                $err = [];
89
            }
90
            throw new DBException('Could not prepare : '.implode(', ', $err).' <'.$sql.'>');
91
        }
92
        return new Statement($temp, $this);
93
    }
94
95
    public function begin() : bool
96
    {
97
         return $this->transaction = true;
98
    }
99
    public function commit() : bool
100
    {
101
        $this->connect();
102
        if (!$this->transaction) {
103
            return false;
104
        }
105
        if (!\oci_commit($this->lnk)) {
106
            return false;
107
        }
108
        $this->transaction = false;
109
        return true;
110
    }
111
    public function rollback() : bool
112
    {
113
        $this->connect();
114
        if (!$this->transaction) {
115
            return false;
116
        }
117
        if (!\oci_rollback($this->lnk)) {
118
            return false;
119
        }
120
        $this->transaction = false;
121
        return true;
122
    }
123
124
    public function isTransaction()
125
    {
126
        return $this->transaction;
127
    }
128
129
    public function lob()
130
    {
131
        return \oci_new_descriptor($this->lnk, \OCI_D_LOB);
132
    }
133
}
134