You are here

public function MappedObject::push in Salesforce Suite 8.3

Same name and namespace in other branches
  1. 8.4 modules/salesforce_mapping/src/Entity/MappedObject.php \Drupal\salesforce_mapping\Entity\MappedObject::push()
  2. 5.0.x modules/salesforce_mapping/src/Entity/MappedObject.php \Drupal\salesforce_mapping\Entity\MappedObject::push()

Push data to Salesforce.

Return value

mixed SFID or NULL depending on result from SF.

Overrides MappedObjectInterface::push

File

modules/salesforce_mapping/src/Entity/MappedObject.php, line 373

Class

MappedObject
Defines a Salesforce Mapped Object entity class.

Namespace

Drupal\salesforce_mapping\Entity

Code

public function push() {

  // @TODO need error handling, logging, and hook invocations within this function, where we can provide full context, or short of that clear documentation on how callers should handle errors and exceptions. At the very least, we need to make sure to include $params in some kind of exception if we're not going to handle it inside this function.
  $mapping = $this
    ->getMapping();
  $drupal_entity = $this
    ->getMappedEntity();

  // Previously hook_salesforce_push_params_alter.
  $params = new PushParams($mapping, $drupal_entity);
  $this
    ->eventDispatcher()
    ->dispatch(SalesforceEvents::PUSH_PARAMS, new SalesforcePushParamsEvent($this, $params));

  // @TODO is this the right place for this logic to live?
  // Cases:
  // 1. Already mapped to an existing Salesforce object + always_upsert not
  //    set?  Update.
  // 2. Not mapped, but upsert key is defined?  Upsert.
  // 3. Not mapped & no upsert key?  Create.
  $result = FALSE;
  $action = '';
  if ($this
    ->sfid() && !$mapping
    ->alwaysUpsert()) {
    $action = 'update';
    $result = $this
      ->client()
      ->objectUpdate($mapping
      ->getSalesforceObjectType(), $this
      ->sfid(), $params
      ->getParams());
  }
  elseif ($mapping
    ->hasKey()) {
    $action = 'upsert';
    $result = $this
      ->client()
      ->objectUpsert($mapping
      ->getSalesforceObjectType(), $mapping
      ->getKeyField(), $mapping
      ->getKeyValue($drupal_entity), $params
      ->getParams());
  }
  else {
    $action = 'create';
    $result = $this
      ->client()
      ->objectCreate($mapping
      ->getSalesforceObjectType(), $params
      ->getParams());
  }
  if ($drupal_entity instanceof EntityChangedInterface) {
    $this
      ->set('entity_updated', $drupal_entity
      ->getChangedTime());
  }

  // @TODO: catch EntityStorageException ? Others ?
  if ($result instanceof SFID) {
    $this
      ->set('salesforce_id', (string) $result);
  }

  // @TODO setNewRevision not chainable, per https://www.drupal.org/node/2839075
  $this
    ->setNewRevision(TRUE);
  $this
    ->set('last_sync_action', 'push_' . $action)
    ->set('last_sync_status', TRUE)
    ->set('revision_log_message', '')
    ->save();

  // Previously hook_salesforce_push_success.
  $this
    ->eventDispatcher()
    ->dispatch(SalesforceEvents::PUSH_SUCCESS, new SalesforcePushParamsEvent($this, $params));
  return $result;
}