Completed
Push — master ( 290642...116a3f )
by Sébastien
04:44
created

MysqliAdapter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 84%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 72
ccs 21
cts 25
cp 0.84
rs 10
c 0
b 0
f 0

4 Methods

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