Completed
Push — master ( 488daa...64d97a )
by Hung
01:46
created

ConnectionBase::silenceConnectionWarning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Foolz\SphinxQL\Drivers;
3
4
use Foolz\SphinxQL\Exception\ConnectionException;
5
use Foolz\SphinxQL\Expression;
6
7
abstract class ConnectionBase implements ConnectionInterface
8
{
9
    /**
10
     * The connection parameters for the database server.
11
     *
12
     * @var array
13
     */
14
    protected $connection_params = array('host' => '127.0.0.1', 'port' => 9306, 'socket' => null);
15
16
    /**
17
     * Internal connection object.
18
     */
19
    protected $connection;
20
21
    /**
22
     * Sets one or more connection parameters.
23
     *
24
     * @param array $params Associative array of parameters and values.
25
     */
26
    public function setParams(Array $params)
27
    {
28
        foreach ($params as $param => $value) {
29
            $this->setParam($param, $value);
30
        }
31
    }
32
33
    /**
34
     * Set a single connection parameter. Valid parameters include:
35
     *
36
     * * string host - The hostname, IP address, or unix socket
37
     * * int port - The port to the host
38
     * * array options - MySQLi options/values, as an associative array. Example: array(MYSQLI_OPT_CONNECT_TIMEOUT => 2)
39
     *
40
     * @param string $param Name of the parameter to modify.
41
     * @param mixed $value Value to which the parameter will be set.
42
     */
43
    public function setParam($param, $value)
44
    {
45
        if ($param === 'host') {
46
            if ($value === 'localhost') {
47
                $value = '127.0.0.1';
48
            } elseif (stripos($value, 'unix:') === 0) {
49
                $param = 'socket';
50
            }
51
        }
52
        if ($param === 'socket') {
53
            if (stripos($value, 'unix:') === 0) {
54
                $value = substr($value, 5);
55
            }
56
            $this->connection_params['host'] = null;
57
        }
58
59
        $this->connection_params[$param] = $value;
60
    }
61
62
    /**
63
     * Returns the connection parameters (host, port, connection timeout) for the current instance.
64
     *
65
     * @return array $params The current connection parameters
66
     */
67
    public function getParams()
68
    {
69
        return $this->connection_params;
70
    }
71
72
    /**
73
     * Returns the current connection established.
74
     *
75
     * @return object Internal connection object
76
     * @throws ConnectionException If no connection has been established or open
77
     */
78
    public function getConnection()
79
    {
80
        if (!is_null($this->connection)) {
81
            return $this->connection;
82
        }
83
84
        throw new ConnectionException('The connection to the server has not been established yet.');
85
    }
86
87
    /**
88
     * Adds quotes around values when necessary.
89
     * Based on FuelPHP's quoting function.
90
     *
91
     * @param Expression|string|null|bool|array|int|float $value The input string, eventually wrapped in an expression
92
     *      to leave it untouched
93
     *
94
     * @return Expression|string|int The untouched Expression or the quoted string
95
     */
96
    public function quote($value)
97
    {
98
        if ($value === null) {
99
            return 'null';
100
        } elseif ($value === true) {
101
            return 1;
102
        } elseif ($value === false) {
103
            return 0;
104
        } elseif ($value instanceof Expression) {
105
            // Use the raw expression
106
            return $value->value();
107
        } elseif (is_int($value)) {
108
            return (int) $value;
109
        } elseif (is_float($value)) {
110
            // Convert to non-locale aware float to prevent possible commas
111
            return sprintf('%F', $value);
112
        }  elseif (is_array($value)) {
113
            // Supports MVA attributes
114
            return '('.implode(',', $this->quoteArr($value)).')';
115
        }
116
117
        return $this->escape($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 96 can also be of type boolean; however, Foolz\SphinxQL\Drivers\C...tionInterface::escape() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
118
    }
119
120
    /**
121
     * Calls $this->quote() on every element of the array passed.
122
     *
123
     * @param array $array The array of strings to quote
124
     *
125
     * @return array The array of quotes strings
126
     */
127
    public function quoteArr(Array $array = array())
128
    {
129
        $result = array();
130
131
        foreach ($array as $key => $item) {
132
            $result[$key] = $this->quote($item);
133
        }
134
135
        return $result;
136
    }
137
138
    /**
139
     * Closes and unset the connection to the Sphinx server.
140
     *
141
     * @return $this
142
     */
143
    public function close()
144
    {
145
        $this->connection = null;
146
        return $this;
147
    }
148
149
    /**
150
     * Establishes a connection if needed
151
     * @throws ConnectionException
152
     */
153
    protected function ensureConnection()
154
    {
155
        try {
156
            $this->getConnection();
157
        } catch (ConnectionException $e) {
158
            $this->connect();
159
        }
160
    }
161
162
    /**
163
     * Establishes a connection to the Sphinx server.
164
     *
165
     * @return bool True if connected
166
     * @throws ConnectionException If a connection error was encountered
167
     */
168
    abstract public function connect();
169
170
}
171