Passed
Pull Request — master (#197)
by
unknown
01:57
created

Connection::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Foolz\SphinxQL\Drivers\Pdo;
3
4
use Foolz\SphinxQL\Drivers\ConnectionBase;
5
use Foolz\SphinxQL\Drivers\MultiResultSet;
6
use Foolz\SphinxQL\Drivers\ResultSet;
7
use Foolz\SphinxQL\Exception\ConnectionException;
8
use Foolz\SphinxQL\Exception\DatabaseException;
9
use Foolz\SphinxQL\Exception\SphinxQLException;
10
use mysqli;
11
use PDO;
12
use PDOException;
13
use RuntimeException;
14
15
class Connection extends ConnectionBase
16
{
17
18
    /**
19
     * @return PDO
20
     * @throws ConnectionException
21
     */
22
    public function getConnection(): PDO
23
    {
24
        $connection = parent::getConnection();
25
26
        if ($connection instanceof mysqli) {
27
            throw new RuntimeException('Connection type mismatch');
28
        }
29
30
        return $connection;
31
    }
32
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function query($query)
38
    {
39
        $this->ensureConnection();
40
41
        $statement = $this->getConnection()->prepare($query);
42
43
        try {
44
            $statement->execute();
45
        } catch (PDOException $exception) {
46
            throw new DatabaseException($exception->getMessage() . ' [' . $query . ']', $exception->getCode(), $exception);
47
        }
48
49
        return new ResultSet(new ResultSetAdapter($statement));
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function connect(): bool
56
    {
57
        $params = $this->getParams();
58
59
        $dsn = 'mysql:';
60
        if (isset($params['host']) && $params['host'] != '') {
61
            $dsn .= 'host=' . $params['host'] . ';';
62
        }
63
        if (isset($params['port'])) {
64
            $dsn .= 'port=' . $params['port'] . ';';
65
        }
66
        if (isset($params['charset'])) {
67
            $dsn .= 'charset=' . $params['charset'] . ';';
68
        }
69
70
        if (isset($params['socket']) && $params['socket'] != '') {
71
            $dsn .= 'unix_socket=' . $params['socket'] . ';';
72
        }
73
74
        try {
75
            $con = new PDO($dsn);
76
        } catch (PDOException $exception) {
77
            throw new ConnectionException($exception->getMessage(), $exception->getCode(), $exception);
78
        }
79
80
        $this->connection = $con;
81
        $this->getConnection()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
82
83
        return true;
84
    }
85
86
    /**
87
     * @return bool
88
     * @throws ConnectionException
89
     */
90
    public function ping()
91
    {
92
        $this->ensureConnection();
93
94
        return $this->getConnection() !== null;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function multiQuery(array $queue)
101
    {
102
        $this->ensureConnection();
103
104
        if (count($queue) === 0) {
105
            throw new SphinxQLException('The Queue is empty.');
106
        }
107
108
        try {
109
            $statement = $this->getConnection()->query(implode(';', $queue));
110
        } catch (PDOException $exception) {
111
            throw new DatabaseException($exception->getMessage() .' [ '.implode(';', $queue).']', $exception->getCode(), $exception);
112
        }
113
114
        return new MultiResultSet(new MultiResultSetAdapter($statement));
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function escape($value)
121
    {
122
        $this->ensureConnection();
123
124
        return $this->getConnection()->quote($value);
125
    }
126
}
127