You are here

public function ClientFactory::getClient in Acquia Content Hub 8.2

Instantiates the content hub client.

Return value

\Acquia\ContentHubClient\ContentHubClient|bool The ContentHub Client

1 call to ClientFactory::getClient()
ClientFactory::authenticate in src/Client/ClientFactory.php
Makes a call to get a client response based on the client name.
1 method overrides ClientFactory::getClient()
ClientFactoryMock::getClient in tests/modules/acquia_contenthub_server_test/src/Client/ClientFactoryMock.php
Instantiates the content hub client.

File

src/Client/ClientFactory.php, line 151

Class

ClientFactory
Instantiates an Acquia ContentHub Client object.

Namespace

Drupal\acquia_contenthub\Client

Code

public function getClient(Settings $settings = NULL) {
  $validate = (bool) (!$settings);
  if (!$settings) {
    if (isset($this->client)) {
      return $this->client;
    }
    $settings = $this
      ->getSettings();
  }
  if (!$this
    ->isConfigurationSet($settings)) {
    return FALSE;
  }

  // Override configuration.
  $languages_ids = array_keys(\Drupal::languageManager()
    ->getLanguages());
  array_push($languages_ids, ClientCDFObject::LANGUAGE_UNDETERMINED);
  $config = [
    'base_url' => $settings
      ->getUrl(),
    'client-languages' => $languages_ids,
    'client-user-agent' => $this
      ->getClientUserAgent(),
  ];
  $this->client = new ContentHubClient($config, $this->loggerFactory
    ->get('acquia_contenthub'), $settings, $settings
    ->getMiddleware(), $this->dispatcher);
  $send_update = $this->config
    ->get('send_contenthub_updates') ?? TRUE;

  // Only send Client CDF updates, if send update flag is TRUE.
  if ($validate && $send_update && $this->client
    ->getRemoteSettings()) {
    $event = new BuildClientCdfEvent(ClientCDFObject::create($settings
      ->getUuid(), [
      'settings' => $settings
        ->toArray(),
    ]));
    $this->dispatcher
      ->dispatch(AcquiaContentHubEvents::BUILD_CLIENT_CDF, $event);
    $this->clientCDFObject = $event
      ->getCdf();
    $this
      ->updateClientCdf();
  }
  return $this->client;
}