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

Driver   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 0
loc 100
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A __destruct() 0 4 1
A connect() 0 14 6
A test() 0 13 3
A disconnect() 0 6 2
A prepare() 0 10 1
A raw() 0 4 1
A begin() 0 7 1
A commit() 0 8 1
A rollback() 0 8 1
A isTransaction() 0 4 1
1
<?php
2
3
namespace vakata\database\driver\odbc;
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 = false;
16
17
    public function __construct(array $connection)
18
    {
19
        $temp = explode('://', $connection['orig'], 2)[1];
20
        $temp = array_pad(explode('?', $temp, 2), 2, '');
21
        $connection = [];
22
        $connection['opts'] = [];
23
        parse_str($temp[1], $connection['opts']);
24
        $temp = $temp[0];
25
        if (strpos($temp, '@') !== false) {
26
            $temp = array_pad(explode('@', $temp, 2), 2, '');
27
            list($connection['user'], $connection['pass']) = array_pad(explode(':', $temp[0], 2), 2, '');
28
            $temp = $temp[1];
29
        }
30
        $connection['dsn'] = $temp;
31
        $this->connection = $connection;
32
    }
33
    public function __destruct()
34
    {
35
        $this->disconnect();
36
    }
37
    public function connect()
38
    {
39
        if ($this->lnk === null) {
40
            $this->lnk = call_user_func(
41
                $this->option('persist') ? '\odbc_pconnect' : '\odbc_connect',
42
                $this->connection['dsn'],
43
                isset($this->connection['user']) ? $this->connection['user'] : '',
44
                isset($this->connection['pass']) ? $this->connection['pass'] : ''
45
            );
46
            if ($this->lnk === false) {
47
                throw new DBException('Connect error');
48
            }
49
        }
50
    }
51
    public function test() : bool
52
    {
53
        if ($this->lnk) {
54
            return true;
55
        }
56
        try {
57
            @$this->connect();
58
            return true;
59
        } catch (\Exception $e) {
60
            $this->lnk = null;
61
            return false;
62
        }
63
    }
64
    public function disconnect()
65
    {
66
        if (is_resource($this->lnk)) {
67
            \odbc_close($this->lnk);
68
        }
69
    }
70
    public function prepare(string $sql) : StatementInterface
71
    {
72
        $this->connect();
73
        return new Statement(
74
            $sql,
75
            $this->lnk,
76
            $this->connection['opts']['charset_in'] ?? null,
77
            $this->connection['opts']['charset_out'] ?? null
78
        );
79
    }
80
    public function raw(string $sql)
81
    {
82
        return \odbc_exec($this->lnk, $sql);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \odbc_exec($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...
83
    }
84
    public function begin() : bool
85
    {
86
        $this->connect();
87
        $this->transaction = true;
88
        \odbc_autocommit($this->lnk, false);
89
        return true;
90
    }
91
    public function commit() : bool
92
    {
93
        $this->connect();
94
        $this->transaction = false;
95
        $res = \odbc_commit($this->lnk);
96
        \odbc_autocommit($this->lnk, false);
97
        return $res;
98
    }
99
    public function rollback() : bool
100
    {
101
        $this->connect();
102
        $this->transaction = false;
103
        $res = \odbc_rollback($this->lnk);
104
        \odbc_autocommit($this->lnk, false);
105
        return $res;
106
    }
107
    public function isTransaction()
108
    {
109
        return $this->transaction;
110
    }
111
}
112