Completed
Push — master ( 17c57a...8a23e4 )
by Sébastien
02:02
created

MysqliAdapter::getResource()   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
9
class MysqliAdapter implements AdapterInterface
10
{
11
12
    use Mysql\MysqlCommonTrait;
13
14
    /**
15
     *
16
     * @var \mysqli
17
     */
18
    protected $resource;
19
20
21
    /**
22
     * Constructor
23
     *
24
     * @param mysqli $connection
25
     */
26 6
    public function __construct(mysqli $connection)
27
    {
28 6
        $this->resource = $connection;
29 6
    }
30
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    public function quoteValue($value)
36
    {
37 1
        return "'" . $this->resource->real_escape_string($value) . "'";
38
    }
39
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 3
    public function query($query)
45
    {
46
        try {
47 3
            $r = $this->resource->query($query);
48
49 3
            $results = new Resultset();
50
51 3
            if ($r === false) {
52 2
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
53 3
            } elseif ($r !== true && !$r instanceof \mysqli_result) {
54
                throw new Exception\InvalidArgumentException("Query didn't return any result [$query].");
55 3
            } elseif ($r instanceof \mysqli_result) {
56 2
                while ($row = $r->fetch_assoc()) {
57 2
                    $results->append($row);
58 2
                }
59 2
            }
60 3
        } catch (Exception\InvalidArgumentException $e) {
61 2
            throw $e;
62
        } catch (\Exception $e) {
63
            $msg = "MysqliException: {$e->getMessage()} [$query]";
64
            throw new Exception\InvalidArgumentException($msg);
65
        }
66 3
        return $results;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 2
    public function execute($query)
73
    {
74 2
        $this->query($query);
75 2
    }
76
77
    /**
78
     * {@ineritdoc}
79
     * @return \mysqli
80
     */
81 1
    public function getResource()
82
    {
83 1
        return $this->resource;
84
    }
85
}
86