View source
<?php
namespace Drupal\salesforce_push\Plugin\SalesforcePushQueueProcessor;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Queue\SuspendQueueException;
use Drupal\salesforce\EntityNotFoundException;
use Drupal\salesforce\Event\SalesforceEvents;
use Drupal\salesforce\Rest\RestClientInterface;
use Drupal\salesforce_mapping\Entity\MappedObject;
use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
use Drupal\salesforce_mapping\Event\SalesforcePushOpEvent;
use Drupal\salesforce_mapping\MappingConstants;
use Drupal\salesforce_push\PushQueueInterface;
use Drupal\salesforce_push\PushQueueProcessorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Rest extends PluginBase implements PushQueueProcessorInterface {
protected $queue;
protected $client;
protected $mappingStorage;
protected $mappedObjectStorage;
protected $eventDispatcher;
protected $etm;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, PushQueueInterface $queue, RestClientInterface $client, EntityTypeManagerInterface $etm, EventDispatcherInterface $eventDispatcher) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->queue = $queue;
$this->client = $client;
$this->etm = $etm;
$this->mappingStorage = $etm
->getStorage('salesforce_mapping');
$this->mappedObjectStorage = $etm
->getStorage('salesforce_mapped_object');
$this->eventDispatcher = $eventDispatcher;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('queue.salesforce_push'), $container
->get('salesforce.client'), $container
->get('entity_type.manager'), $container
->get('event_dispatcher'));
}
public function process(array $items) {
if (!$this->client
->isAuthorized()) {
throw new SuspendQueueException('Salesforce client not authorized.');
}
foreach ($items as $item) {
try {
$this
->processItem($item);
$this->queue
->deleteItem($item);
} catch (\Exception $e) {
$this->queue
->failItem($e, $item);
}
}
}
public function processItem(\stdClass $item) {
$mapping = $this->mappingStorage
->load($item->name);
$mapped_object = $this
->getMappedObject($item, $mapping);
if ($mapped_object
->isNew() && $item->op == MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_DELETE) {
return;
}
try {
$this->eventDispatcher
->dispatch(SalesforceEvents::PUSH_MAPPING_OBJECT, new SalesforcePushOpEvent($mapped_object, $item->op));
if ($item->op == MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_DELETE) {
$mapped_object
->pushDelete();
$mapped_object
->delete();
}
else {
$entity = $this->etm
->getStorage($mapping->drupal_entity_type)
->load($item->entity_id);
if ($entity === NULL) {
throw new EntityNotFoundException($item->entity_id, $mapping->drupal_entity_type);
}
$mapped_object
->setDrupalEntity($entity)
->push();
}
} catch (\Exception $e) {
$this->eventDispatcher
->dispatch(SalesforceEvents::PUSH_FAIL, new SalesforcePushOpEvent($mapped_object, $item->op));
if (!$mapped_object
->isNew()) {
$mapped_object
->set('last_sync_action', $item->op)
->set('last_sync_status', FALSE)
->set('revision_log_message', $e
->getMessage())
->save();
}
throw $e;
}
}
protected function getMappedObject(\stdClass $item, SalesforceMappingInterface $mapping) {
$mapped_object = FALSE;
if ($item->mapped_object_id) {
$mapped_object = $this->mappedObjectStorage
->load($item->mapped_object_id);
}
if ($mapped_object) {
return $mapped_object;
}
if ($item->entity_id) {
$mapped_object = $this->mappedObjectStorage
->loadByProperties([
'drupal_entity__target_type' => $mapping->drupal_entity_type,
'drupal_entity__target_id' => $item->entity_id,
'salesforce_mapping' => $mapping
->id(),
]);
}
if ($mapped_object) {
if (is_array($mapped_object)) {
$mapped_object = current($mapped_object);
}
return $mapped_object;
}
return $this
->createMappedObject($item, $mapping);
}
protected function createMappedObject(\stdClass $item, SalesforceMappingInterface $mapping) {
return new MappedObject([
'drupal_entity' => [
'target_id' => $item->entity_id,
'target_type' => $mapping->drupal_entity_type,
],
'salesforce_mapping' => $mapping
->id(),
]);
}
}