You are here

protected function PullBase::createEntity in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php \Drupal\salesforce_pull\Plugin\QueueWorker\PullBase::createEntity()
  2. 8.3 modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php \Drupal\salesforce_pull\Plugin\QueueWorker\PullBase::createEntity()

Create a Drupal entity and mapped object.

Parameters

\Drupal\salesforce_mapping\Entity\SalesforceMappingInterface $mapping: Object of field maps.

\Drupal\salesforce\SObject $sf_object: Current Salesforce record array.

Return value

string|null Return \Drupal\salesforce_mapping\MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE on successful create, NULL otherwise.

Throws

\Exception

1 call to PullBase::createEntity()
PullBase::processItem in modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php
Queue item process callback.

File

modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php, line 242

Class

PullBase
Provides base functionality for the Salesforce Pull Queue Workers.

Namespace

Drupal\salesforce_pull\Plugin\QueueWorker

Code

protected function createEntity(SalesforceMappingInterface $mapping, SObject $sf_object) {
  if (!$mapping
    ->checkTriggers([
    MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE,
  ])) {
    return;
  }
  try {

    // Define values to pass to entity_create().
    $entity_type = $mapping
      ->getDrupalEntityType();
    $entity_keys = $this->etm
      ->getDefinition($entity_type)
      ->getKeys();
    $values = [];
    if (isset($entity_keys['bundle']) && !empty($entity_keys['bundle'])) {
      $values[$entity_keys['bundle']] = $mapping
        ->getDrupalBundle();
    }

    // See note above about flag.
    $values['salesforce_pull'] = TRUE;

    // Create entity.
    $entity = $this->etm
      ->getStorage($entity_type)
      ->create($values);

    // Create mapped object.
    $mapped_object = $this->mappedObjectStorage
      ->create([
      'drupal_entity' => [
        'target_type' => $entity_type,
      ],
      'salesforce_mapping' => $mapping->id,
      'salesforce_id' => (string) $sf_object
        ->id(),
    ]);
    $mapped_object
      ->setDrupalEntity($entity)
      ->setSalesforceRecord($sf_object);
    $event = $this->eventDispatcher
      ->dispatch(new SalesforcePullEvent($mapped_object, MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE), SalesforceEvents::PULL_PREPULL);
    if (!$event
      ->isPullAllowed()) {
      $this->eventDispatcher
        ->dispatch(new SalesforceNoticeEvent(NULL, 'Pull was not allowed for %label with %sfid', [
        '%label' => $entity
          ->label(),
        '%sfid' => (string) $sf_object
          ->id(),
      ]), SalesforceEvents::NOTICE);
      return FALSE;
    }
    $mapped_object
      ->pull();

    // Push upsert ID to SF object, if allowed and not already set.
    if ($mapping
      ->hasKey() && $mapping
      ->checkTriggers([
      MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_CREATE,
      MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_UPDATE,
    ]) && $sf_object
      ->field($mapping
      ->getKeyField()) === NULL) {
      $params = new PushParams($mapping, $entity);
      $this->eventDispatcher
        ->dispatch(new SalesforcePushParamsEvent($mapped_object, $params), SalesforceEvents::PUSH_PARAMS);

      // Get just the key param and send that.
      $key_field = $mapping
        ->getKeyField();
      $key_param = [
        $key_field => $params
          ->getParam($key_field),
      ];
      $sent_id = $this
        ->sendEntityId($mapping
        ->getSalesforceObjectType(), $mapped_object
        ->sfid(), $key_param);
      if (!$sent_id) {
        throw new PullException();
      }
    }
    $this->eventDispatcher
      ->dispatch(new SalesforceNoticeEvent(NULL, 'Created entity %id %label associated with Salesforce Object ID: %sfid', [
      '%id' => $entity
        ->id(),
      '%label' => $entity
        ->label(),
      '%sfid' => (string) $sf_object
        ->id(),
    ]), SalesforceEvents::NOTICE);
    return MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE;
  } catch (\Exception $e) {
    $this->eventDispatcher
      ->dispatch(new SalesforceNoticeEvent($e, 'Pull-create failed for Salesforce Object ID: %sfobjectid', [
      '%sfobjectid' => (string) $sf_object
        ->id(),
    ]), SalesforceEvents::WARNING);

    // Throwing a new exception to keep current item in cron queue.
    throw $e;
  }
}