Completed
Push — master ( 718bd6...a938bc )
by Sébastien
07:05
created

Dbal2Adapter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 69
ccs 17
cts 18
cp 0.9444
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
B query() 0 24 4
1
<?php
2
3
namespace Soluble\DbWrapper\Adapter\Doctrine;
4
5
use Soluble\DbWrapper\Exception;
6
use Soluble\DbWrapper\Result\Resultset;
7
use Soluble\DbWrapper\Adapter\AdapterInterface;
8
use Soluble\DbWrapper\Connection\Doctrine\Dbal2Connection;
9
10
class Dbal2Adapter implements AdapterInterface
11
{
12
    /**
13
     * @var \Doctrine\DBAL\Connection
14
     */
15
    protected $dbal;
16
17
    /**
18
     * @var Dbal2Connection
19
     */
20
    protected $connection;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param \Doctrine\DBAL\Connection $dbal
26
     */
27 4
    public function __construct(\Doctrine\DBAL\Connection $dbal)
28
    {
29 4
        $this->dbal = $dbal;
30 4
        $this->connection = new Dbal2Connection($this, $dbal);
31 4
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function quoteValue($value)
37
    {
38 1
        return $this->dbal->quote($value);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function query($query, $resultsetType = Resultset::TYPE_ARRAY)
45
    {
46
        // This error may happen with the libmysql instead of mysqlnd and using set statement (set @test=1)
47
        // : "Attempt to read a row while there is no result set associated with the statement"
48
49
        try {
50
            /**
51
             * @var \Doctrine\DBAL\Driver\Mysqli\MysqliStatement
52
             */
53 1
            $r = $this->dbal->query($query);
54
55 1
            $results = new Resultset($resultsetType);
56 1
            if ($r->columnCount() > 0) {
57 1
                while ($row = $r->fetch()) {
58
                    $results->append((array) $row);
59
                }
60
            }
61 1
        } catch (\Exception $e) {
62 1
            $msg = "Doctrine\Dbal2 adapter query error: {$e->getMessage()} [$query]";
63 1
            throw new Exception\InvalidArgumentException($msg);
64
        }
65
66 1
        return $results;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @return Dbal2Connection
73
     */
74 1
    public function getConnection()
75
    {
76 1
        return $this->connection;
77
    }
78
}
79