summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Connection.php
blob: b9d910c99241346613c2fa2a87172a474a254350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php

namespace Elastica;
use Elastica\Exception\InvalidException;
use Elastica\Transport\AbstractTransport;

/**
 * Elastica connection instance to an elasticasearch node
 *
 * @category Xodoa
 * @package  Elastica
 * @author   Nicolas Ruflin <spam@ruflin.com>
 */
class Connection extends Param
{
    /**
     * Default elastic search port
     */
    const DEFAULT_PORT = 9200;

    /**
     * Default host
     */
    const DEFAULT_HOST = 'localhost';

    /**
     * Default transport
     *
     * @var string
     */
    const DEFAULT_TRANSPORT = 'Http';

    /**
     * Number of seconds after a timeout occurs for every request
     * If using indexing of file large value necessary.
     */
    const TIMEOUT = 300;

    /**
     * Creates a new connection object. A connection is enabled by default
     *
     * @param array $params OPTIONAL Connection params: host, port, transport, timeout. All are optional
     */
    public function __construct(array $params = array())
    {
        $this->setParams($params);
        $this->setEnabled(true);

        // Set empty config param if not exists
        if (!$this->hasParam('config')) {
            $this->setParam('config', array());
        }
    }

    /**
     * @return int Server port
     */
    public function getPort()
    {
        return $this->hasParam('port')?$this->getParam('port'):self::DEFAULT_PORT;
    }

    /**
     * @param  int                 $port
     * @return \Elastica\Connection
     */
    public function setPort($port)
    {
        return $this->setParam('port', (int) $port);
    }

    /**
     * @return string Host
     */
    public function getHost()
    {
        return $this->hasParam('host')?$this->getParam('host'):self::DEFAULT_HOST;
    }

    /**
     * @param  string              $host
     * @return \Elastica\Connection
     */
    public function setHost($host)
    {
        return $this->setParam('host', $host);
    }

    /**
     * @return string|null Host
     */
    public function getProxy()
    {
        return $this->hasParam('proxy')?$this->getParam('proxy'):null;
    }

    /**
     * Set proxy for http connections. Null is for environmental proxy,
     * empty string to disable proxy and proxy string to set actual http proxy.
     *
     * @see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTPROXY
     * @param  string|null              $proxy
     * @return \Elastica\Connection
     */
    public function setProxy($proxy)
    {
        return $this->setParam('proxy', $proxy);
    }

    /**
     * @return string|array
     */
    public function getTransport()
    {
        return $this->hasParam('transport')?$this->getParam('transport'):self::DEFAULT_TRANSPORT;
    }

    /**
     * @param  string|array        $transport
     * @return \Elastica\Connection
     */
    public function setTransport($transport)
    {
        return $this->setParam('transport', $transport);
    }

    /**
     * @return string
     */
    public function getPath()
    {
        return $this->hasParam('path')?$this->getParam('path'):'';
    }

    /**
     * @param  string              $path
     * @return \Elastica\Connection
     */
    public function setPath($path)
    {
        return $this->setParam('path', $path);
    }

    /**
     * @param  int                 $timeout Timeout in seconds
     * @return \Elastica\Connection
     */
    public function setTimeout($timeout)
    {
        return $this->setParam('timeout', $timeout);
    }

    /**
     * @return int Connection timeout in seconds
     */
    public function getTimeout()
    {
        return (int) $this->hasParam('timeout')?$this->getParam('timeout'):self::TIMEOUT;
    }

    /**
     * Enables a connection
     *
     * @param  bool                $enabled OPTIONAL (default = true)
     * @return \Elastica\Connection
     */
    public function setEnabled($enabled = true)
    {
        return $this->setParam('enabled', $enabled);
    }

    /**
     * @return bool True if enabled
     */
    public function isEnabled()
    {
        return (bool) $this->getParam('enabled');
    }

    /**
     * Returns an instance of the transport type
     *
     * @return \Elastica\Transport\AbstractTransport Transport object
     * @throws \Elastica\Exception\InvalidException  If invalid transport type
     */
    public function getTransportObject()
    {
        $transport = $this->getTransport();

        return AbstractTransport::create($transport, $this);
    }

    /**
     * @return bool Returns true if connection is persistent. True by default
     */
    public function isPersistent()
    {
        return (bool) $this->hasParam('persistent')?$this->getParam('persistent'):true;
    }

    /**
     * @param  array               $config
     * @return \Elastica\Connection
     */
    public function setConfig(array $config)
    {
        return $this->setParam('config', $config);
    }

    /**
     * @param  string              $key
     * @param  mixed               $value
     * @return \Elastica\Connection
     */
    public function addConfig($key, $value)
    {
        $this->_params['config'][$key] = $value;

        return $this;
    }

    /**
     * @param  string $key
     * @return bool
     */
    public function hasConfig($key)
    {
        $config = $this->getConfig();

        return isset($config[$key]);
    }

    /**
     * Returns a specific config key or the whole
     * config array if not set
     *
     * @param  string                              $key Config key
     * @throws \Elastica\Exception\InvalidException
     * @return array|string                        Config value
     */
    public function getConfig($key = '')
    {
        $config = $this->getParam('config');
        if (empty($key)) {
            return $config;
        }

        if (!array_key_exists($key, $config)) {
            throw new InvalidException('Config key is not set: ' . $key);
        }

        return $config[$key];
    }

    /**
     * @param  \Elastica\Connection|array $params Params to create a connection
     * @throws Exception\InvalidException
     * @return \Elastica\Connection
     */
    public static function create($params = array())
    {
        $connection = null;

        if ($params instanceof Connection) {
            $connection = $params;
        } elseif (is_array($params)) {
            $connection = new Connection($params);
        } else {
            throw new InvalidException('Invalid data type');
        }

        return $connection;
    }
}