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

Driver::commit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.8333
cc 3
nc 3
nop 0
1
<?php
2
3
namespace vakata\database\driver\ibase;
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
12
class Driver extends DriverAbstract implements DriverInterface
13
{
14
    protected $lnk = null;
15
    protected $transaction = null;
16
17
    public function __construct(array $connection)
18
    {
19
        $this->connection = $connection;
20
        if (!is_file($connection['name']) && is_file('/'.$connection['name'])) {
21
            $this->connection['name'] = '/'.$connection['name'];
22
        }
23
        $this->connection['host'] = ($connection['host'] === 'localhost' || $connection['host'] === '') ?
24
                '' : $connection['host'].':';
25
    }
26
    public function __destruct()
27
    {
28
        $this->disconnect();
29
    }
30
    public function connect()
31
    {
32
        $this->lnk = call_user_func(
33
            $this->option('persist') ? '\ibase_pconnect' : '\ibase_connect',
34
            $this->connection['host'].$this->connection['name'],
35
            $this->connection['user'],
36
            $this->connection['pass'],
37
            strtoupper($this->option('charset', 'utf8'))
38
        );
39
        if ($this->lnk === false) {
40
            throw new DBException('Connect error: '.\ibase_errmsg());
41
        }
42
    }
43
    public function test() : bool
44
    {
45
        if ($this->lnk) {
46
            return true;
47
        }
48
        try {
49
            @$this->connect();
50
            return true;
51
        } catch (\Exception $e) {
52
            $this->lnk = null;
53
            return false;
54
        }
55
    }
56
    public function disconnect()
57
    {
58
        if (is_resource($this->lnk)) {
59
            \ibase_close($this->lnk);
60
        }
61
    }
62
    public function raw(string $sql)
63
    {
64
        return \ibase_query($this->lnk, $sql);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \ibase_query($this->lnk, $sql); (resource) 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...
65
    }
66
    public function prepare(string $sql) : StatementInterface
67
    {
68
        $this->connect();
69
        $statement = \ibase_prepare($this->transaction !== null ? $this->transaction : $this->lnk, $sql);
70
        if ($statement === false) {
71
            throw new DBException('Prepare error: ' . \ibase_errmsg());
72
        }
73
        return new Statement(
74
            $statement,
75
            $this->lnk
76
        );
77
    }
78
79
    public function begin() : bool
80
    {
81
        $this->connect();
82
        $this->transaction = \ibase_trans($this->lnk);
83
        if ($this->transaction === false) {
84
            $this->transaction === null;
85
        }
86
        return ($this->transaction !== null);
87
    }
88
    public function commit() : bool
89
    {
90
        $this->connect();
91
        if ($this->transaction === null) {
92
            return false;
93
        }
94
        if (!\ibase_commit($this->transaction)) {
95
            return false;
96
        }
97
        $this->transaction = null;
98
99
        return true;
100
    }
101
    public function rollback() : bool
102
    {
103
        $this->connect();
104
        if ($this->transaction === null) {
105
            return false;
106
        }
107
        if (!\ibase_rollback($this->transaction)) {
108
            return false;
109
        }
110
        $this->transaction = null;
111
112
        return true;
113
    }
114
115
    public function isTransaction()
116
    {
117
        return $this->transaction !== null;
118
    }
119
}
120