You are here

function salesforce_get_usage_requirements in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 salesforce.install \salesforce_get_usage_requirements()
  2. 5.0.x salesforce.install \salesforce_get_usage_requirements()

Return usage requirements array.

1 call to salesforce_get_usage_requirements()
salesforce_requirements in ./salesforce.install
Implements hook_requirements().

File

./salesforce.install, line 176
Salesforce install file.

Code

function salesforce_get_usage_requirements() {
  $requirements = [
    'title' => t('Salesforce usage'),
    'value' => t('API Limit Info'),
  ];
  $usage = FALSE;
  try {
    $usage = \Drupal::service('salesforce.client')
      ->getApiUsage();
  } catch (Exception $e) {

    // Noop.
  }
  if (empty($usage)) {

    // Missing usage information is not necessarily a problem.
    $requirements += [
      'severity' => REQUIREMENT_OK,
      'description' => t('Usage information unavailable'),
    ];
  }
  else {
    $usage = str_replace('api-usage=', '', $usage);
    list($usage, $limit) = explode('/', $usage, 2);
    $pct = 'N/A';
    if ($limit > 0) {
      $pct = $usage / $limit * 100.0;
    }
    $args = [
      '%usage' => number_format($usage),
      '%limit' => number_format($limit),
      '%pct' => number_format($pct, 2) . '%',
    ];
    $requirements += [
      'description' => t('Usage: %usage requests of %limit limit (%pct) in the past 24 hours.', $args),
      'severity' => $pct >= 100 ? REQUIREMENT_ERROR : ($pct >= 80 ? REQUIREMENT_WARNING : REQUIREMENT_OK),
    ];
  }
  return $requirements;
}