You are here

public function AcsfMessage::send in Acquia Cloud Site Factory Connector 8.2

Same name and namespace in other branches
  1. 8 src/AcsfMessage.php \Drupal\acsf\AcsfMessage::send()

Sends the message to the remote server.

File

src/AcsfMessage.php, line 110

Class

AcsfMessage
AcsfMessage.

Namespace

Drupal\acsf

Code

public function send() {
  if (function_exists('is_acquia_host') && !is_acquia_host()) {
    return;
  }
  $this->response = $this
    ->sendMessage($this->config
    ->getUrl(), $this->method, $this->endpoint, $this->parameters, $this->config
    ->getUsername(), $this->config
    ->getPassword());

  // Don't allow empty responses.
  if (empty($this->response)) {
    throw new AcsfMessageEmptyResponseException(sprintf('The message to %s resulted in an empty response.', $this->endpoint));
  }

  // Only allow AcsfMessageResponse compatible responses.
  if (!is_subclass_of($this->response, '\\Drupal\\acsf\\AcsfMessageResponse')) {
    throw new AcsfMessageMalformedResponseException(sprintf('The message to %s resulted in a malformed response. It should be an AcsfMessageResponse object.', $this->endpoint));
  }

  // If the response failed, throw an exception.
  if ($this->response
    ->failed()) {

    // The REST API returns error descriptions in the "message" field of the
    // response body.
    if (!empty($this->response->body['message'])) {
      $error_message = sprintf('The request to %s failed with HTTP error: %s %s.', $this->endpoint, $this->response->code, $this->response->body['message']);
    }
    else {
      $error_message = sprintf('The request to %s failed.', $this->endpoint);
    }
    throw new AcsfMessageFailedResponseException($error_message);
  }

  // Allow the implementer to respond right away.
  $this
    ->receiveResponse($this->response);

  // Allow an anonymous callback.
  if (!empty($this->callback)) {
    try {
      $callback = $this->callback;
      $callback($this->response);
    } catch (\Exception $e) {

      // @todo log here?
    }
  }
}