You are here

protected function ClientManager::setConnection in Acquia Content Hub 8

Function returns content hub client.

Parameters

array $config: Configuration array.

Return value

\Acquia\ContentHubClient\ContentHub Returns the Content Hub Client

Throws

\Drupal\acquia_contenthub\ContentHubException Throws exception when cannot connect to Content Hub.

1 call to ClientManager::setConnection()
ClientManager::__construct in src/Client/ClientManager.php
ClientManager constructor.

File

src/Client/ClientManager.php, line 121

Class

ClientManager
Provides a service for managing pending server tasks.

Namespace

Drupal\acquia_contenthub\Client

Code

protected function setConnection(array $config = []) {
  $this->client =& drupal_static(__METHOD__);
  if (NULL === $this->client) {
    $hostname = $this->config
      ->get('hostname');

    // Override configuration.
    $config = array_merge([
      'base_url' => $hostname,
      'client-user-agent' => $this->clientUserAgent,
      'adapterConfig' => [
        'schemaId' => 'Drupal8',
        'defaultLanguageId' => $this->languageManager
          ->getDefaultLanguage()
          ->getId(),
      ],
    ], $config);

    // Get API information.
    $api = $this->config
      ->get('api_key');
    $secret = $this->config
      ->get('secret_key');
    $client_name = $this->config
      ->get('client_name');
    $origin = $this->config
      ->get('origin');

    // If any of these variables is empty, then we do NOT have a valid
    // connection.
    if (!Uuid::isValid($origin) || empty($client_name) || empty($hostname) || empty($api) || empty($secret)) {
      return FALSE;
    }
    $this->client = new ContentHub($origin, $this->collector
      ->getMiddlewares(), $config);
  }
  return $this;
}