class Rest in Salesforce Suite 5.0.x
Same name and namespace in other branches
- 8.4 modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php \Drupal\salesforce_push\Plugin\SalesforcePushQueueProcessor\Rest
- 8.3 modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php \Drupal\salesforce_push\Plugin\SalesforcePushQueueProcessor\Rest
Rest queue processor plugin.
Plugin annotation
@Plugin(
id = "rest",
label = @Translation("REST Push Queue Processor")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\salesforce_push\Plugin\SalesforcePushQueueProcessor\Rest implements PushQueueProcessorInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Rest
1 file declares its use of Rest
- SalesforcePushQueueProcessorRestTest.php in modules/
salesforce_push/ tests/ src/ Unit/ SalesforcePushQueueProcessorRestTest.php
File
- modules/
salesforce_push/ src/ Plugin/ SalesforcePushQueueProcessor/ Rest.php, line 28
Namespace
Drupal\salesforce_push\Plugin\SalesforcePushQueueProcessorView source
class Rest extends PluginBase implements PushQueueProcessorInterface {
/**
* Push queue service.
*
* @var \Drupal\salesforce_push\PushQueueInterface
*/
protected $queue;
/**
* Storage handler for SF mappings.
*
* @var \Drupal\salesforce_mapping\SalesforceMappingStorage
*/
protected $mappingStorage;
/**
* Storage handler for Mapped Objects.
*
* @var \Drupal\salesforce_mapping\MappedObjectStorage
*/
protected $mappedObjectStorage;
/**
* Event dispatcher service.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* ETM service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $etm;
/**
* Auth manager.
*
* @var \Drupal\salesforce\SalesforceAuthProviderPluginManagerInterface
*/
protected $authMan;
/**
* Rest constructor.
*
* @param array $configuration
* Plugin config.
* @param string $plugin_id
* Plugin id.
* @param array $plugin_definition
* Plugin definition.
* @param \Drupal\salesforce_push\PushQueueInterface $queue
* Push queue service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $etm
* ETM service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* Event dispatcher service.
* @param \Drupal\salesforce\SalesforceAuthProviderPluginManagerInterface $authMan
* Auth manager.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, PushQueueInterface $queue, EntityTypeManagerInterface $etm, EventDispatcherInterface $eventDispatcher, SalesforceAuthProviderPluginManagerInterface $authMan) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->queue = $queue;
$this->etm = $etm;
$this->mappingStorage = $etm
->getStorage('salesforce_mapping');
$this->mappedObjectStorage = $etm
->getStorage('salesforce_mapped_object');
$this->eventDispatcher = $eventDispatcher;
$this->authMan = $authMan;
}
/**
* {@inheritdoc}
*/
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('entity_type.manager'), $container
->get('event_dispatcher'), $container
->get('plugin.manager.salesforce.auth_providers'));
}
/**
* Process push queue items.
*/
public function process(array $items) {
if (!$this->authMan
->getToken()) {
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);
}
}
}
/**
* Push queue item process callback.
*
* @param object $item
* The push queue item.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function processItem(\stdClass $item) {
// Allow exceptions to bubble up for PushQueue to sort things out.
$mapping = $this->mappingStorage
->load($item->name);
$mapped_object = $this
->getMappedObject($item, $mapping);
if ($mapped_object
->isNew() && $item->op == MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_DELETE) {
// If mapped object doesn't exist or fails to load for this delete, this
// item can be considered successfully processed.
return;
}
// @TODO: the following is nearly identical to the end of salesforce_push_entity_crud(). Can we DRY it? Do we care?
try {
$this->eventDispatcher
->dispatch(new SalesforcePushOpEvent($mapped_object, $item->op), SalesforceEvents::PUSH_MAPPING_OBJECT);
// If this is a delete, destroy the SF object and we're done.
if ($item->op == MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_DELETE) {
$mapped_object
->pushDelete();
// This has to be cleaned up here because we need the object to process
// Async.
$mapped_object
->delete();
}
else {
$entity = $this->etm
->getStorage($mapping->drupal_entity_type)
->load($item->entity_id);
if ($entity === NULL) {
// Bubble this up also.
throw new EntityNotFoundException($item->entity_id, $mapping->drupal_entity_type);
}
// Push to SF. This also saves the mapped object.
$mapped_object
->setDrupalEntity($entity)
->push();
}
} catch (\Exception $e) {
$this->eventDispatcher
->dispatch(new SalesforcePushOpEvent($mapped_object, $item->op), SalesforceEvents::PUSH_FAIL);
// Log errors and throw exception to cause this item to be re-queued.
if (!$mapped_object
->isNew()) {
// Only update existing mapped objects.
$mapped_object
->set('last_sync_action', $item->op)
->set('last_sync_status', FALSE)
->set('revision_log_message', $e
->getMessage())
->save();
}
throw $e;
}
}
/**
* Return the mapped object given a queue item and mapping.
*
* @param object $item
* Push queue item.
* @param \Drupal\salesforce_mapping\Entity\SalesforceMappingInterface $mapping
* The mapping.
*
* @return \Drupal\salesforce_mapping\Entity\MappedObject
* The mapped object.
*/
protected function getMappedObject(\stdClass $item, SalesforceMappingInterface $mapping) {
$mapped_object = FALSE;
// Prefer mapped object id if we have one.
if ($item->mapped_object_id) {
$mapped_object = $this->mappedObjectStorage
->load($item->mapped_object_id);
}
if ($mapped_object) {
return $mapped_object;
}
// Fall back to entity+mapping, which is a unique key.
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);
}
/**
* Helper method to generate a new MappedObject during push procesing.
*
* @param object $item
* Push queue item.
* @param \Drupal\salesforce_mapping\Entity\SalesforceMappingInterface $mapping
* The mapping.
*
* @return \Drupal\salesforce_mapping\Entity\MappedObjectInterface
* The new mapped object.
*/
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(),
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
Rest:: |
protected | property | Auth manager. | |
Rest:: |
protected | property | ETM service. | |
Rest:: |
protected | property | Event dispatcher service. | |
Rest:: |
protected | property | Storage handler for Mapped Objects. | |
Rest:: |
protected | property | Storage handler for SF mappings. | |
Rest:: |
protected | property | Push queue service. | |
Rest:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
Rest:: |
protected | function | Helper method to generate a new MappedObject during push procesing. | |
Rest:: |
protected | function | Return the mapped object given a queue item and mapping. | |
Rest:: |
public | function |
Process push queue items. Overrides PushQueueProcessorInterface:: |
|
Rest:: |
public | function | Push queue item process callback. | |
Rest:: |
public | function |
Rest constructor. Overrides PluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |