vendor/friendsofsymfony/elastica-bundle/src/Elastica/Client.php line 63

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSElasticaBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\ElasticaBundle\Elastica;
  11. use Elastica\Client as BaseClient;
  12. use Elastica\Exception\ClientException;
  13. use Elastica\Index as BaseIndex;
  14. use Elastica\Request;
  15. use Elastica\Response;
  16. use FOS\ElasticaBundle\Logger\ElasticaLogger;
  17. use Symfony\Component\Stopwatch\Stopwatch;
  18. /**
  19.  * Extends the default Elastica client to provide logging for errors that occur
  20.  * during communication with ElasticSearch.
  21.  *
  22.  * @author Gordon Franke <info@nevalon.de>
  23.  */
  24. class Client extends BaseClient
  25. {
  26.     /**
  27.      * Stores created indexes to avoid recreation.
  28.      *
  29.      * @var array<string, BaseIndex>
  30.      */
  31.     private $indexCache = [];
  32.     /**
  33.      * Stores created index template to avoid recreation.
  34.      *
  35.      * @var array<string, IndexTemplate>
  36.      */
  37.     private $indexTemplateCache = [];
  38.     /**
  39.      * Symfony's debugging Stopwatch.
  40.      *
  41.      * @var Stopwatch|null
  42.      */
  43.     private $stopwatch;
  44.     /**
  45.      * {@inheritdoc}
  46.      *
  47.      * @param array<mixed> $data
  48.      * @param array<mixed> $query
  49.      */
  50.     public function request(string $pathstring $method Request::GET$data = [], array $query = [], string $contentType Request::DEFAULT_CONTENT_TYPE): Response
  51.     {
  52.         if ($this->stopwatch) {
  53.             $this->stopwatch->start('es_request''fos_elastica');
  54.         }
  55.         $response parent::request($path$method$data$query$contentType);
  56.         $responseData $response->getData();
  57.         $transportInfo $response->getTransferInfo();
  58.         $connection $this->getLastRequest()->getConnection();
  59.         $forbiddenHttpCodes $connection->hasConfig('http_error_codes') ? $connection->getConfig('http_error_codes') : [];
  60.         if (isset($transportInfo['http_code']) && \in_array($transportInfo['http_code'], $forbiddenHttpCodestrue)) {
  61.             $body \json_encode($responseData);
  62.             $message \sprintf('Error in transportInfo: response code is %s, response body is %s'$transportInfo['http_code'], $body);
  63.             throw new ClientException($message);
  64.         }
  65.         if (isset($responseData['took'], $responseData['hits'])) {
  66.             $this->logQuery($path$method$data$query$response->getQueryTime(), $response->getEngineTime(), $responseData['hits']['total']['value'] ?? 0);
  67.         } else {
  68.             $this->logQuery($path$method$data$query$response->getQueryTime(), 00);
  69.         }
  70.         if ($this->stopwatch) {
  71.             $this->stopwatch->stop('es_request');
  72.         }
  73.         return $response;
  74.     }
  75.     public function getIndex(string $name): BaseIndex
  76.     {
  77.         // TODO PHP >= 7.4 ??=
  78.         return $this->indexCache[$name] ?? ($this->indexCache[$name] = new Index($this$name));
  79.     }
  80.     /**
  81.      * @param string $name
  82.      */
  83.     public function getIndexTemplate($name): IndexTemplate
  84.     {
  85.         // TODO PHP >= 7.4 ??=
  86.         return $this->indexTemplateCache[$name] ?? ($this->indexTemplateCache[$name] = new IndexTemplate($this$name));
  87.     }
  88.     /**
  89.      * Sets a stopwatch instance for debugging purposes.
  90.      */
  91.     public function setStopwatch(?Stopwatch $stopwatch null): void
  92.     {
  93.         $this->stopwatch $stopwatch;
  94.     }
  95.     /**
  96.      * Log the query if we have an instance of ElasticaLogger.
  97.      *
  98.      * @param array<mixed>|string $data
  99.      * @param array<mixed>        $query
  100.      * @param float               $queryTime
  101.      * @param int                 $engineMS
  102.      */
  103.     private function logQuery(string $pathstring $method$data, array $query$queryTime$engineMS 0int $itemCount 0): void
  104.     {
  105.         if (!$this->_logger instanceof ElasticaLogger) {
  106.             return;
  107.         }
  108.         $connection $this->getLastRequest()->getConnection();
  109.         $connectionArray = [
  110.             'host' => $connection->getHost(),
  111.             'port' => $connection->getPort(),
  112.             'transport' => $connection->getTransport(),
  113.             'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : [],
  114.         ];
  115.         $this->_logger->logQuery($path$method$data$queryTime$connectionArray$query$engineMS$itemCount);
  116.     }
  117. }