1 | <?php |
||
28 | class Client extends BaseClient |
||
29 | { |
||
30 | /** |
||
31 | * Stores created indexes to avoid recreation. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $indexCache = []; |
||
36 | |||
37 | /** |
||
38 | * Stores created index template to avoid recreation. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $indexTemplateCache = []; |
||
43 | |||
44 | /** |
||
45 | * Symfony's debugging Stopwatch. |
||
46 | * |
||
47 | * @var Stopwatch|null |
||
48 | */ |
||
49 | private $stopwatch; |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 13 | public function request(string $path, string $method = Request::GET, $data = [], array $query = [], string $contentType = Request::DEFAULT_CONTENT_TYPE): Response |
|
55 | { |
||
56 | 13 | if ($this->stopwatch) { |
|
57 | 11 | $this->stopwatch->start('es_request', 'fos_elastica'); |
|
58 | } |
||
59 | |||
60 | 13 | $response = parent::request($path, $method, $data, $query, $contentType); |
|
61 | 13 | $responseData = $response->getData(); |
|
62 | |||
63 | 13 | $transportInfo = $response->getTransferInfo(); |
|
64 | 13 | $connection = $this->getLastRequest()->getConnection(); |
|
65 | 13 | $forbiddenHttpCodes = $connection->hasConfig('http_error_codes') ? $connection->getConfig('http_error_codes') : []; |
|
66 | |||
67 | 13 | if (isset($transportInfo['http_code']) && \in_array($transportInfo['http_code'], $forbiddenHttpCodes, true)) { |
|
68 | 1 | $body = \is_array($responseData) ? \json_encode($responseData) : $responseData; |
|
69 | 1 | $message = \sprintf('Error in transportInfo: response code is %s, response body is %s', $transportInfo['http_code'], $body); |
|
70 | 1 | throw new ClientException($message); |
|
71 | } |
||
72 | |||
73 | 12 | if (isset($responseData['took'], $responseData['hits'])) { |
|
74 | 2 | $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), $response->getEngineTime(), $responseData['hits']['total']['value'] ?? 0); |
|
75 | } else { |
||
76 | 11 | $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), 0, 0); |
|
77 | } |
||
78 | |||
79 | 12 | if ($this->stopwatch) { |
|
80 | 11 | $this->stopwatch->stop('es_request'); |
|
81 | } |
||
82 | |||
83 | 12 | return $response; |
|
84 | } |
||
85 | |||
86 | 13 | public function getIndex(string $name): BaseIndex |
|
94 | |||
95 | 6 | public function getIndexTemplate($name): IndexTemplate |
|
103 | |||
104 | /** |
||
105 | * Sets a stopwatch instance for debugging purposes. |
||
106 | */ |
||
107 | 15 | public function setStopwatch(?Stopwatch $stopwatch = null): void |
|
111 | |||
112 | /** |
||
113 | * Log the query if we have an instance of ElasticaLogger. |
||
114 | * |
||
115 | * @param array|string $data |
||
116 | * @param int $queryTime |
||
117 | * @param int $engineMS |
||
118 | */ |
||
119 | 12 | private function logQuery(string $path, string $method, $data, array $query, $queryTime, $engineMS = 0, int $itemCount = 0): void |
|
136 | } |
||
137 |