PdoConnection::multiQuery()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
rs 9.2
c 0
b 0
f 0
cc 4
eloc 14
nc 3
nop 2
crap 4.0092
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 9
    public function __construct($dsn)
17
    {
18 9
        $this->dsn = $dsn;
19 9
    }
20
21 9 View Code Duplication
    public function query($query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23 9
        $this->initialize();
24
25
        try {
26 6
            $stmt = $this->pdo->query($query);
27 4
        } catch (\PDOException $e) {
28
            throw new ConnectionException($e->getMessage(), 0, $e);
29
        }
30
31 6
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
32
    }
33
34 3
    public function multiQuery($query, array $resultSetNames = array())
35
    {
36 3
        $this->initialize();
37
38
        try {
39 3
            $stmt = $this->pdo->query($query);
40 2
        } catch (\PDOException $e) {
41
            throw new ConnectionException($e->getMessage(), 0, $e);
42
        }
43
44 3
        $results = array();
45 3
        $i = 0;
46
        do {
47 3
            $key = isset($resultSetNames[$i]) ? $resultSetNames[$i] : $i;
48 3
            $results[$key] = $stmt->fetchAll(\PDO::FETCH_ASSOC);
49 3
            ++$i;
50 3
        } while ($stmt->nextRowset());
51
52 3
        return $results;
53
    }
54
55 3 View Code Duplication
    public function exec($query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57 3
        $this->initialize();
58
59
        try {
60 3
            return $this->pdo->exec($query);
61
        } catch (\PDOException $e) {
62
            throw new ConnectionException($e->getMessage(), 0, $e);
63
        }
64
    }
65
66
    public function quote($value)
67
    {
68
        $this->initialize();
69
70
        if (false === $value = $this->pdo->quote((string) $value)) {
71
            throw new ConnectionException($this->pdo->errorInfo(), $this->pdo->errorCode());
72
        }
73
74
        return $value;
75
    }
76
77 9
    protected function initialize()
78
    {
79 9
        if (null === $this->pdo) {
80
            try {
81 9
                $this->pdo = new \PDO($this->dsn, null, null, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
82 6
                $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
83 7
            } catch (\PDOException $e) {
84 3
                throw new ConnectionException($e->getMessage(), 0, $e);
85
            }
86 4
        }
87 6
    }
88
89
    public function checkConnection()
90
    {
91
        try {
92
            $this->query('SELECT 1');
93
        } catch (ConnectionException $e) {
94
            $this->pdo = null;
95
        }
96
    }
97
}
98