Completed
Push — master ( dbcbc1...04d817 )
by Sébastien
02:01
created

MysqliAdapter::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Soluble\DbWrapper\Adapter;
4
5
use Soluble\DbWrapper\Exception;
6
use mysqli;
7
use Soluble\DbWrapper\Result\Resultset;
8
use Soluble\DbWrapper\Connection\MysqlConnection;
9
10
class MysqliAdapter implements AdapterInterface
11
{
12
13
    /**
14
     *
15
     * @var \mysqli
16
     */
17
    protected $resource;
18
    
19
    /**
20
     *
21
     * @var MysqlConnection
22
     */
23
    protected $connection;
24
25
26
    /**
27
     * Constructor
28
     *
29
     * @param mysqli $resource
30
     */
31 7
    public function __construct(mysqli $resource)
32
    {
33 7
        $this->resource = $resource;
34 7
        $this->connection = new MysqlConnection($this, $resource);
35 7
    }
36
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function quoteValue($value)
42
    {
43 1
        return "'" . $this->resource->real_escape_string($value) . "'";
44
    }
45
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 4
    public function query($query)
51
    {
52
        try {
53 4
            $r = $this->resource->query($query);
54
55 4
            $results = new Resultset();
56
57 4
            if ($r === false) {
58 2
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
59 4
            } elseif ($r !== true && !$r instanceof \mysqli_result) {
60
                throw new Exception\InvalidArgumentException("Query didn't return any result [$query].");
61 4
            } elseif ($r instanceof \mysqli_result) {
62 3
                while ($row = $r->fetch_assoc()) {
63 3
                    $results->append($row);
64 3
                }
65 3
            }
66 4
        } catch (Exception\InvalidArgumentException $e) {
67 2
            throw $e;
68
        } catch (\Exception $e) {
69
            $msg = "MysqliException: {$e->getMessage()} [$query]";
70
            throw new Exception\InvalidArgumentException($msg);
71
        }
72 4
        return $results;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     * @return MysqlConnection
78
     */
79 3
    public function getConnection() 
80
    {
81 3
        return $this->connection;
82
    }
83
}
84