You are here

public function Client::getSubscription in Acquia Connector 8

Same name and namespace in other branches
  1. 8.2 src/Client.php \Drupal\acquia_connector\Client::getSubscription()
  2. 3.x src/Client.php \Drupal\acquia_connector\Client::getSubscription()

Get Acquia subscription from Acquia.

Parameters

string $id: Acquia Subscription ID.

string $key: Acquia Subscription key.

array $body: Optional.

Return value

array|false Acquia Subscription array or FALSE.

Throws

\Exception

File

src/Client.php, line 157

Class

Client
Acquia connector client.

Namespace

Drupal\acquia_connector

Code

public function getSubscription($id, $key, array $body = []) {
  $body['identifier'] = $id;

  // There is an identifier and key, so attempt communication.
  $subscription = [];
  $this->state
    ->set('acquia_subscription_data.timestamp', \Drupal::time()
    ->getRequestTime());

  // Include version number information.
  acquia_connector_load_versions();
  if (IS_ACQUIA_DRUPAL) {
    $body['version'] = ACQUIA_DRUPAL_VERSION;
    $body['series'] = ACQUIA_DRUPAL_SERIES;
    $body['branch'] = ACQUIA_DRUPAL_BRANCH;
    $body['revision'] = ACQUIA_DRUPAL_REVISION;
  }
  if ($search_info = $this
    ->getSearchModulesData()) {
    $body['search_version'] = $search_info;
  }
  try {
    $response = $this
      ->nspiCall('/agent-api/subscription', $body);
    if (!empty($response['result']['authenticator']) && $this
      ->validateResponse($key, $response['result'], $response['authenticator'])) {
      $subscription += $response['result']['body'];

      // Subscription activated.
      if (is_numeric($this->state
        ->get('acquia_subscription_data')) && is_array($response['result']['body'])) {
        $this->state
          ->set('acquia_subscription_data', $subscription);
      }
      return $subscription;
    }
  } catch (ConnectorException $e) {
    $this
      ->messenger()
      ->addError($this
      ->t('Error occurred while retrieving Acquia subscription information. See logs for details.'));
    if ($e
      ->isCustomized()) {
      $this
        ->getLogger('acquia connector')
        ->error($e
        ->getCustomMessage() . '. Response data: @data', [
        '@data' => json_encode($e
          ->getAllCustomMessages()),
      ]);
    }
    else {
      $this
        ->getLogger('acquia connector')
        ->error($e
        ->getMessage());
    }
    throw $e;
  }
  return FALSE;
}