You are here

function salesforce_push_entity_crud_mapping in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 modules/salesforce_push/salesforce_push.module \salesforce_push_entity_crud_mapping()
  2. 5.0.x modules/salesforce_push/salesforce_push.module \salesforce_push_entity_crud_mapping()

Helper method for salesforce_push_entity_crud()

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

string $op: The current CRUD operation.

\Drupal\salesforce_mapping\Entity\SalesforceMappingInterface $mapping: The mapping.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to salesforce_push_entity_crud_mapping()
salesforce_push_entity_crud in modules/salesforce_push/salesforce_push.module
Push entities to Salesforce.

File

modules/salesforce_push/salesforce_push.module, line 105
Push updates to Salesforce when a Drupal entity is updated.

Code

function salesforce_push_entity_crud_mapping(EntityInterface $entity, $op, SalesforceMappingInterface $mapping) {
  $mapped_object = NULL;

  // Look for existing mapped object or create a new one (except for deletes).
  $props = [
    'drupal_entity__target_type' => $entity
      ->getEntityTypeId(),
    'drupal_entity__target_id' => $entity
      ->id(),
    'salesforce_mapping' => $mapping
      ->id(),
  ];
  $mapped_objects = \Drupal::service('entity_type.manager')
    ->getStorage('salesforce_mapped_object')
    ->loadByProperties($props);
  if (empty($mapped_objects)) {

    // No mappings found.
    if ($op == MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_DELETE) {

      // If no existing mapping, and this is a delete, purge any entries from
      // push queue and we're done.
      \Drupal::service('queue.salesforce_push')
        ->setName($mapping
        ->id())
        ->deleteItemByEntity($entity);
      return;
    }
    $mapped_object = new MappedObject([]);
    $mapped_object->salesforce_mapping = $mapping;
    $mapped_object
      ->setDrupalEntity($entity);
  }
  else {

    // There should really only be one in this case, since we're loading on a
    // multi-field unique key, but loadByProperties returns an array.
    $mapped_object = current($mapped_objects);
  }

  // Event subscribers should call $event->disallowPush() to prevent push.
  $event = \Drupal::service('event_dispatcher')
    ->dispatch(SalesforceEvents::PUSH_ALLOWED, new SalesforcePushAllowedEvent($mapped_object, $op));
  if ($event
    ->isPushAllowed() === FALSE) {
    return;
  }

  // Enqueue async push if the mapping is configured to do so, and quit.
  if ($mapping->async) {
    try {
      salesforce_push_enqueue_async($entity, $mapping, $mapped_object, $op);
    } catch (\Exception $e) {
      \Drupal::service('event_dispatcher')
        ->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent($e));
    }
    return;
  }

  // Attempt real-time push. Enqueue async push on failure.
  try {
    \Drupal::service('event_dispatcher')
      ->dispatch(SalesforceEvents::PUSH_MAPPING_OBJECT, new SalesforcePushOpEvent($mapped_object, $op));

    // If this is a delete, destroy the SF object.
    if ($op == MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_DELETE) {
      $mapped_object
        ->pushDelete();
    }
    else {

      // Otherwise, push to SF. This also saves the mapped object.
      $mapped_object
        ->push();
    }

    // On success, delete any push queue items for this entity.
    \Drupal::service('queue.salesforce_push')
      ->setName($mapping
      ->id())
      ->deleteItemByEntity($entity);
  } catch (\Exception $e) {
    \Drupal::service('event_dispatcher')
      ->dispatch(SalesforceEvents::PUSH_FAIL, new SalesforcePushOpEvent($mapped_object, $op));
    \Drupal::service('event_dispatcher')
      ->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent($e));
    try {
      salesforce_push_enqueue_async($entity, $mapping, $mapped_object, $op);
    } catch (\Exception $e) {
      \Drupal::service('event_dispatcher')
        ->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent($e));
    }
    if (!$mapped_object
      ->isNew()) {

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