You are here

protected function PullBase::updateEntity in Salesforce Suite 8.3

Same name and namespace in other branches
  1. 8.4 modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php \Drupal\salesforce_pull\Plugin\QueueWorker\PullBase::updateEntity()
  2. 5.0.x modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php \Drupal\salesforce_pull\Plugin\QueueWorker\PullBase::updateEntity()

Update an existing Drupal entity.

Parameters

\Drupal\salesforce_mapping\Entity\SalesforceMappingInterface $mapping: Object of field maps.

\Drupal\salesforce_mapping\Entity\MappedObjectInterface $mapped_object: SF Mmapped object.

\Drupal\salesforce\SObject $sf_object: Current Salesforce record array.

bool $force_pull: If true, ignore entity and SF timestamps.

1 call to PullBase::updateEntity()
PullBase::processItem in modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php
Queue item process callback.

File

modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php, line 139

Class

PullBase
Provides base functionality for the Salesforce Pull Queue Workers.

Namespace

Drupal\salesforce_pull\Plugin\QueueWorker

Code

protected function updateEntity(SalesforceMappingInterface $mapping, MappedObjectInterface $mapped_object, SObject $sf_object, $force_pull = FALSE) {
  if (!$mapping
    ->checkTriggers([
    MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE,
  ])) {
    return;
  }
  try {
    $entity = $mapped_object
      ->getMappedEntity();
    if (!$entity) {
      $this->eventDispatcher
        ->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent(NULL, 'Drupal entity existed at one time for Salesforce object %sfobjectid, but does not currently exist.', [
        '%sfobjectid' => (string) $sf_object
          ->id(),
      ]));
      return;
    }

    // Flag this entity as having been processed. This does not persist,
    // but is used by salesforce_push to avoid duplicate processing.
    $entity->salesforce_pull = TRUE;
    $entity_updated = !empty($entity->changed->value) ? $entity->changed->value : $mapped_object
      ->getChanged();
    $pull_trigger_date = $sf_object
      ->field($mapping
      ->getPullTriggerDate());
    $sf_record_updated = strtotime($pull_trigger_date);
    $mapped_object
      ->setDrupalEntity($entity)
      ->setSalesforceRecord($sf_object);

    // Push upsert ID to SF object, if allowed and not set.
    if ($mapping
      ->hasKey() && $mapping
      ->checkTriggers([
      MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_CREATE,
      MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_UPDATE,
    ]) && $sf_object
      ->field($mapping
      ->getKeyField()) === NULL) {
      $params = new PushParams($mapping, $entity);
      $this->eventDispatcher
        ->dispatch(SalesforceEvents::PUSH_PARAMS, new SalesforcePushParamsEvent($mapped_object, $params));

      // Get just the key param and send that.
      $key_field = $mapping
        ->getKeyField();
      $key_param = [
        $key_field => $params
          ->getParam($key_field),
      ];
      $sent_id = $this
        ->sendEntityId($mapping
        ->getSalesforceObjectType(), $mapped_object
        ->sfid(), $key_param);
      if (!$sent_id) {
        throw new PullException();
      }
    }
    $event = $this->eventDispatcher
      ->dispatch(SalesforceEvents::PULL_PREPULL, new SalesforcePullEvent($mapped_object, MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE));
    if (!$event
      ->isPullAllowed()) {
      $this->eventDispatcher
        ->dispatch(SalesforceEvents::NOTICE, new SalesforceNoticeEvent(NULL, 'Pull was not allowed for %label with %sfid', [
        '%label' => $entity
          ->label(),
        '%sfid' => (string) $sf_object
          ->id(),
      ]));
      return FALSE;
    }
    if ($sf_record_updated > $entity_updated || $mapped_object->force_pull || $force_pull) {

      // Set fields values on the Drupal entity.
      $mapped_object
        ->pull();
      $this->eventDispatcher
        ->dispatch(SalesforceEvents::NOTICE, new SalesforceNoticeEvent(NULL, 'Updated entity %label associated with Salesforce Object ID: %sfid', [
        '%label' => $entity
          ->label(),
        '%sfid' => (string) $sf_object
          ->id(),
      ]));
      return MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE;
    }
  } catch (\Exception $e) {
    $this->eventDispatcher
      ->dispatch(SalesforceEvents::WARNING, new SalesforceErrorEvent($e, 'Failed to update entity %label from Salesforce object %sfobjectid.', [
      '%label' => isset($entity) ? $entity
        ->label() : "Unknown",
      '%sfobjectid' => (string) $sf_object
        ->id(),
    ]));

    // Throwing a new exception keeps current item in cron queue.
    throw $e;
  }
}