Dbal2Adapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 6
    public function __construct(\Doctrine\DBAL\Connection $dbal)
28
    {
29 6
        $this->dbal = $dbal;
30 6
        $this->connection = new Dbal2Connection($this, $dbal);
31 6
    }
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 3
    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 3
            $r = $this->dbal->query($query);
54
55 3
            $results = new Resultset($resultsetType);
56 3
            if ($r->columnCount() > 0) {
57 3
                while ($row = $r->fetch()) {
58 2
                    $results->append((array) $row);
59
                }
60
            }
61 2
        } catch (\Exception $e) {
62 2
            $msg = "Doctrine\Dbal2 adapter query error: {$e->getMessage()} [$query]";
63 2
            throw new Exception\InvalidArgumentException($msg);
64
        }
65
66 3
        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