BatchExportProcessor.php in Content Synchronizer 3.x
File
src/Processors/BatchExportProcessor.php
View source
<?php
namespace Drupal\content_synchronizer\Processors;
use Drupal\content_synchronizer\Base\BatchProcessorBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class BatchExportProcessor extends BatchProcessorBase {
use StringTranslationTrait;
protected $writer;
protected $exportProcessor;
public function __construct(ExportEntityWriter $writer) {
$this->writer = $writer;
}
public function exportEntities(array $entities, $finishCallback = NULL) {
$operations = $this
->getBatchOperations($entities, $finishCallback);
$batch = [
'title' => $this
->t('Exporting entities...'),
'operations' => $operations,
'finished' => get_called_class() . '::onFinishBatchProcess',
];
batch_set($batch);
}
protected function getBatchOperations(array $entities, $finishCallback = NULL) {
$operations = [];
foreach ($entities as $entity) {
$operations[] = [
get_called_class() . '::processBatchOperation',
[
[
'entity_id' => $entity
->id(),
'entity_type' => $entity
->getEntityTypeId(),
'writer' => $this->writer,
'finishCallback' => $finishCallback,
],
],
];
}
return $operations;
}
public static function processBatchOperation(array $entityData, array $context) {
$writer = $entityData['writer'];
if ($entity = \Drupal::entityTypeManager()
->getStorage($entityData['entity_type'])
->load($entityData['entity_id'])) {
$processor = new ExportProcessor($writer);
$processor
->exportEntity($entity);
}
$context['results']['writer'] = $writer;
$context['results']['finishCallback'] = $entityData['finishCallback'];
}
public static function onFinishBatchProcess($success, $results, $operations) {
$writer = $results['writer'];
$processor = new ExportProcessor($writer);
if ($archiveUri = $processor
->closeProcess()) {
if (array_key_exists('finishCallback', $results)) {
self::callFinishCallback($results['finishCallback'], $archiveUri);
}
}
}
}