You are here

public function SalesforceMappingCommands::purgeSalesforce in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 modules/salesforce_mapping/src/Commands/SalesforceMappingCommands.php \Drupal\salesforce_mapping\Commands\SalesforceMappingCommands::purgeSalesforce()
  2. 5.0.x modules/salesforce_mapping/src/Commands/SalesforceMappingCommands.php \Drupal\salesforce_mapping\Commands\SalesforceMappingCommands::purgeSalesforce()

Clean up Mapped Objects by deleting records referencing missing records.

@command salesforce_mapping:purge-salesforce @aliases sfpsf,sf-purge-salesforce

Parameters

string $name: Id of the salesforce mapping whose mapped objects should be purged.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to SalesforceMappingCommands::purgeSalesforce()
SalesforceMappingCommands::purgeAll in modules/salesforce_mapping/src/Commands/SalesforceMappingCommands.php
Clean up Mapped Objects table.

File

modules/salesforce_mapping/src/Commands/SalesforceMappingCommands.php, line 273

Class

SalesforceMappingCommands
A Drush commandfile.

Namespace

Drupal\salesforce_mapping\Commands

Code

public function purgeSalesforce($name) {
  $object_types = $this
    ->objectTypesByPrefix();
  $mapped_obj_table = $this->etm
    ->getDefinition('salesforce_mapped_object')
    ->getBaseTable();
  $query = $this->database
    ->select($mapped_obj_table, 'm');
  $query
    ->addExpression('distinct substr(salesforce_id, 1, 3)');
  if ($name && strtoupper($name) != 'ALL') {
    $query
      ->condition('salesforce_mapping', $name);
  }
  $sfid_prefixes = $query
    ->execute()
    ->fetchCol();
  foreach ($sfid_prefixes as $prefix) {
    if (empty($object_types[$prefix]['name'])) {
      $query = $this->database
        ->select($mapped_obj_table, 'm')
        ->fields('m', [
        'salesforce_id',
        'id',
      ]);
      $query
        ->condition('salesforce_id', $prefix . '%', 'LIKE');
      $mapped_obj_ids = $query
        ->execute()
        ->fetchAllKeyed();
      if (empty($mapped_obj_ids)) {
        continue;
      }
      $this
        ->logger()
        ->warning(dt('Unknown object type for Salesforce ID prefix !prefix', [
        '!prefix' => $prefix,
      ]));
      $this
        ->purgeConfirmAndDelete($mapped_obj_ids, 'prefix ' . $prefix);
      continue;
    }
    $query = $this->database
      ->select($mapped_obj_table, 'm')
      ->fields('m', [
      'salesforce_id',
      'id',
    ]);
    if ($name && strtoupper($name) != 'ALL') {
      $query
        ->condition('salesforce_mapping', $name);
    }
    else {
      $query
        ->condition('salesforce_id', $prefix . '%', 'LIKE');
    }
    $sfids = $query
      ->execute()
      ->fetchAllKeyed();
    $to_delete = $sfids;

    // SOQL queries are limited to 4000-characters in where statements.
    // Chunkify in case we have more than ~200 sfids.
    foreach (array_chunk($sfids, 200, TRUE) as $chunk) {
      $soql_query = new SelectQuery($object_types[$prefix]['name']);
      $soql_query->fields[] = 'Id';
      $soql_query
        ->addCondition('Id', array_keys($chunk));
      $results = $this->client
        ->query($soql_query);
      foreach ($results
        ->records() as $record) {
        unset($to_delete[(string) $record
          ->id()]);
      }
    }
    if (empty($to_delete)) {
      $this
        ->logger()
        ->info(dt('No orphaned mapped objects found for SObject type !type', [
        '!type' => $object_types[$prefix]['name'],
      ]));
      continue;
    }
    $this
      ->purgeConfirmAndDelete(array_values($to_delete), 'SObject type *' . $object_types[$prefix]['name'] . '*');
  }
}