You are here

public function CommandHelper::setIndexServerCommand in Search API 8

Switches an index to another server.

Parameters

string $indexId: The ID of the index.

string $serverId: The ID of the index's new server.

Throws

\Drupal\search_api\ConsoleException If either the index or the server couldn't be loaded.

File

src/Utility/CommandHelper.php, line 618

Class

CommandHelper
Provides functionality to be used by CLI tools.

Namespace

Drupal\search_api\Utility

Code

public function setIndexServerCommand($indexId, $serverId) {

  // Fetch current index and server data.
  $index = $this
    ->loadIndexes([
    $indexId,
  ]);
  $server = $this
    ->loadServers([
    $serverId,
  ]);
  $index = reset($index);
  $server = reset($server);
  if (!$index) {
    throw new ConsoleException($this
      ->t('Invalid index ID "@index_id".', [
      '@index_id' => $indexId,
    ]));
  }
  if (!$server) {
    throw new ConsoleException($this
      ->t('Invalid server ID "@server_id".', [
      '@server_id' => $serverId,
    ]));
  }

  // Set the new server on the index.
  try {

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $this
      ->reloadEntityOverrideFree($index);
    $index
      ->setServer($server);
    $index
      ->save();
    $this->logger
      ->info($this
      ->t('Index @index has been set to use server @server and items have been queued for indexing.', [
      '@index' => $indexId,
      '@server' => $serverId,
    ]));
  } catch (EntityStorageException $e) {
    $this->logger
      ->warning($e
      ->getMessage());
    $this->logger
      ->warning($this
      ->t('There was an error setting index @index to use server @server, or this index is already configured to use this server.', [
      '@index' => $indexId,
      '@server' => $serverId,
    ]));
  }
}