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