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