You are here

public function Rest::processItem in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php \Drupal\salesforce_push\Plugin\SalesforcePushQueueProcessor\Rest::processItem()
  2. 5.0.x modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php \Drupal\salesforce_push\Plugin\SalesforcePushQueueProcessor\Rest::processItem()

Push queue item process callback.

Parameters

object $item: The push queue item.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to Rest::processItem()
Rest::process in modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php
Process push queue items.

File

modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php, line 141

Class

Rest
Rest queue processor plugin.

Namespace

Drupal\salesforce_push\Plugin\SalesforcePushQueueProcessor

Code

public function processItem(\stdClass $item) {

  // Allow exceptions to bubble up for PushQueue to sort things out.
  $mapping = $this->mappingStorage
    ->load($item->name);
  $mapped_object = $this
    ->getMappedObject($item, $mapping);
  if ($mapped_object
    ->isNew() && $item->op == MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_DELETE) {

    // If mapped object doesn't exist or fails to load for this delete, this
    // item can be considered successfully processed.
    return;
  }

  // @TODO: the following is nearly identical to the end of salesforce_push_entity_crud(). Can we DRY it? Do we care?
  try {
    $this->eventDispatcher
      ->dispatch(SalesforceEvents::PUSH_MAPPING_OBJECT, new SalesforcePushOpEvent($mapped_object, $item->op));

    // If this is a delete, destroy the SF object and we're done.
    if ($item->op == MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_DELETE) {
      $mapped_object
        ->pushDelete();

      // This has to be cleaned up here because we need the object to process
      // Async.
      $mapped_object
        ->delete();
    }
    else {
      $entity = $this->etm
        ->getStorage($mapping->drupal_entity_type)
        ->load($item->entity_id);
      if ($entity === NULL) {

        // Bubble this up also.
        throw new EntityNotFoundException($item->entity_id, $mapping->drupal_entity_type);
      }

      // Push to SF. This also saves the mapped object.
      $mapped_object
        ->setDrupalEntity($entity)
        ->push();
    }
  } catch (\Exception $e) {
    $this->eventDispatcher
      ->dispatch(SalesforceEvents::PUSH_FAIL, new SalesforcePushOpEvent($mapped_object, $item->op));

    // Log errors and throw exception to cause this item to be re-queued.
    if (!$mapped_object
      ->isNew()) {

      // Only update existing mapped objects.
      $mapped_object
        ->set('last_sync_action', $item->op)
        ->set('last_sync_status', FALSE)
        ->set('revision_log_message', $e
        ->getMessage())
        ->save();
    }
    throw $e;
  }
}