You are here

class Subscription in Acquia Connector 8

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

Storage class for Acquia Subscriptions.

@package Drupal\acquia_connector.

Hierarchy

Expanded class hierarchy of Subscription

10 files declare their use of Subscription
AcquiaConnectorModuleTest.php in tests/src/Functional/AcquiaConnectorModuleTest.php
acquia_connector.install in ./acquia_connector.install
Install, update, and uninstall functions for the Acquia Connector module.
acquia_connector.module in ./acquia_connector.module
Acquia Connector module.
acquia_search.install in acquia_search/acquia_search.install
Install, update, and uninstall functions for the Acquia Connector module.
acquia_search.module in acquia_search/acquia_search.module
Integration between Acquia Drupal and Acquia's hosted solr search service.

... See full list

File

src/Subscription.php, line 12

Namespace

Drupal\acquia_connector
View source
class Subscription {

  /**
   * Errors defined by Acquia.
   */
  const NOT_FOUND = 1000;
  const KEY_MISMATCH = 1100;
  const EXPIRED = 1200;
  const REPLAY_ATTACK = 1300;
  const KEY_NOT_FOUND = 1400;
  const MESSAGE_FUTURE = 1500;
  const MESSAGE_EXPIRED = 1600;
  const MESSAGE_INVALID = 1700;
  const VALIDATION_ERROR = 1800;
  const PROVISION_ERROR = 9000;

  /**
   * Subscription message lifetime defined by Acquia.
   */

  // 15 * 60.
  const MESSAGE_LIFETIME = 900;

  /**
   * Get subscription status from Acquia, and store the result.
   *
   * This check also sends a heartbeat to Acquia unless
   * $params['no_heartbeat'] == 1.
   *
   * @param array $params
   *   Optional parameters for \Drupal\acquia_connector\Client::getSubscription.
   *
   * @return mixed
   *   FALSE, integer (error number), or subscription data.
   */
  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;
  }

  /**
   * Helper function to check if an identifier and key exist.
   */
  public function hasCredentials() {
    $storage = new Storage();
    return $storage
      ->getIdentifier() && $storage
      ->getKey();
  }

  /**
   * Helper function to check if the site has an active subscription.
   */
  public function isActive() {
    $active = FALSE;

    // Subscription cannot be active if we have no credentials.
    if (self::hasCredentials()) {
      $config = \Drupal::config('acquia_connector.settings');
      $subscription = \Drupal::state()
        ->get('acquia_subscription_data');
      $subscription_timestamp = \Drupal::state()
        ->get('acquia_subscription_data.timestamp');

      // Make sure we have data at least once per day.
      if (isset($subscription_timestamp) && time() - $subscription_timestamp > 60 * 60 * 24) {
        try {
          $storage = new Storage();
          $key = $storage
            ->getKey();
          $identifier = $storage
            ->getIdentifier();
          $subscription = \Drupal::service('acquia_connector.client')
            ->getSubscription($identifier, $key, [
            'no_heartbeat' => 1,
          ]);
        } catch (ConnectorException $e) {
        }
      }
      $active = !empty($subscription['active']);
    }
    return $active;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Subscription::EXPIRED constant
Subscription::hasCredentials public function Helper function to check if an identifier and key exist.
Subscription::isActive public function Helper function to check if the site has an active subscription.
Subscription::KEY_MISMATCH constant
Subscription::KEY_NOT_FOUND constant
Subscription::MESSAGE_EXPIRED constant
Subscription::MESSAGE_FUTURE constant
Subscription::MESSAGE_INVALID constant
Subscription::MESSAGE_LIFETIME constant
Subscription::NOT_FOUND constant Errors defined by Acquia.
Subscription::PROVISION_ERROR constant
Subscription::REPLAY_ATTACK constant
Subscription::update public function Get subscription status from Acquia, and store the result.
Subscription::VALIDATION_ERROR constant