View source
<?php
namespace Drupal\salesforce_pull;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\salesforce\Event\SalesforceEvents;
use Drupal\salesforce\Event\SalesforceWarningEvent;
use Drupal\salesforce\Event\SalesforceErrorEvent;
use Drupal\salesforce\Event\SalesforceNoticeEvent;
use Drupal\salesforce\Rest\RestClientInterface;
use Drupal\salesforce\SFID;
use Drupal\salesforce_mapping\Event\SalesforceDeleteAllowedEvent;
use Drupal\salesforce_mapping\MappingConstants;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class DeleteHandler {
protected $sfapi;
protected $mappingStorage;
protected $mappedObjectStorage;
protected $etm;
protected $state;
protected $time;
protected $eventDispatcher;
public function __construct(RestClientInterface $sfapi, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, EventDispatcherInterface $event_dispatcher) {
$this->sfapi = $sfapi;
$this->etm = $entity_type_manager;
$this->mappingStorage = $this->etm
->getStorage('salesforce_mapping');
$this->mappedObjectStorage = $this->etm
->getStorage('salesforce_mapped_object');
$this->state = $state;
$this->eventDispatcher = $event_dispatcher;
}
public function processDeletedRecords() {
$pull_info = $this->state
->get('salesforce.mapping_pull_info', []);
foreach ($this->mappingStorage
->loadMultiple() as $mapping) {
if (!$mapping
->checkTriggers([
MappingConstants::SALESFORCE_MAPPING_SYNC_SF_DELETE,
])) {
continue;
}
$last_delete_sync = !empty($pull_info[$mapping
->id()]['last_delete_timestamp']) ? $pull_info[$mapping
->id()]['last_delete_timestamp'] : strtotime('-29 days');
$now = time();
$now = $now > $last_delete_sync + 60 ? $now : $now + 60;
if ($last_delete_sync < strtotime('-29 days')) {
$last_delete_sync = strtotime('-29 days');
}
$last_delete_sync_sf = gmdate('Y-m-d\\TH:i:s\\Z', $last_delete_sync);
$now_sf = gmdate('Y-m-d\\TH:i:s\\Z', $now);
$deleted = $this->sfapi
->getDeleted($mapping
->getSalesforceObjectType(), $last_delete_sync_sf, $now_sf);
$this
->handleDeletedRecords($deleted, $mapping
->getSalesforceObjectType());
$pull_info[$mapping
->id()]['last_delete_timestamp'] = $now;
$this->state
->set('salesforce.mapping_pull_info', $pull_info);
}
return TRUE;
}
protected function handleDeletedRecords(array $deleted, $type) {
if (empty($deleted['deletedRecords'])) {
return;
}
$sf_mappings = $this->mappingStorage
->loadByProperties([
'salesforce_object_type' => $type,
]);
if (empty($sf_mappings)) {
return;
}
foreach ($deleted['deletedRecords'] as $record) {
$this
->handleDeletedRecord($record, $type);
}
}
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;
}
$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));
return;
}
if (!$sf_mapping
->checkTriggers([
MappingConstants::SALESFORCE_MAPPING_SYNC_SF_DELETE,
])) {
return;
}
$deleteAllowedEvent = new SalesforceDeleteAllowedEvent($mapped_object);
$this->eventDispatcher
->dispatch(SalesforceEvents::DELETE_ALLOWED, $deleteAllowedEvent);
if ($deleteAllowedEvent
->isDeleteAllowed() === FALSE) {
return;
}
try {
$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));
return;
}
$mapped_object
->delete();
}
}
}