You are here

public function Subscription::isActive in Acquia Connector 3.x

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

Helper function to check if the site has an active subscription.

File

src/Subscription.php, line 118

Class

Subscription
Storage class for Acquia Subscriptions.

Namespace

Drupal\acquia_connector

Code

public function isActive() {
  $this
    ->initialiseCacheBin();
  $active = FALSE;

  // Subscription cannot be active if we have no credentials.
  if (self::hasCredentials()) {
    if ($cache = $this->cache
      ->get('acquia_connector.subscription_data')) {
      if (is_array($cache->data) && $cache->expire > time()) {
        return !empty($cache->data['active']);
      }
    }
    $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,
        ]);
        $this->cache
          ->set('acquia_connector.subscription_data', $subscription, time() + 60 * 60);
      } catch (ConnectorException $e) {
      }
    }
    $active = !empty($subscription['active']);
  }
  return $active;
}