You are here

protected function DeleteHandler::handleDeletedRecord in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 modules/salesforce_pull/src/DeleteHandler.php \Drupal\salesforce_pull\DeleteHandler::handleDeletedRecord()
  2. 5.0.x modules/salesforce_pull/src/DeleteHandler.php \Drupal\salesforce_pull\DeleteHandler::handleDeletedRecord()

Delete single mapped object.

Parameters

array $record: Record array.

string $type: Salesforce object type.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to DeleteHandler::handleDeletedRecord()
DeleteHandler::handleDeletedRecords in modules/salesforce_pull/src/DeleteHandler.php
Delete records.

File

modules/salesforce_pull/src/DeleteHandler.php, line 169

Class

DeleteHandler
Handles pull cron deletion of Drupal entities based onSF mapping settings.

Namespace

Drupal\salesforce_pull

Code

protected function handleDeletedRecord(array $record, $type) {
  $mapped_objects = $this->mappedObjectStorage
    ->loadBySfid(new SFID($record['id']));
  if (empty($mapped_objects)) {
    return;
  }
  foreach ($mapped_objects as $mapped_object) {
    $entity = $mapped_object
      ->getMappedEntity();
    if (!$entity) {
      $message = 'No entity found for ID %id associated with Salesforce Object ID: %sfid ';
      $args = [
        '%id' => $mapped_object->entity_id->value,
        '%sfid' => $record['id'],
      ];
      $this->eventDispatcher
        ->dispatch(SalesforceEvents::NOTICE, new SalesforceNoticeEvent(NULL, $message, $args));
      $mapped_object
        ->delete();
      return;
    }

    // The mapping entity is an Entity reference field on mapped object, so we
    // need to get the id value this way.
    $sf_mapping = $mapped_object
      ->getMapping();
    if (!$sf_mapping) {
      $message = 'No mapping exists for mapped object %id with Salesforce Object ID: %sfid';
      $args = [
        '%id' => $mapped_object
          ->id(),
        '%sfid' => $record['id'],
      ];
      $this->eventDispatcher
        ->dispatch(SalesforceEvents::WARNING, new SalesforceWarningEvent(NULL, $message, $args));

      // @TODO should we delete a mapped object whose parent mapping no longer exists? Feels like someone else's job.
      // $mapped_object->delete();
      return;
    }
    if (!$sf_mapping
      ->checkTriggers([
      MappingConstants::SALESFORCE_MAPPING_SYNC_SF_DELETE,
    ])) {
      return;
    }

    // Before attempting the final delete, give other modules a chance to disallow it.
    $deleteAllowedEvent = new SalesforceDeleteAllowedEvent($mapped_object);
    $this->eventDispatcher
      ->dispatch(SalesforceEvents::DELETE_ALLOWED, $deleteAllowedEvent);
    if ($deleteAllowedEvent
      ->isDeleteAllowed() === FALSE) {
      return;
    }
    try {

      // Flag this entity to avoid duplicate processing.
      $entity->salesforce_pull = TRUE;
      $entity
        ->delete();
      $message = 'Deleted entity %label with ID: %id associated with Salesforce Object ID: %sfid';
      $args = [
        '%label' => $entity
          ->label(),
        '%id' => $mapped_object->entity_id,
        '%sfid' => $record['id'],
      ];
      $this->eventDispatcher
        ->dispatch(SalesforceEvents::NOTICE, new SalesforceNoticeEvent(NULL, $message, $args));
    } catch (\Exception $e) {
      $this->eventDispatcher
        ->dispatch(SalesforceEvents::ERROR, new SalesforceErrorEvent($e));

      // If mapped entity couldn't be deleted, do not delete the mapped
      // object.
      return;
    }
    $mapped_object
      ->delete();
  }
}