Completed
Push — master ( 4cdb71...eee063 )
by Nicolas
02:28
created

lib/Elastica/Connection.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elastica;
3
4
use Elastica\Exception\InvalidException;
5
use Elastica\Transport\AbstractTransport;
6
7
/**
8
 * Elastica connection instance to an elasticasearch node.
9
 *
10
 * @author   Nicolas Ruflin <[email protected]>
11
 */
12
class Connection extends Param
13
{
14
    /**
15
     * Default elastic search port.
16
     */
17
    const DEFAULT_PORT = 9200;
18
19
    /**
20
     * Default host.
21
     */
22
    const DEFAULT_HOST = 'localhost';
23
24
    /**
25
     * Default transport.
26
     *
27
     * @var string
28
     */
29
    const DEFAULT_TRANSPORT = 'Http';
30
31
    /**
32
     * Default compression.
33
     *
34
     * @var string
35
     */
36
    const DEFAULT_COMPRESSION = false;
37
38
    /**
39
     * Number of seconds after a timeout occurs for every request
40
     * If using indexing of file large value necessary.
41
     */
42
    const TIMEOUT = 300;
43
44
    /**
45
     * Number of seconds after a connection timeout occurs for every request during the connection phase.
46
     *
47
     * @see Connection::setConnectTimeout();
48
     */
49
    const CONNECT_TIMEOUT = 0;
50
51
    /**
52
     * Creates a new connection object. A connection is enabled by default.
53
     *
54
     * @param array $params OPTIONAL Connection params: host, port, transport, timeout. All are optional
55
     */
56
    public function __construct(array $params = [])
57
    {
58
        $this->setParams($params);
59
        $this->setEnabled(true);
60
61
        // Set empty config param if not exists
62
        if (!$this->hasParam('config')) {
63
            $this->setParam('config', []);
64
        }
65
    }
66
67
    /**
68
     * @return int Server port
69
     */
70
    public function getPort()
71
    {
72
        return $this->hasParam('port') ? $this->getParam('port') : self::DEFAULT_PORT;
73
    }
74
75
    /**
76
     * @param int $port
77
     *
78
     * @return $this
79
     */
80
    public function setPort($port)
81
    {
82
        return $this->setParam('port', (int) $port);
83
    }
84
85
    /**
86
     * @return string Host
87
     */
88
    public function getHost()
89
    {
90
        return $this->hasParam('host') ? $this->getParam('host') : self::DEFAULT_HOST;
91
    }
92
93
    /**
94
     * @param string $host
95
     *
96
     * @return $this
97
     */
98
    public function setHost($host)
99
    {
100
        return $this->setParam('host', $host);
101
    }
102
103
    /**
104
     * @return string|null Host
105
     */
106
    public function getProxy()
107
    {
108
        return $this->hasParam('proxy') ? $this->getParam('proxy') : null;
109
    }
110
111
    /**
112
     * Set proxy for http connections. Null is for environmental proxy,
113
     * empty string to disable proxy and proxy string to set actual http proxy.
114
     *
115
     * @see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTPROXY
116
     *
117
     * @param string|null $proxy
118
     *
119
     * @return $this
120
     */
121
    public function setProxy($proxy)
122
    {
123
        return $this->setParam('proxy', $proxy);
124
    }
125
126
    /**
127
     * @return string|array
128
     */
129
    public function getTransport()
130
    {
131
        return $this->hasParam('transport') ? $this->getParam('transport') : self::DEFAULT_TRANSPORT;
132
    }
133
134
    /**
135
     * @param string|array $transport
136
     *
137
     * @return $this
138
     */
139
    public function setTransport($transport)
140
    {
141
        return $this->setParam('transport', $transport);
142
    }
143
144
    /**
145
     * @return bool
146
     */
147
    public function hasCompression()
148
    {
149
        return (bool) $this->hasParam('compression') ? $this->getParam('compression') : self::DEFAULT_COMPRESSION;
150
    }
151
152
    /**
153
     * @param bool $compression
154
     *
155
     * @return $this
156
     */
157
    public function setCompression($compression = null)
158
    {
159
        return $this->setParam('compression', $compression);
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public function getPath()
166
    {
167
        return $this->hasParam('path') ? $this->getParam('path') : '';
168
    }
169
170
    /**
171
     * @param string $path
172
     *
173
     * @return $this
174
     */
175
    public function setPath($path)
176
    {
177
        return $this->setParam('path', $path);
178
    }
179
180
    /**
181
     * @param int $timeout Timeout in seconds
182
     *
183
     * @return $this
184
     */
185
    public function setTimeout($timeout)
186
    {
187
        return $this->setParam('timeout', $timeout);
188
    }
189
190
    /**
191
     * @return int Connection timeout in seconds
192
     */
193
    public function getTimeout()
194
    {
195
        return (int) $this->hasParam('timeout') ? $this->getParam('timeout') : self::TIMEOUT;
196
    }
197
198
    /**
199
     * Number of seconds after a connection timeout occurs for every request during the connection phase.
200
     * Use a small value if you need a fast fail in case of dead, unresponsive or unreachable servers (~5 sec).
201
     *
202
     * Set to zero to switch to the default built-in connection timeout (300 seconds in curl).
203
     *
204
     * @see http://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html
205
     *
206
     * @param int $timeout Connect timeout in seconds
207
     *
208
     * @return $this
209
     */
210
    public function setConnectTimeout($timeout)
211
    {
212
        return $this->setParam('connectTimeout', $timeout);
213
    }
214
215
    /**
216
     * @return int Connection timeout in seconds
217
     */
218
    public function getConnectTimeout()
219
    {
220
        return (int) $this->hasParam('connectTimeout') ? $this->getParam('connectTimeout') : self::CONNECT_TIMEOUT;
221
    }
222
223
    /**
224
     * Enables a connection.
225
     *
226
     * @param bool $enabled OPTIONAL (default = true)
227
     *
228
     * @return $this
229
     */
230
    public function setEnabled($enabled = true)
231
    {
232
        return $this->setParam('enabled', $enabled);
233
    }
234
235
    /**
236
     * @return bool True if enabled
237
     */
238
    public function isEnabled()
239
    {
240
        return (bool) $this->getParam('enabled');
241
    }
242
243
    /**
244
     * Returns an instance of the transport type.
245
     *
246
     * @throws \Elastica\Exception\InvalidException If invalid transport type
247
     *
248
     * @return \Elastica\Transport\AbstractTransport Transport object
249
     */
250
    public function getTransportObject()
251
    {
252
        $transport = $this->getTransport();
253
254
        return AbstractTransport::create($transport, $this);
255
    }
256
257
    /**
258
     * @return bool Returns true if connection is persistent. True by default
259
     */
260
    public function isPersistent()
261
    {
262
        return (bool) $this->hasParam('persistent') ? $this->getParam('persistent') : true;
263
    }
264
265
    /**
266
     * @param array $config
267
     *
268
     * @return $this
269
     */
270
    public function setConfig(array $config)
271
    {
272
        return $this->setParam('config', $config);
273
    }
274
275
    /**
276
     * @param string $key
277
     * @param mixed  $value
278
     *
279
     * @return $this
280
     */
281
    public function addConfig($key, $value)
282
    {
283
        $this->_params['config'][$key] = $value;
284
285
        return $this;
286
    }
287
288
    /**
289
     * @param string $key
290
     *
291
     * @return bool
292
     */
293
    public function hasConfig($key)
294
    {
295
        $config = $this->getConfig();
296
297
        return isset($config[$key]);
298
    }
299
300
    /**
301
     * Returns a specific config key or the whole
302
     * config array if not set.
303
     *
304
     * @param string $key Config key
305
     *
306
     * @throws \Elastica\Exception\InvalidException
307
     *
308
     * @return array|string Config value
309
     */
310
    public function getConfig($key = '')
311
    {
312
        $config = $this->getParam('config');
313
        if (empty($key)) {
314
            return $config;
315
        }
316
317
        if (!array_key_exists($key, $config)) {
318
            throw new InvalidException('Config key is not set: '.$key);
319
        }
320
321
        return $config[$key];
322
    }
323
324
    /**
325
     * @param \Elastica\Connection|array $params Params to create a connection
326
     *
327
     * @throws Exception\InvalidException
328
     *
329
     * @return self
330
     */
331 View Code Duplication
    public static function create($params = [])
0 ignored issues
show
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...
332
    {
333
        if (is_array($params)) {
334
            return new self($params);
335
        }
336
337
        if ($params instanceof self) {
338
            return $params;
339
        }
340
341
        throw new InvalidException('Invalid data type');
342
    }
343
344
    /**
345
     * @return string User
346
     */
347
    public function getUsername()
348
    {
349
        return $this->hasParam('username') ? $this->getParam('username') : null;
350
    }
351
352
    /**
353
     * @return string Password
354
     */
355
    public function getPassword()
356
    {
357
        return $this->hasParam('password') ? $this->getParam('password') : null;
358
    }
359
}
360