Completed
Push — 1.x ( 747b4c...b6bf38 )
by Adrian
04:52 queued 11s
created

Client::request()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 2
b 0
f 0
nc 7
nop 2
dl 0
loc 25
rs 9.7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Manticoresearch;
6
7
use Manticoresearch\Connection\ConnectionPool;
8
use Manticoresearch\Connection\Strategy\SelectorInterface;
9
use Manticoresearch\Connection\Strategy\StaticRoundRobin;
10
11
use Manticoresearch\Endpoints\Pq;
12
use Manticoresearch\Exceptions\ConnectionException;
13
use Manticoresearch\Exceptions\NoMoreNodesException;
14
use Manticoresearch\Exceptions\RuntimeException;
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\NullLogger;
17
18
/**
19
 * Manticore  client object
20
 * @package Manticoresearch
21
 * @category Manticoresearch
22
 * @author Adrian Nuta <[email protected]>
23
 * @link https://manticoresearch.com
24
 */
25
class Client
26
{
27
    /**
28
     *
29
     */
30
    const VERSION = '1.0.0';
31
32
    /**
33
     * @var array
34
     */
35
    protected $config = [];
36
    /**
37
     * @var string
38
     */
39
    private $connectionStrategy = StaticRoundRobin::class;
40
    /**
41
     * @var ConnectionPool
42
     */
43
    protected $connectionPool;
44
45
    /**
46
     * @var LoggerInterface|NullLogger
47
     */
48
    protected $logger;
49
50
    protected $lastResponse;
51
52
    /*
53
     * $config can be a connection array or
54
     * $config['connections] = array of connections
55
     * $config['connectionStrategy'] = class name of pool strategy
56
     */
57
    public function __construct($config = [], LoggerInterface $logger = null)
58
    {
59
        $this->setConfig($config);
60
        $this->logger = $logger ?? new NullLogger();
61
        $this->initConnections();
62
    }
63
64
    protected function initConnections()
65
    {
66
        $connections = [];
67
        if (isset($this->config['connections'])) {
68
            foreach ($this->config['connections'] as $connection) {
69
                if (is_array($connection)) {
70
                    $connections[] = Connection::create($connection);
71
                } else {
72
                    $connections[] = $connection;
73
                }
74
            }
75
        }
76
77
        if (empty($connections)) {
78
            $connections[] = Connection::create($this->config);
79
        }
80
        if (isset($this->config['connectionStrategy'])) {
81
            if (is_string($this->config['connectionStrategy'])) {
82
                $strategyName = '\\Manticoresearch\\Connection\\Strategy\\' . $this->config['connectionStrategy'];
83
                if (class_exists($strategyName)) {
84
                    $strategy = new $strategyName();
85
                } elseif (class_exists($this->config['connectionStrategy'])) {
86
                    $strategyName = $this->config['connectionStrategy'];
87
                    $strategy = new $strategyName();
88
                }
89
            } elseif ($this->config['connectionStrategy'] instanceof SelectorInterface) {
90
                $strategy = $this->config['connectionStrategy'];
91
            } else {
92
                throw new RuntimeException('Cannot create a strategy based on provided settings!');
93
            }
94
        } else {
95
            $strategy = new $this->connectionStrategy;
96
        }
97
        if (!isset($this->config['retries'])) {
98
            $this->config['retries'] = count($connections);
99
        }
100
        $this->connectionPool = new Connection\ConnectionPool(
101
            $connections,
102
            $strategy ?? new $this->connectionStrategy,
103
            $this->config['retries']
104
        );
105
    }
106
107
    /**
108
     * @param string|array $hosts
109
     */
110
    public function setHosts($hosts)
111
    {
112
        $this->config['connections'] = $hosts;
113
        $this->initConnections();
114
    }
115
116
    /**
117
     * @param array $config
118
     * @return $this
119
     */
120
    public function setConfig(array $config): self
121
    {
122
        $this->config = array_merge($this->config, $config);
123
        return $this;
124
    }
125
126
    /**
127
     * @param array $config
128
     * @return Client
129
     */
130
    public static function create($config): Client
131
    {
132
        return self::createFromArray($config);
133
    }
134
135
    /**
136
     * @param array $config
137
     * @return Client
138
     */
139
    public static function createFromArray($config): Client
140
    {
141
        return new self($config);
142
    }
143
144
    /**
145
     * @return mixed
146
     */
147
    public function getConnections()
148
    {
149
        return $this->connectionPool->getConnections();
150
    }
151
152
    /**
153
     * @return ConnectionPool
154
     */
155
    public function getConnectionPool(): ConnectionPool
156
    {
157
        return $this->connectionPool;
158
    }
159
160
    /**
161
     * Endpoint: search
162
     * @param array $params
163
     * @param bool $obj
164
     * @return array|Response
165
     */
166
    public function search(array $params = [], $obj = false)
167
    {
168
        $endpoint = new Endpoints\Search($params);
169
        $response = $this->request($endpoint);
170
        if ($obj === true) {
171
            return $response;
172
        } else {
173
            return $response->getResponse();
174
        }
175
    }
176
177
    /**
178
     * Endpoint: insert
179
     * @param array $params
180
     * @return array
181
     */
182
    public function insert(array $params = [])
183
    {
184
        $endpoint = new Endpoints\Insert($params);
185
        $response = $this->request($endpoint);
186
187
        return $response->getResponse();
188
    }
189
190
    /**
191
     * Endpoint: replace
192
     * @param array $params
193
     * @return mixed
194
     */
195
    public function replace(array $params = [])
196
    {
197
        $endpoint = new Endpoints\Replace($params);
198
        $response = $this->request($endpoint);
199
200
        return $response->getResponse();
201
    }
202
203
    /**
204
     * Endpoint: update
205
     * @param array $params
206
     * @return array
207
     */
208
    public function update(array $params = [])
209
    {
210
        $endpoint = new Endpoints\Update($params);
211
        $response = $this->request($endpoint);
212
213
        return $response->getResponse();
214
    }
215
216
    /**
217
     * Endpoint: sql
218
     * @param array $params
219
     * @return array
220
     */
221
    public function sql(array $params = [])
222
    {
223
        $endpoint = new Endpoints\Sql($params);
224
        if (isset($params['mode'])) {
225
            $endpoint->setMode($params['mode']);
226
            $response = $this->request($endpoint, ['responseClass' => 'Manticoresearch\\Response\\SqlToArray']);
227
        } else {
228
            $response = $this->request($endpoint);
229
        }
230
        return $response->getResponse();
231
    }
232
233
    /**
234
     * Endpoint: delete
235
     * @param array $params
236
     * @return array
237
     */
238
    public function delete(array $params = [])
239
    {
240
        $endpoint = new Endpoints\Delete($params);
241
        $response = $this->request($endpoint);
242
243
        return $response->getResponse();
244
    }
245
246
    /**
247
     * Endpoint: pq
248
     */
249
    public function pq(): Pq
250
    {
251
        return new Pq($this);
252
    }
253
254
    /**
255
     * Endpoint: indices
256
     */
257
    public function indices(): Indices
258
    {
259
        return new Indices($this);
260
    }
261
262
    /**
263
     * Endpoint: nodes
264
     */
265
    public function nodes(): Nodes
266
    {
267
        return new Nodes($this);
268
    }
269
270
    public function cluster(): Cluster
271
    {
272
        return new Cluster($this);
273
    }
274
275
    /**
276
     * Endpoint: bulk
277
     * @param array $params
278
     * @return array
279
     */
280
    public function bulk(array $params = [])
281
    {
282
        $endpoint = new Endpoints\Bulk($params);
283
        $response = $this->request($endpoint);
284
285
        return $response->getResponse();
286
    }
287
288
    /**
289
     * Endpoint: suggest
290
     * @param array $params
291
     * @return array
292
     */
293
    public function suggest(array $params = [])
294
    {
295
        $endpoint = new Endpoints\Suggest();
296
        $endpoint->setIndex($params['index']);
297
        $endpoint->setBody($params['body']);
298
        $response = $this->request($endpoint, ['responseClass' => 'Manticoresearch\\Response\\SqlToArray']);
299
        return $response->getResponse();
300
    }
301
302
    public function keywords(array $params = [])
303
    {
304
        $endpoint = new Endpoints\Keywords();
305
        $endpoint->setIndex($params['index']);
306
        $endpoint->setBody($params['body']);
307
        $response = $this->request($endpoint, ['responseClass' => 'Manticoresearch\\Response\\SqlToArray']);
308
        return $response->getResponse();
309
    }
310
311
    public function explainQuery(array $params = [])
312
    {
313
        $endpoint = new Endpoints\ExplainQuery();
314
        $endpoint->setIndex($params['index']);
315
        $endpoint->setBody($params['body']);
316
        $response = $this->request($endpoint, ['responseClass' => 'Manticoresearch\\Response\\SqlToArray']);
317
        return $response->getResponse();
318
    }
319
320
321
    /*
322
     * @return Response
323
     */
324
325
    public function request(Request $request, array $params = []): Response
326
    {
327
        try {
328
            $connection = $this->connectionPool->getConnection();
329
            $this->lastResponse = $connection->getTransportHandler($this->logger)->execute($request, $params);
0 ignored issues
show
introduced by
The method execute() does not exist on Manticoresearch\Transport. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

329
            $this->lastResponse = $connection->getTransportHandler($this->logger)->/** @scrutinizer ignore-call */ execute($request, $params);
Loading history...
330
        } catch (NoMoreNodesException $e) {
331
            $this->logger->error('Manticore Search Request out of retries:', [
332
                'exception' => $e->getMessage(),
333
                'request' => $request->toArray()
334
            ]);
335
            $this->initConnections();
336
            throw $e;
337
        } catch (ConnectionException $e) {
338
            $this->logger->warning('Manticore Search Request failed ' . $this->connectionPool->retries_attempts . ':', [
339
                'exception' => $e->getMessage(),
340
                'request' => $e->getRequest()->toArray()
341
            ]);
342
343
            if (isset($connection)) {
344
                $connection->mark(false);
345
            }
346
347
            return $this->request($request, $params);
348
        }
349
        return $this->lastResponse;
350
    }
351
352
    /*
353
 *
354
 * @return Response
355
 */
356
357
    public function getLastResponse(): Response
358
    {
359
        return $this->lastResponse;
360
    }
361
}
362