You are here

class BatchExportProcessor in Content Synchronizer 8

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

BatchExport processor.

Hierarchy

Expanded class hierarchy of BatchExportProcessor

3 files declare their use of BatchExportProcessor
ExportConfirmForm.php in src/Form/ExportConfirmForm.php
LaunchExportForm.php in src/Form/LaunchExportForm.php
QuickExportController.php in src/Controller/QuickExportController.php

File

src/Processors/BatchExportProcessor.php, line 10

Namespace

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

  /**
   * The writer service.
   *
   * @var ExportEntityWriter
   */
  protected $writer;

  /**
   * The export processor.
   *
   * @var ExportProcessor
   */
  protected $exportProcessor;

  /**
   * {@inheritdoc}
   */
  public function __construct(ExportEntityWriter $writer) {
    $this->writer = $writer;
  }

  /**
   * Export entities.
   *
   * @param array $entities
   *   Entities to export.
   * @param mixed $finishCallback
   *   Callback method.
   */
  public function exportEntities(array $entities, $finishCallback = NULL) {
    $operations = $this
      ->getBatchOperations($entities, $finishCallback);
    $batch = [
      'title' => t('Exporting entities...'),
      'operations' => $operations,
      'finished' => get_called_class() . '::onFinishBatchProcess',
    ];
    batch_set($batch);
  }

  /**
   * {@inheritdoc}
   */
  protected function getBatchOperations(array $entities, $finishCallback = NULL) {
    $operations = [];

    /** @var \Drupal\Core\Entity\Entity $entity */
    foreach ($entities as $entity) {
      $operations[] = [
        get_called_class() . '::processBatchOperation',
        [
          [
            'entity_id' => $entity
              ->id(),
            'entity_type' => $entity
              ->getEntityTypeId(),
            'writer' => $this->writer,
            'finishCallback' => $finishCallback,
          ],
        ],
      ];
    }
    return $operations;
  }

  /**
   * Do a batch operation.
   *
   * @param array $entityData
   *   Entity data.
   * @param array $context
   *   Context.
   */
  public static function processBatchOperation(array $entityData, array $context) {

    /** @var ExportEntityWriter $writer */
    $writer = $entityData['writer'];

    // Get the entity :

    /** @var \Drupal\Core\Entity\Entity $entity */
    if ($entity = \Drupal::entityTypeManager()
      ->getStorage($entityData['entity_type'])
      ->load($entityData['entity_id'])) {

      /** @var ExportProcessor $processor */
      $processor = new ExportProcessor($writer);
      $processor
        ->exportEntity($entity);
    }
    $context['results']['writer'] = $writer;
    $context['results']['finishCallback'] = $entityData['finishCallback'];
  }

  /**
   * {@inheritdoc}
   */
  public static function onFinishBatchProcess($success, $results, $operations) {

    /** @var ExportEntityWriter $writer */
    $writer = $results['writer'];
    $processor = new ExportProcessor($writer);
    if ($archiveUri = $processor
      ->closeProcess()) {

      // Redirection :
      if (array_key_exists('finishCallback', $results)) {
        self::callFinishCallback($results['finishCallback'], $archiveUri);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BatchExportProcessor::$exportProcessor protected property The export processor.
BatchExportProcessor::$writer protected property The writer service.
BatchExportProcessor::exportEntities public function Export entities.
BatchExportProcessor::getBatchOperations protected function
BatchExportProcessor::onFinishBatchProcess public static function Callback method. Overrides BatchProcessorBase::onFinishBatchProcess
BatchExportProcessor::processBatchOperation public static function Do a batch operation. Overrides BatchProcessorBase::processBatchOperation
BatchExportProcessor::__construct public function
BatchProcessorBase::callFinishCallback protected static function Call the callback method if defined.