Completed
Push — master ( 7fd4a2...4773f0 )
by Konstantin
14:28 queued 09:30
created

PdoConnection::multiQuery()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
ccs 12
cts 14
cp 0.8571
rs 9.2
cc 4
eloc 14
nc 3
nop 2
crap 4.0466
1
<?php
2
3
namespace Brouzie\Sphinxy\Connection;
4
5
use Brouzie\Sphinxy\Exception\ConnectionException;
6
7
class PdoConnection implements ConnectionInterface
8
{
9
    /**
10
     * @var \PDO
11
     */
12
    private $pdo;
13
14
    private $dsn;
15
16 11
    public function __construct($dsn)
17
    {
18 11
        $this->dsn = $dsn;
19 11
    }
20
21 7
    public function query($query)
22
    {
23 5
        $this->initialize();
24
25
        try {
26 2
            $stmt = $this->pdo->query($query);
27
        } catch (\PDOException $e) {
28
            throw new ConnectionException($e->getMessage(), 0, $e);
29 4
        }
30
31 2
        $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
32
33 6
        return $result;
34
    }
35
36 3
    public function multiQuery($query, array $resultSetNames = array())
37
    {
38 1
        $this->initialize();
39
40
        try {
41 1
            $stmt = $this->pdo->query($query);
42
        } catch (\PDOException $e) {
43
            throw new ConnectionException($e->getMessage(), 0, $e);
44 2
        }
45
46 3
        $results = array();
47 3
        $i = 0;
48
        do {
49 1
            $key = isset($resultSetNames[$i]) ? $resultSetNames[$i] : $i;
50 1
            $results[$key] = $stmt->fetchAll(\PDO::FETCH_ASSOC);
51 1
            ++$i;
52 1
        } while ($stmt->nextRowset());
53
54 3
        return $results;
55 2
    }
56
57 1
    public function exec($query)
58
    {
59 1
        $this->initialize();
60
61
        try {
62 1
            return $this->pdo->exec($query);
63
        } catch (\PDOException $e) {
64
            throw new ConnectionException($e->getMessage(), 0, $e);
65
        }
66
    }
67
68
    public function quote($value)
69
    {
70
        $this->initialize();
71
72
        if (false === $value = $this->pdo->quote((string) $value)) {
73
            throw new ConnectionException($this->pdo->errorInfo(), $this->pdo->errorCode());
74
        }
75
76
        return $value;
77
    }
78
79 9
    protected function initialize()
80
    {
81 7
        if (null === $this->pdo) {
82
            try {
83 5
                $this->pdo = new \PDO($this->dsn, null, null, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
84 1
            } catch (\PDOException $e) {
85 1
                throw new ConnectionException($e->getMessage(), 0, $e);
86 6
            }
87
        }
88 4
    }
89
}
90