You are here

public static function AcquiaLiftAPI::getInstance in Acquia Lift Connector 7.2

Same name and namespace in other branches
  1. 7 includes/acquia_lift.classes.inc \AcquiaLiftAPI::getInstance()

Singleton factory method.

Return value

AcquiaLiftAPI

14 calls to AcquiaLiftAPI::getInstance()
AcquiaLiftLearn::getAPIInstance in plugins/agent_types/AcquiaLiftLearn.inc
Implements AcquiaLiftLearningAgentInterface::getAPIInstance().
AcquiaLiftTest::getAcquiaLiftAPI in tests/AcquiaLiftAPI.test
Returns a AcquiaLiftAPI instance that can be used to test methods.
AcquiaLiftTest::testEnsureUniqueAgentName in tests/AcquiaLiftAPI.test
AcquiaLiftTest::testGetInstance in tests/AcquiaLiftAPI.test
Tests getting a AcquiaLiftAPI instance with invalid and valid credentials.
AcquiaLiftTest::testGetInstanceWithAPIUrl in tests/AcquiaLiftAPI.test
Tests getting a AcquiaLiftAPI instance that uses the default API url.

... See full list

File

includes/AcquiaLiftAPI.inc, line 154

Class

AcquiaLiftAPI

Code

public static function getInstance($account_info) {
  if (empty(self::$instance)) {
    if (drupal_valid_test_ua()) {
      $broken = variable_get('acquia_lift_web_test_broken_client', FALSE);
      self::setTestInstance($broken);
      return self::$instance;
    }
    if (empty($account_info['api_url']) || !valid_url($account_info['api_url'])) {
      throw new AcquiaLiftCredsException('Acquia Lift API URL is missing or not a valid URL.');
    }
    $api_url = $account_info['api_url'];
    $needs_scheme = strpos($api_url, '://') === FALSE;
    if ($needs_scheme) {
      global $is_https;

      // Use the same scheme for Acquia Lift as we are using here.
      $url_scheme = $is_https ? 'https://' : 'http://';
      $api_url = $url_scheme . $api_url;
    }
    if (substr($api_url, -1) === '/') {
      $api_url = substr($api_url, 0, -1);
    }
    $acquia_lift_version = "undefined";
    if (!empty($account_info['public_key'])) {
      $public_key = $account_info['public_key'];
    }
    else {
      throw new AcquiaLiftCredsException('Acquia Lift Public Key was not found.');
    }
    if (!empty($account_info['private_key'])) {
      $private_key = $account_info['private_key'];

      // Additionally, decode the private key. According to the HMAC Spec 2.0 it is
      // transferred as base64 decoded string but we need to make sure we decode it before
      // using it.
      $private_key = base64_decode($private_key);

      // Validate if the secret key is a valid base64 encoded key.
      if ($private_key === FALSE) {
        throw new AcquiaLiftCredsException(t('secret key could not be decoded as base64. Please validate your credentials or contact Acquia Support.'));
      }

      // Do not continue if the decoded version is empty. Very unlikely that this will ever occur.
      if (empty($private_key)) {
        throw new AcquiaLiftCredsException(t('secret key cannot be empty. Please validate your credentials or contact Acquia Support.'));
      }
    }
    else {
      throw new AcquiaLiftCredsException('Acquia Lift Private Key was not found.');
    }
    if (!empty($account_info['acquia_lift_version'])) {
      $acquia_lift_version = $account_info['acquia_lift_version'];
    }
    $validate_response = $account_info['validate_response'];
    $lift_profiles_account_name = isset($account_info['profiles']['account_name']) ? $account_info['profiles']['account_name'] : '';

    // @todo Add integration with Acquia Customer Auth here. Can only happen
    // after Acquia Decision Service has integrated with Acquia Customer Auth and
    // customer auth has become public. This will add additional security as
    // we then can self-generate public and private keys per customer. They
    // will also be able to expire easily.
    self::$instance = new self($api_url, $public_key, $private_key, $acquia_lift_version, $validate_response, $lift_profiles_account_name);
  }
  return self::$instance;
}