You are here

protected function PullBase::createEntity in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php \Drupal\salesforce_pull\Plugin\QueueWorker\PullBase::createEntity()
  2. 5.0.x 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 244

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(SalesforceEvents::PULL_PREPULL, new SalesforcePullEvent($mapped_object, MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE));
    if (!$event
      ->isPullAllowed()) {
      $this->eventDispatcher
        ->dispatch(SalesforceEvents::NOTICE, new SalesforceNoticeEvent(NULL, 'Pull was not allowed for %label with %sfid', [
        '%label' => $entity
          ->label(),
        '%sfid' => (string) $sf_object
          ->id(),
      ]));
      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(SalesforceEvents::PUSH_PARAMS, new SalesforcePushParamsEvent($mapped_object, $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(SalesforceEvents::NOTICE, new SalesforceNoticeEvent(NULL, 'Created entity %id %label associated with Salesforce Object ID: %sfid', [
      '%id' => $entity
        ->id(),
      '%label' => $entity
        ->label(),
      '%sfid' => (string) $sf_object
        ->id(),
    ]));
    return MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE;
  } catch (\Exception $e) {
    $this->eventDispatcher
      ->dispatch(SalesforceEvents::WARNING, new SalesforceNoticeEvent($e, 'Pull-create failed for Salesforce Object ID: %sfobjectid', [
      '%sfobjectid' => (string) $sf_object
        ->id(),
    ]));

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