Completed
Push — master ( ffc474...cf0a8f )
by Hung
05:25 queued 59s
created

ConnectionBase::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
     * Disables any warning outputs returned on the connection with @ prefix.
23
     *
24
     * @var boolean
25
     */
26
    protected $silence_connection_warning = false;
27
28
    /**
29
     * Sets one or more connection parameters.
30
     *
31
     * @param array $params Associative array of parameters and values.
32
     */
33
    public function setParams(Array $params)
34
    {
35
        foreach ($params as $param => $value) {
36
            $this->setParam($param, $value);
37
        }
38
    }
39
40
    /**
41
     * Set a single connection parameter. Valid parameters include:
42
     *
43
     * * string host - The hostname, IP address, or unix socket
44
     * * int port - The port to the host
45
     * * array options - MySQLi options/values, as an associative array. Example: array(MYSQLI_OPT_CONNECT_TIMEOUT => 2)
46
     *
47
     * @param string $param Name of the parameter to modify.
48
     * @param mixed $value Value to which the parameter will be set.
49
     */
50
    public function setParam($param, $value)
51
    {
52
        if ($param === 'host') {
53
            if ($value === 'localhost') {
54
                $value = '127.0.0.1';
55
            } elseif (stripos($value, 'unix:') === 0) {
56
                $param = 'socket';
57
            }
58
        }
59
        if ($param === 'socket') {
60
            if (stripos($value, 'unix:') === 0) {
61
                $value = substr($value, 5);
62
            }
63
            $this->connection_params['host'] = null;
64
        }
65
66
        $this->connection_params[$param] = $value;
67
    }
68
69
    /**
70
     * Returns the connection parameters (host, port, connection timeout) for the current instance.
71
     *
72
     * @return array $params The current connection parameters
73
     */
74
    public function getParams()
75
    {
76
        return $this->connection_params;
77
    }
78
79
    /**
80
     * Returns the current connection established.
81
     *
82
     * @return object Internal connection object
83
     * @throws ConnectionException If no connection has been established or open
84
     */
85
    public function getConnection()
86
    {
87
        if (!is_null($this->connection)) {
88
            return $this->connection;
89
        }
90
91
        throw new ConnectionException('The connection to the server has not been established yet.');
92
    }
93
94
    /**
95
     * Adds quotes around values when necessary.
96
     * Based on FuelPHP's quoting function.
97
     *
98
     * @param Expression|string|null|bool|array|int|float $value The input string, eventually wrapped in an expression
99
     *      to leave it untouched
100
     *
101
     * @return Expression|string|int The untouched Expression or the quoted string
102
     */
103
    public function quote($value)
104
    {
105
        if ($value === null) {
106
            return 'null';
107
        } elseif ($value === true) {
108
            return 1;
109
        } elseif ($value === false) {
110
            return 0;
111
        } elseif ($value instanceof Expression) {
112
            // Use the raw expression
113
            return $value->value();
114
        } elseif (is_int($value)) {
115
            return (int) $value;
116
        } elseif (is_float($value)) {
117
            // Convert to non-locale aware float to prevent possible commas
118
            return sprintf('%F', $value);
119
        }  elseif (is_array($value)) {
120
            // Supports MVA attributes
121
            return '('.implode(',', $this->quoteArr($value)).')';
122
        }
123
124
        return $this->escape($value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 103 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...
125
    }
126
127
    /**
128
     * Calls $this->quote() on every element of the array passed.
129
     *
130
     * @param array $array The array of strings to quote
131
     *
132
     * @return array The array of quotes strings
133
     */
134
    public function quoteArr(Array $array = array())
135
    {
136
        $result = array();
137
138
        foreach ($array as $key => $item) {
139
            $result[$key] = $this->quote($item);
140
        }
141
142
        return $result;
143
    }
144
145
    /**
146
     * Closes and unset the connection to the Sphinx server.
147
     *
148
     * @return $this
149
     */
150
    public function close()
151
    {
152
        $this->connection = null;
153
        return $this;
154
    }
155
156
    /**
157
     * Establishes a connection if needed
158
     * @throws ConnectionException
159
     */
160
    protected function ensureConnection()
161
    {
162
        try {
163
            $this->getConnection();
164
        } catch (ConnectionException $e) {
165
            $this->connect();
166
        }
167
    }
168
169
    /**
170
     * Establishes a connection to the Sphinx server.
171
     *
172
     * @param bool $suppress_error If the warnings on the connection should be suppressed
173
     *
174
     * @return bool True if connected
175
     * @throws ConnectionException If a connection error was encountered
176
     */
177
    abstract public function connect($suppress_error = false);
178
179
    /**
180
     * Forces the connection to suppress all errors returned. This should only be used
181
     * when the production server is running with high error reporting settings.
182
     *
183
     * @param boolean $enable True if it should be enabled, false if it should be disabled
184
     * @deprecated
185
     * not good
186
     */
187
    public function silenceConnectionWarning($enable = true)
188
    {
189
        $this->silence_connection_warning = $enable;
190
    }
191
}
192