You are here

public function EntitySaveHandler::execute in Services Client 7.2

Execute event and send event to remove endpoint.

Return value

ServicesClientEventResult

Overrides EventHandler::execute

File

include/event.inc, line 935

Class

EntitySaveHandler
General entity save handler.

Code

public function execute() {

  // Load mapped object.
  $object = $this
    ->getMappedObject();

  // Set services client control data.
  $control = $this
    ->getControlData();
  $control
    ->setData($object);
  $this
    ->beforeSync($object);

  // Allow other modules to alter mapping.
  drupal_alter('services_client_mapped_object', $this, $object);

  // Create action result.
  $result = new ServicesClientEventResult();
  $result
    ->setHandler($this);
  $result->event = $this
    ->getEvent();
  $result->object = $object;
  $result->entity = $this
    ->getEntity();
  $result->entity_type = $result->event->entity_type;

  // Allow other modules to react on before request.
  module_invoke_all('services_client_before_request', $this, $object);
  if ($control
    ->isLooping()) {
    $this
      ->log(ServicesClientLogLevel::ERROR, "LOOP; entity_type: @type, entity_id: @id, event: @event", array(
      '@type' => $this->event->entity_type,
      '@id' => $this
        ->getEntityId(),
      '@event' => $this->event->name,
    ), WATCHDOG_ERROR);

    // Mark error as loop
    $result->error_type = ServicesClientErrorType::LOOP;
  }
  else {
    $this
      ->log(ServicesClientLogLevel::INFO, "SENDING; connection : @connection, event : @event, entity_type : @entity_type, entity_id : @entity_id, uuid : @uuid, object : <pre>@object</pre>", array(
      '@event' => $this->event->name,
      '@connection' => $this
        ->getConnectionId(),
      '@entity_type' => $this->event->entity_type,
      '@entity_id' => $this
        ->getEntityId(),
      '@uuid' => isset($this
        ->getEntity()->uuid) ? $this
        ->getEntity()->uuid : NULL,
      '@object' => $this
        ->debugObject($object),
    ));
    try {
      $result->response = $this
        ->doSync($object);
      $result->request = $this
        ->getConnection()
        ->getRequest();
    } catch (ServicesClientConnectionResponseException $e) {
      $e
        ->log();
      $result->error_message = $e
        ->getServicesMessage();
      $result->error_code = $e
        ->getErrorCode();
      $result->request = $e->request;
      $result->response = $e->response;

      // Determien what error type, by default we assume remote server failed.
      $error_type = ServicesClientErrorType::REMOTE_SERVER;

      // Logic errors that came from remote site, like can't login
      if ($e
        ->getErrorCode() >= 400 && $e
        ->getErrorCode() < 500) {
        $error_type = ServicesClientErrorType::REMOTE_LOGIC;
      }
      elseif ($e
        ->getErrorCode() < 100) {
        $error_type = ServicesClientErrorType::NETWORK;
      }

      // Set error type
      $result->error_type = $error_type;
    } catch (Exception $e) {
      $result->error_message = $e
        ->getMessage();
      $result->error_type = ServicesClientErrorType::UNKNOWN;
    }
  }
  $this
    ->logErrorResult($result);
  $this
    ->afterSync($object, $result);
  $this
    ->log(ServicesClientLogLevel::DEVEL, "RESULT; <pre>@result</pre>", array(
    '@result' => print_r($result, TRUE),
  ));

  // Allow other modules to react on after request.
  module_invoke_all('services_client_after_request', $this, $object, $result);
  return $result;
}