You are here

class DESConnector in Elasticsearch Connector 8

Drupal Elasticsearch Interface.

@package Drupal\elasticsearch_connector

Hierarchy

Expanded class hierarchy of DESConnector

File

src/DESConnector/DESConnector.php, line 19
Provides Elasticsearch Client for Drupal's Elasticsearch Connector module.

Namespace

Drupal\elasticsearch_connector\DESConnector
View source
class DESConnector implements DESConnectorInterface {
  const CLUSTER_STATUS_GREEN = 'green';
  const CLUSTER_STATUS_YELLOW = 'yellow';
  const CLUSTER_STATUS_RED = 'red';
  protected static $instances;
  protected $client;

  /**
   * Singleton constructor.
   */
  private function __construct($client) {

    // TODO: Validate if we have a valid client.
    $this->client = $client;
  }

  /**
   * Singleton clone.
   */
  private function __clone() {
  }

  /**
   * Singleton wakeup.
   */
  private function __wakeup() {
  }

  /**
   * Singleton sleep.
   */
  private function __sleep() {
  }

  /**
   * Magic method call.
   */
  public function __call($name, $arguments) {
    if (method_exists($this
      ->getClient(), $name)) {
      return call_user_func_array(array(
        $this
          ->getClient(),
        $name,
      ), $arguments);
    }
  }

  /**
   * Initializes the needed client.
   *
   * TODO: We need to check the available options for the ClientBuilder
   *       and set them after the alter hook.
   *
   * @param array $hosts
   *   The URLs of the Elasticsearch hosts.
   *
   * @return Client
   */
  public static function getInstance(array $hosts) {
    if (ELASTICSEARCH_CONNECTOR_VERSION < 2) {
      return DESConnector81::getInstance($hosts);
    }
    else {
      return DESConnector82::getInstance($hosts);
    }
  }

  /**
   * @return mixed
   */
  public function getCluster() {
    return $this
      ->getClient()
      ->cluster();
  }

  /**
   * @return mixed
   */
  public function getNodes() {
    return $this
      ->getClient()
      ->nodes();
  }

  /**
   * @return mixed
   */
  public function getIndices() {
    return $this
      ->getClient()
      ->indices();
  }

  /**
   * @return mixed
   */
  public function bulk(array $params) {
    return $this
      ->getClient()
      ->bulk($params);
  }

  /**
   * @return mixed
   */
  protected function getClient() {
    return $this->client;
  }

  /**
   * Check if we have a connection the cluster.
   *
   * @return bool
   */
  public function getClusterStatus() {
    try {
      $health = $this
        ->getCluster()
        ->health();
      return $health['status'];
    } catch (Exception $e) {
      return FALSE;
    }
  }

  /**
   * Check if we have a connection the cluster.
   *
   * @return bool
   */
  public function clusterIsOk() {
    try {
      $health = $this
        ->getCluster()
        ->health();
      if (in_array($health['status'], array(
        self::CLUSTER_STATUS_GREEN,
        self::CLUSTER_STATUS_YELLOW,
      ))) {
        $status = TRUE;
      }
      else {
        $status = FALSE;
      }
    } catch (Exception $e) {
      $status = FALSE;
    }
    return $status;
  }

  /**
   * Get cluster health.
   *
   * @return array
   */
  public function getClusterHealth() {
    return $this
      ->getCluster()
      ->health();
  }

  /**
   * Get cluster state.
   *
   * @return array
   */
  public function getClusterState() {
    return $this
      ->getCluster()
      ->state();
  }

  /**
   * Get cluster stats.
   *
   * @return array
   */
  public function getClusterStats() {
    return $this
      ->getCluster()
      ->stats();
  }

  /**
   * Return cluster info.
   *
   * @return array
   *   Info array.
   *
   * @throws \Exception
   *   Exception().
   */
  public function getClusterInfo() {
    $result = FALSE;
    try {
      try {
        $result['state'] = $this
          ->getClusterState();
        $result['health'] = $this
          ->getClusterHealth();
        $result['stats'] = $this
          ->getClusterStats();
      } catch (\Exception $e) {

        // TODO: Do not set messages or log messages into the abstraction.
        drupal_set_message($e
          ->getMessage(), 'error');
      }
    } catch (\Exception $e) {
      throw $e;
    }
    return $result;
  }

  /**
   * Get nodes stats.
   *
   * @return array
   */
  public function getNodesStats() {
    return $this
      ->getNodes()
      ->stats();
  }

  /**
   * Get nodes info.
   *
   * @return array
   */
  public function getNodesInfo() {
    return $this
      ->getNodes()
      ->info();
  }

  /**
   * Return nodes info.
   *
   * @return array
   *   Info array.
   *
   * @throws \Exception
   *   Exception().
   */
  public function getNodesProperties() {
    $result = FALSE;
    try {
      try {
        $result['stats'] = $this
          ->getNodesStats();
        $result['info'] = $this
          ->getNodesInfo();
      } catch (\Exception $e) {

        // TODO: Do not set messages or log messages into the abstraction.
        drupal_set_message($e
          ->getMessage(), 'error');
      }
    } catch (\Exception $e) {
      throw $e;
    }
    return $result;
  }

  /**
   * Elasticsearch info function.
   *
   * @return array
   */
  public function info() {
    return $this
      ->getClient()
      ->info();
  }

  /**
   * Elasticsearch ping function.
   *
   * @return array
   */
  public function ping() {
    return $this
      ->getClient()
      ->ping();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DESConnector::$client protected property 2
DESConnector::$instances protected static property 2
DESConnector::bulk public function
DESConnector::clusterIsOk public function Check if we have a connection the cluster.
DESConnector::CLUSTER_STATUS_GREEN constant 2
DESConnector::CLUSTER_STATUS_RED constant 2
DESConnector::CLUSTER_STATUS_YELLOW constant 2
DESConnector::getClient protected function
DESConnector::getCluster public function
DESConnector::getClusterHealth public function Get cluster health.
DESConnector::getClusterInfo public function Return cluster info.
DESConnector::getClusterState public function Get cluster state.
DESConnector::getClusterStats public function Get cluster stats.
DESConnector::getClusterStatus public function Check if we have a connection the cluster.
DESConnector::getIndices public function
DESConnector::getInstance public static function Initializes the needed client. Overrides DESConnectorInterface::getInstance 2
DESConnector::getNodes public function
DESConnector::getNodesInfo public function Get nodes info.
DESConnector::getNodesProperties public function Return nodes info.
DESConnector::getNodesStats public function Get nodes stats.
DESConnector::info public function Elasticsearch info function.
DESConnector::ping public function Elasticsearch ping function.
DESConnector::__call public function Magic method call.
DESConnector::__clone private function Singleton clone. 2
DESConnector::__construct private function Singleton constructor. 2
DESConnector::__sleep private function Singleton sleep. 2
DESConnector::__wakeup private function Singleton wakeup. 2