You are here

class BatchImportProcessor in Content Synchronizer 8

Same name and namespace in other branches
  1. 8.2 src/Processors/BatchImportProcessor.php \Drupal\content_synchronizer\Processors\BatchImportProcessor
  2. 3.x src/Processors/BatchImportProcessor.php \Drupal\content_synchronizer\Processors\BatchImportProcessor

Batch Import.

Hierarchy

Expanded class hierarchy of BatchImportProcessor

1 file declares its use of BatchImportProcessor
LaunchImportForm.php in src/Form/LaunchImportForm.php

File

src/Processors/BatchImportProcessor.php, line 11

Namespace

Drupal\content_synchronizer\Processors
View source
class BatchImportProcessor extends BatchProcessorBase {

  /**
   * The entities to import.
   *
   * @var array
   */
  protected $entitiesToImport;

  /**
   * Launch the import of the import entity.
   *
   * @param \Drupal\content_synchronizer\Entity\ImportEntity $import
   *   The import entity.
   * @param array $entitiesToImport
   *   The entities to import.
   * @param mixed $finishCallBack
   *   The finishcallback.
   * @param string $creationType
   *   The creation entity type.
   * @param string $updateType
   *   The update entity type.
   */
  public function import(ImportEntity $import, array $entitiesToImport, $finishCallBack = NULL, $creationType = ImportProcessor::PUBLICATION_UNPUBLISH, $updateType = ImportProcessor::UPDATE_IF_RECENT) {
    $operations = $this
      ->getBatchOperations($import, $entitiesToImport, $finishCallBack, $creationType, $updateType);
    $batch = [
      'title' => t('Importing entities...'),
      'operations' => $operations,
      'finished' => get_called_class() . '::onFinishBatchProcess',
    ];
    batch_set($batch);
  }

  /**
   * Set the entities to import list.
   */
  public function setEntitiesToImport(array $entitiesToImport) {
    $this->entitiesToImport = $entitiesToImport;
  }

  /**
   * {@inheritdoc}
   */
  protected function getBatchOperations(ImportEntity $import, array $entitiesToImport, $finishCallBack = NULL, $creationType = ImportProcessor::PUBLICATION_UNPUBLISH, $updateType = ImportProcessor::UPDATE_IF_RECENT) {
    $operations = [];
    foreach ($entitiesToImport as $data) {
      $data['finishCallback'] = $finishCallBack;
      $data['importId'] = $import
        ->id();
      $data['creationType'] = $creationType;
      $data['updateType'] = $updateType;
      $operations[] = [
        get_called_class() . '::processBatchOperation',
        [
          $data,
        ],
      ];
    }
    return $operations;
  }

  /**
   * Do a batch operation.
   *
   * @param array $data
   *   The data to treat.
   * @param array $context
   *   The context.
   */
  public static function processBatchOperation(array $data, array $context) {
    $importProcessor = new ImportProcessor(ImportEntity::load($data['importId']));
    $importProcessor
      ->setCreationType($data['creationType']);
    $importProcessor
      ->setUpdateType($data['updateType']);
    $importProcessor
      ->importEntityFromRootData($data);
    $context['results']['finishCallback'] = $data['finishCallback'];
  }

  /**
   * Callback.
   */
  public static function onFinishBatchProcess($success, $results, $operations) {
    if (array_key_exists('finishCallback', $results)) {
      self::callFinishCallback($results['finishCallback'], $results);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BatchImportProcessor::$entitiesToImport protected property The entities to import.
BatchImportProcessor::getBatchOperations protected function
BatchImportProcessor::import public function Launch the import of the import entity.
BatchImportProcessor::onFinishBatchProcess public static function Callback. Overrides BatchProcessorBase::onFinishBatchProcess
BatchImportProcessor::processBatchOperation public static function Do a batch operation. Overrides BatchProcessorBase::processBatchOperation
BatchImportProcessor::setEntitiesToImport public function Set the entities to import list.
BatchProcessorBase::callFinishCallback protected static function Call the callback method if defined.