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 | * Symfony's debugging Stopwatch. |
||
37 | * |
||
38 | * @var Stopwatch|null |
||
39 | */ |
||
40 | private $stopwatch; |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | 11 | public function request($path, $method = Request::GET, $data = [], array $query = [], $contentType = Request::DEFAULT_CONTENT_TYPE) |
|
46 | { |
||
47 | 11 | if ($this->stopwatch) { |
|
48 | 9 | $this->stopwatch->start('es_request', 'fos_elastica'); |
|
49 | } |
||
50 | |||
51 | 11 | $response = parent::request($path, $method, $data, $query, $contentType); |
|
52 | 11 | $responseData = $response->getData(); |
|
53 | |||
54 | 11 | $transportInfo = $response->getTransferInfo(); |
|
55 | 11 | $connection = $this->getLastRequest()->getConnection(); |
|
56 | 11 | $forbiddenHttpCodes = $connection->hasConfig('http_error_codes') ? $connection->getConfig('http_error_codes') : []; |
|
57 | |||
58 | 11 | if (isset($transportInfo['http_code']) && in_array($transportInfo['http_code'], $forbiddenHttpCodes, true)) { |
|
59 | 1 | $body = is_array($responseData) ? json_encode($responseData) : $responseData; |
|
60 | 1 | $message = sprintf('Error in transportInfo: response code is %s, response body is %s', $transportInfo['http_code'], $body); |
|
61 | 1 | throw new ClientException($message); |
|
62 | } |
||
63 | |||
64 | 10 | if (isset($responseData['took']) && isset($responseData['hits'])) { |
|
65 | 2 | $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), $response->getEngineTime(), $responseData['hits']['total']); |
|
66 | } else { |
||
67 | 9 | $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), 0, 0); |
|
68 | } |
||
69 | |||
70 | 10 | if ($this->stopwatch) { |
|
71 | 9 | $this->stopwatch->stop('es_request'); |
|
72 | } |
||
73 | |||
74 | 10 | return $response; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string $name |
||
79 | * |
||
80 | * @return Index|mixed |
||
81 | */ |
||
82 | 11 | public function getIndex($name) |
|
90 | |||
91 | /** |
||
92 | * Sets a stopwatch instance for debugging purposes. |
||
93 | * |
||
94 | * @param Stopwatch $stopwatch |
||
95 | */ |
||
96 | 12 | public function setStopwatch(Stopwatch $stopwatch = null) |
|
100 | |||
101 | /** |
||
102 | * Log the query if we have an instance of ElasticaLogger. |
||
103 | * |
||
104 | * @param string $path |
||
105 | * @param string $method |
||
106 | * @param array|string $data |
||
107 | * @param array $query |
||
108 | * @param int $queryTime |
||
109 | * @param int $engineMS |
||
110 | * @param int $itemCount |
||
111 | */ |
||
112 | 10 | private function logQuery($path, $method, $data, array $query, $queryTime, $engineMS = 0, $itemCount = 0) |
|
131 | } |
||
132 |