You are here

public function ClientManager::getClientForCluster in Elasticsearch Connector 8.6

Same name and namespace in other branches
  1. 8.7 src/ElasticSearch/ClientManager.php \Drupal\elasticsearch_connector\ElasticSearch\ClientManager::getClientForCluster()
  2. 8.2 src/ElasticSearch/ClientManager.php \Drupal\elasticsearch_connector\ElasticSearch\ClientManager::getClientForCluster()
  3. 8.5 src/ElasticSearch/ClientManager.php \Drupal\elasticsearch_connector\ElasticSearch\ClientManager::getClientForCluster()

Get a client to interact with the given Elasticsearch cluster.

Parameters

\Drupal\elasticsearch_connector\Entity\Cluster $cluster: Cluster to get a client for.

Return value

\nodespark\DESConnector\ClientInterface Client object to interact with the given cluster.

Overrides ClientManagerInterface::getClientForCluster

File

src/ElasticSearch/ClientManager.php, line 46

Class

ClientManager
Class ClientManager.

Namespace

Drupal\elasticsearch_connector\ElasticSearch

Code

public function getClientForCluster(Cluster $cluster) {
  $hosts = [
    [
      'url' => $cluster->url,
      'options' => $cluster->options,
    ],
  ];
  $hash = json_encode($hosts);
  if (!isset($this->clients[$hash])) {
    $options = [
      'hosts' => [
        $cluster
          ->getRawUrl(),
      ],
      'options' => [],
      'curl' => [
        CURLOPT_CONNECTTIMEOUT => !empty($cluster->options['timeout']) ? $cluster->options['timeout'] : Cluster::ELASTICSEARCH_CONNECTOR_DEFAULT_TIMEOUT,
      ],
    ];
    if ($cluster->options['use_authentication']) {
      $options['auth'] = [
        $cluster->url => [
          'username' => $cluster->options['username'],
          'password' => $cluster->options['password'],
          'method' => $cluster->options['authentication_type'],
        ],
      ];
    }
    $this->moduleHandler
      ->alter('elasticsearch_connector_load_library_options', $options, $cluster);
    $this->clients[$hash] = $this->clientManagerFactory
      ->create($options);
  }
  return $this->clients[$hash];
}