You are here

public function SalesforceSoapPartner::trySoap in Salesforce Suite 7.3

Wrap SF SOAP API resources in a try-catch so that we can automatically re-auth when we have an invalid session id / access token.

Parameters

string $function: The name of the SforcePartnerClient function to attempt.

mixed arg1, arg2, arg3...: Parameters to pass through to $function

Return value

the results of $function

See also

SforcePartnerClient()

File

modules/salesforce_soap/salesforce_soap.inc, line 88
Contains SalesforceSoapPartner.

Class

SalesforceSoapPartner
Expose the partner SOAP API by extending SforcePartnerClient and configuring it with the OAUTH credentials and endpoints from the Salesforce API class.

Code

public function trySoap($function) {
  $args = func_get_args();
  array_shift($args);
  try {
    $results = call_user_func_array(array(
      $this,
      $function,
    ), $args);

    // If returned without exceptions, reset the refreshed flag.
    $this->refreshed = FALSE;
    return $results;
  } catch (SoapFault $e) {

    // sf:INVALID_SESSION_ID is thrown on expired login (and other reasons).
    // Our only recourse is to try refreshing our auth token. If we get any
    // other exception, bubble it up.
    if ($e->faultcode != 'sf:INVALID_SESSION_ID') {
      throw $e;
    }
    if (!$this->refreshed) {

      // If we got an invalid session exception, try to refresh the auth
      // token through REST API. The "refreshed" flag will make sure we retry
      // only once.
      $this->refreshed = TRUE;
      $this->salesforceApi
        ->refreshToken();
      return $this
        ->trySoap($function, $args);
    }

    // If we've already tried a refresh, this refresh token is probably
    // invalid. Kill it, log, and bubble the exception.
    $this
      ->setConnected(FALSE);
    variable_del('salesforce_refresh_token');
    $link = l(t('the Salesforce configuration page'), 'admin/config/services/salesforce');
    $message = t('Your website is not authorized to connect with Salesforce. Please visit !config to authorize your site.', array(
      '!config' => $link,
    ));
    salesforce_set_message($message, $error, FALSE);
    watchdog_exception('salesforce_soap', $e, t('Salesforce SOAP %function failed. Your website is not authorized to connect with Salesforce.'), array(
      '%function' => $function,
    ), WATCHDOG_ERROR, $link);
    throw $e;
  }
}