You are here

public function Subscription::update in Acquia Connector 8

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

Get subscription status from Acquia, and store the result.

This check also sends a heartbeat to Acquia unless $params['no_heartbeat'] == 1.

Parameters

array $params: Optional parameters for \Drupal\acquia_connector\Client::getSubscription.

Return value

mixed FALSE, integer (error number), or subscription data.

File

src/Subscription.php, line 46

Class

Subscription
Storage class for Acquia Subscriptions.

Namespace

Drupal\acquia_connector

Code

public function update(array $params = []) {
  $config = \Drupal::configFactory()
    ->getEditable('acquia_connector.settings');
  $current_subscription = \Drupal::state()
    ->get('acquia_subscription_data');
  $subscription = FALSE;
  if (!self::hasCredentials()) {

    // If there is not an identifier or key, delete any old subscription data.
    \Drupal::state()
      ->delete('acquia_subscription_data');
    \Drupal::state()
      ->set('acquia_subscription_data', [
      'active' => FALSE,
    ]);
    \Drupal::state()
      ->delete('spi.site_name');
    \Drupal::state()
      ->delete('spi.site_machine_name');
  }
  else {

    // Get our subscription data.
    try {
      $storage = new Storage();
      $key = $storage
        ->getKey();
      $identifier = $storage
        ->getIdentifier();
      $subscription = \Drupal::service('acquia_connector.client')
        ->getSubscription($identifier, $key, $params);
    } catch (ConnectorException $e) {
      switch ($e
        ->getCustomMessage('code')) {
        case self::NOT_FOUND:
        case self::EXPIRED:

          // Fall through since these values are stored and used by
          // acquia_search_acquia_subscription_status()
          $subscription = $e
            ->getCustomMessage('code');
          break;
        default:

          // Likely server error (503) or connection timeout (-110) so leave
          // current subscription in place. _acquia_agent_request() logged an
          // error message.
          return $current_subscription;
      }
    }
    if ($subscription && $subscription != $current_subscription) {
      \Drupal::state()
        ->set('acquia_subscription_data', $subscription);
    }
  }
  return $subscription;
}