Completed
Push — master ( d12521...8ad93f )
by Sébastien
01:59
created

MysqlConnectionAdapter::executeMysqli()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.583

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 22
ccs 10
cts 14
cp 0.7143
rs 8.6738
cc 5
eloc 14
nc 10
nop 1
crap 5.583
1
<?php
2
3
namespace Soluble\Schema\Source\Mysql;
4
5
/**
6
 * Wrapper for basic mysqli/pdo usage
7
 */
8
9
use Soluble\Schema\Exception;
10
use ArrayObject;
11
12
class MysqlConnectionAdapter
13
{
14
15
    const DRIVER_TYPE_PDO = 'pdo';
16
    const DRIVER_TYPE_MYSQLI = 'mysqli';
17
18
    /**
19
     *
20
     * @var string
21
     */
22
    protected $driver;
23
24
    /**
25
     *
26
     * @var \mysqli|null
27
     */
28
    protected $mysqli;
29
30
    /**
31
     *
32
     * @var \PDO|null
33
     */
34
    protected $pdo;
35
36
    /**
37
     *
38
     * @throws Exception\InvalidArgumentException
39
     * @param \mysqli|\PDO $conn
40
     */
41 26
    public function __construct($conn)
42
    {
43 26
        if ($conn instanceof \mysqli) {
44 4
            $this->mysqli = $conn;
45 4
            $this->type = self::DRIVER_TYPE_MYSQLI;
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46 26
        } elseif ($conn instanceof \PDO && $conn->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql') {
47 25
            $this->pdo = $conn;
48 25
            $this->type = self::DRIVER_TYPE_PDO;
49 25
        } else {
50
            $msg = "MysqlConnectionAdapter requires connection to be either 'pdo:mysql' or 'mysqli'";
51
            throw new Exception\InvalidArgumentException($msg);
52
        }
53 26
    }
54
55
    /**
56
     * Return current schema name
57
     * @return string|false
58
     */
59 26
    public function getCurrentSchema()
60
    {
61 26
        $query = 'SELECT DATABASE() as current_schema';
62 26
        $results = $this->query($query);
63 26
        if (count($results) == 0) {
64
            return false;
65
        }
66 26
        return $results[0]['current_schema'];
67
    }
68
69
    /**
70
     *
71
     * @param string $value
72
     * @return string
73
     */
74 6
    public function quoteValue($value)
75
    {
76 6
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
77
            $quoted = "'" . $this->mysqli->real_escape_string($value) . "'";
78
        } else {
79 6
            $quoted = $this->pdo->quote($value);
80
        }
81 6
        return $quoted;
82
    }
83
84
    /**
85
     * Execute query and return query as an ArrayObject
86
     *
87
     * @param string $query
88
     * @return ArrayObject
89
     */
90 26
    public function query($query)
91
    {
92 26
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
93 4
            $results = $this->executeMysqli($query);
94 4
        } else {
95 25
            $results = $this->executePDO($query);
96
        }
97 26
        return $results;
98
    }
99
100
    /**
101
     *
102
     * @param string $query
103
     * @return ArrayObject
104
     */
105 25
    protected function executePDO($query)
106
    {
107
108
        try {
109 25
            $stmt = $this->pdo->query($query, \PDO::FETCH_ASSOC);
110 25
            if (!$stmt) {
111
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
112
            }
113 25
            $results = new ArrayObject();
114 25
            foreach ($stmt as $row) {
115 25
                $results->append($row);
116 25
            }
117 25
        } catch (\Exception $e) {
118
            $msg = "Query error: {$e->getMessage}";
0 ignored issues
show
Bug introduced by
The property getMessage does not seem to exist. Did you mean message?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
119
            throw new Exception\InvalidArgumentException($msg);
120
        }
121 25
        return $results;
122
    }
123
124
    /**
125
     *
126
     * @param string $query
127
     * @return ArrayObject
128
     */
129 4
    protected function executeMysqli($query)
130
    {
131
        try {
132 4
            $r = $this->mysqli->query($query);
133 4
            if (!$r) {
134
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
135 4
            } elseif (!$r instanceof \mysqli_result) {
136
                throw new Exception\InvalidArgumentException("Query didn't return any result [$query].");
137
            }
138
            
139 4
            $results = new ArrayObject();
140
            
141 4
            while ($row = $r->fetch_assoc()) {
142 4
                $results->append($row);
143 4
            }
144
            
145 4
        } catch (\Exception $e) {
146
            $msg = "Query error: {$e->getMessage}";
0 ignored issues
show
Bug introduced by
The property getMessage does not seem to exist. Did you mean message?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
147
            throw new Exception\InvalidArgumentException($msg);
148
        }
149 4
        return $results;
150
    }
151
}
152