You are here

class SettingsHelper in Acquia Lift Connector 8.3

Same name and namespace in other branches
  1. 8.4 src/Service/Helper/SettingsHelper.php \Drupal\acquia_lift\Service\Helper\SettingsHelper
  2. 8 src/Service/Helper/SettingsHelper.php \Drupal\acquia_lift\Service\Helper\SettingsHelper

Defines the Settings Helper class.

Hierarchy

Expanded class hierarchy of SettingsHelper

5 files declare their use of SettingsHelper
acquia_lift.install in ./acquia_lift.install
AdminSettingsForm.php in src/Form/AdminSettingsForm.php
PageContext.php in src/Service/Context/PageContext.php
PathContext.php in src/Service/Context/PathContext.php
SettingsHelperTest.php in tests/src/Unit/Service/Helper/SettingsHelperTest.php

File

src/Service/Helper/SettingsHelper.php, line 13

Namespace

Drupal\acquia_lift\Service\Helper
View source
class SettingsHelper {

  /**
   * Default identity type's default value.
   */
  const DEFAULT_IDENTITY_TYPE_DEFAULT = 'email';

  /**
   * Is an invalid credential.
   *
   * @param array
   *   Credential settings array.
   * @return boolean
   *   True if is an invalid credential.
   */
  public static function isInvalidCredential($credential_settings) {
    if (SELF::isInvalidCredentialAccountId($credential_settings['account_id']) || SELF::isInvalidCredentialSiteId($credential_settings['site_id']) || SELF::isInvalidCredentialAssetsUrl($credential_settings['assets_url']) || isset($credential_settings['decision_api_url']) && SELF::isInvalidCredentialDecisionAPIUrl($credential_settings['decision_api_url']) || isset($credential_settings['oauth_url']) && SELF::isInvalidCredentialOauthUrl($credential_settings['oauth_url'])) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Is an invalid credential Account ID. Invalid if:
   *   1) Missing, or
   *   2) Not start with a letter and contain only alphanumerical characters.
   *
   * @param string
   *   Credential Account ID.
   * @return boolean
   *   True if is an invalid credential Account ID.
   */
  public static function isInvalidCredentialAccountId($account_id) {
    if (empty($account_id) || !preg_match('/^[a-zA-Z_][a-zA-Z\\d_]*$/', $account_id)) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Is an invalid credential Site ID. Invalid if:
   *   1) Missing, or
   *   2) Not alphanumerical.
   *
   * @param string
   *   Credential Site ID.
   * @return boolean
   *   True if is an invalid credential Site ID.
   */
  public static function isInvalidCredentialSiteId($site_id) {
    if (empty($site_id)) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Is an invalid credential Assets URL. Invalid if:
   *   1) Missing, or
   *   2) Not a valid URL.
   *
   * @param string
   *   Credential Assets URL.
   * @return boolean
   *   True if is an invalid credential Assets URL.
   */
  public static function isInvalidCredentialAssetsUrl($assets_url) {
    if (empty($assets_url) || !UrlHelper::isValid($assets_url)) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Is an invalid credential Decision API URL. Invalid if:
   *   1) Exist, and
   *   2) Not a valid URL.
   *
   * @param string
   *   Credential Decision API URL.
   * @return boolean
   *   True if is an invalid credential Decision API URL.
   */
  public static function isInvalidCredentialDecisionAPIUrl($decision_api_url) {
    if (!empty($decision_api_url) && !UrlHelper::isValid($decision_api_url)) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Is an invalid credential OAuth URL. Invalid if:
   *   1) Exist, and
   *   2) Not a valid URL.
   *
   * @param string
   *   Credential OAuth URL.
   * @return boolean
   *   True if is an invalid credential OAuth URL.
   */
  public static function isInvalidCredentialOauthUrl($oauth_url) {
    if (!empty($oauth_url) && !UrlHelper::isValid($oauth_url)) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Is a valid bootstrap mode.
   *
   * @param string
   *   Mode to compare
   * @return boolean
   *   True if valid, false otherwise.
   */
  public static function isValidBootstrapMode($test_mode) {
    $valid_modes = [
      'auto',
      'manual',
    ];
    return in_array($test_mode, $valid_modes);
  }

  /**
   * Is a valid content replacement mode.
   *
   * @param string
   *   Mode to compare
   * @return boolean
   *   True if valid, false otherwise.
   */
  public static function isValidContentReplacementMode($test_mode) {
    $valid_modes = [
      'trusted',
      'untrusted',
      'customized',
    ];
    return in_array($test_mode, $valid_modes);
  }

  /**
   * Returns the list of UDFs that can be mapped to.
   *
   * @param string $type
   *   The type of UDF field. Can be person, touch or event.
   * @return array
   *   An array of possible UDF metatag values for the given type.
   * @throws Exception
   *   An exception if the type given is not supported.
   */
  public static function getUdfLimitsForType($type = "person") {
    if ($type !== 'person' && $type !== 'touch' && $type !== 'event') {
      throw new Exception('This UDF Field type is not supported.');
    }
    $counts = [
      'person' => 50,
      'touch' => 20,
      'event' => 50,
    ];
    return $counts[$type];
  }

  /**
   * Ping URI.
   *
   * @param string $base_uri
   *   Base URI.
   * @param string $path
   *   Path to "ping" end point.
   * @return array
   *   Returns 'statusCode' and 'reasonPhrase' of the response.
   */
  public static function pingUri($base_uri, $path) {

    /** @var \Drupal\Core\Http\ClientFactory $clientFactory */
    $clientFactory = \Drupal::service('http_client_factory');
    $client = $clientFactory
      ->fromOptions([
      'base_uri' => $base_uri,
    ]);
    try {
      $response = $client
        ->get($path, [
        'http_errors' => false,
      ]);
    } catch (RequestException $e) {
      return [];
    }
    return [
      'statusCode' => $response
        ->getStatusCode(),
      'reasonPhrase' => $response
        ->getReasonPhrase(),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SettingsHelper::DEFAULT_IDENTITY_TYPE_DEFAULT constant Default identity type's default value.
SettingsHelper::getUdfLimitsForType public static function Returns the list of UDFs that can be mapped to.
SettingsHelper::isInvalidCredential public static function Is an invalid credential.
SettingsHelper::isInvalidCredentialAccountId public static function Is an invalid credential Account ID. Invalid if: 1) Missing, or 2) Not start with a letter and contain only alphanumerical characters.
SettingsHelper::isInvalidCredentialAssetsUrl public static function Is an invalid credential Assets URL. Invalid if: 1) Missing, or 2) Not a valid URL.
SettingsHelper::isInvalidCredentialDecisionAPIUrl public static function Is an invalid credential Decision API URL. Invalid if: 1) Exist, and 2) Not a valid URL.
SettingsHelper::isInvalidCredentialOauthUrl public static function Is an invalid credential OAuth URL. Invalid if: 1) Exist, and 2) Not a valid URL.
SettingsHelper::isInvalidCredentialSiteId public static function Is an invalid credential Site ID. Invalid if: 1) Missing, or 2) Not alphanumerical.
SettingsHelper::isValidBootstrapMode public static function Is a valid bootstrap mode.
SettingsHelper::isValidContentReplacementMode public static function Is a valid content replacement mode.
SettingsHelper::pingUri public static function Ping URI.