You are here

public function ClientManager::createRequest in Acquia Content Hub 8

Makes an API Call Request to Acquia Content Hub, with exception handling.

It handles generic exceptions and allows for text overrides.

Parameters

string $request: The name of the request.

array $args: The arguments to pass to the request.

array $exception_messages: The exception messages to overwrite.

Return value

bool|mixed The return value of the request if succeeds, FALSE otherwise.

Overrides ClientManagerInterface::createRequest

1 call to ClientManager::createRequest()
ClientManager::isClientNameAvailable in src/Client/ClientManager.php
Checks whether the client name given is available in this Subscription.

File

src/Client/ClientManager.php, line 286

Class

ClientManager
Provides a service for managing pending server tasks.

Namespace

Drupal\acquia_contenthub\Client

Code

public function createRequest($request, array $args = [], array $exception_messages = []) {
  try {

    // Check that we have a valid connection.
    if (empty($this
      ->getConnection())) {
      $error = $this
        ->t('This client is NOT registered to Content Hub. Please register first');
      throw new \Exception($error);
    }

    // Process each individual request.
    switch ($request) {

      // Case for all API calls with no arguments that do NOT require
      // authentication.
      case 'ping':
      case 'definition':
        return $this
          ->getConnection()
          ->{$request}();

      // Case for all API calls with no argument that require authentication.
      case 'getSettings':
      case 'purge':
      case 'restore':
      case 'reindex':
      case 'mapping':
      case 'regenerateSharedSecret':
        return $this->client
          ->{$request}();

      // Case for all API calls with 1 argument.
      case 'register':
      case 'getClientByName':
      case 'createEntity':
      case 'createEntities':
      case 'putEntities':
      case 'readEntity':
      case 'readEntities':
      case 'updateEntities':
      case 'deleteEntity':
      case 'listEntities':
      case 'addWebhook':
      case 'deleteWebhook':

      // This request only requires one argument (webhook_uuid), but we
      // are using the second one to pass the webhook_url.
      case 'searchEntity':
        if (!isset($args[0])) {
          $error = $this
            ->t('Request %request requires %num argument.', [
            '%request' => $request,
            '%num' => 1,
          ]);
          throw new \Exception($error);
        }
        return $this->client
          ->{$request}($args[0]);

      // Case for all API calls with 2 arguments.
      case 'logs':
      case 'updateEntity':
        if (!isset($args[0]) || !isset($args[1])) {
          $error = $this
            ->t('Request %request requires %num arguments.', [
            '%request' => $request,
            '%num' => 2,
          ]);
          throw new \Exception($error);
        }
        return $this->client
          ->{$request}($args[0], $args[1]);
    }
  } catch (ServerException $ex) {
    $msg = $this
      ->getExceptionMessage($request, $args, $ex, $exception_messages);
  } catch (ConnectException $ex) {
    $msg = $this
      ->getExceptionMessage($request, $args, $ex, $exception_messages);
  } catch (ClientException $ex) {
    $response = json_decode($ex
      ->getResponse()
      ->getBody(), TRUE);
    $msg = $this
      ->getExceptionMessage($request, $args, $ex, $exception_messages, $response);
  } catch (RequestException $ex) {
    $msg = $this
      ->getExceptionMessage($request, $args, $ex, $exception_messages);
  } catch (\Exception $ex) {
    $msg = $this
      ->getExceptionMessage($request, $args, $ex, $exception_messages);
  }

  // Now show and log the error message.
  if (isset($msg)) {
    if ($msg !== FALSE) {
      $this->loggerFactory
        ->get('acquia_contenthub')
        ->error($msg);

      // Throw $ex;.
    }
    else {

      // If the message is FALSE, then there is no error message, which
      // means the request was expecting an exception to be successful.
      return TRUE;
    }
  }
  return FALSE;
}