You are here

class Rest in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 modules/salesforce_push/src/Plugin/SalesforcePushQueueProcessor/Rest.php \Drupal\salesforce_push\Plugin\SalesforcePushQueueProcessor\Rest
  2. 5.0.x 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

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\SalesforcePushQueueProcessor
View 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(SalesforceEvents::PUSH_MAPPING_OBJECT, new SalesforcePushOpEvent($mapped_object, $item->op));

      // 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(SalesforceEvents::PUSH_FAIL, new SalesforcePushOpEvent($mapped_object, $item->op));

      // 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
Rest::$authMan protected property Auth manager.
Rest::$etm protected property ETM service.
Rest::$eventDispatcher protected property Event dispatcher service.
Rest::$mappedObjectStorage protected property Storage handler for Mapped Objects.
Rest::$mappingStorage protected property Storage handler for SF mappings.
Rest::$queue protected property Push queue service.
Rest::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Rest::createMappedObject protected function Helper method to generate a new MappedObject during push procesing.
Rest::getMappedObject protected function Return the mapped object given a queue item and mapping.
Rest::process public function Process push queue items. Overrides PushQueueProcessorInterface::process
Rest::processItem public function Push queue item process callback.
Rest::__construct public function Rest constructor. Overrides PluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.