Subscription.php in Acquia Connector 3.x
File
src/Subscription.php
View source
<?php
namespace Drupal\acquia_connector;
use Drupal\acquia_connector\Helper\Storage;
class Subscription {
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;
const MESSAGE_LIFETIME = 900;
protected $cache;
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()) {
\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 {
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:
$subscription = $e
->getCustomMessage('code');
break;
default:
return $current_subscription;
}
}
if ($subscription && $subscription != $current_subscription) {
\Drupal::state()
->set('acquia_subscription_data', $subscription);
}
}
return $subscription;
}
public function hasCredentials() {
$storage = new Storage();
return $storage
->getIdentifier() && $storage
->getKey();
}
protected function initialiseCacheBin() {
if (empty($this->cache)) {
$cache = \Drupal::cache();
$this->cache = $cache;
}
}
public function isActive() {
$this
->initialiseCacheBin();
$active = FALSE;
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');
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;
}
}