View source
<?php
namespace Drupal\usermerge;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\user\UserInterface;
use Drupal\usermerge\Exception\UserMergeException;
class BatchGenerator implements BatchGeneratorInterface {
use DependencySerializationTrait;
use StringTranslationTrait;
protected $userMerger;
protected $messenger;
protected $multiStepStorage;
protected $userStorage;
public function __construct(UserMergerInterface $user_merger, MessengerInterface $messenger, MultiStepStorageInterface $multi_step_storage, EntityTypeManagerInterface $entity_type_manager) {
$this->userMerger = $user_merger;
$this->messenger = $messenger;
$this->multiStepStorage = $multi_step_storage;
$this->userStorage = $entity_type_manager
->getStorage('user');
}
public function createBatch() : void {
$batch = [
'title' => $this
->t('Merge account…'),
'init_message' => $this
->t('Starting merge process.'),
'progress_message' => $this
->t('Completed step @current of @total.'),
'error_message' => $this
->t('There was an error in the merge process.'),
'operations' => [],
'finished' => [
$this,
'batchFinished',
],
'progressive' => TRUE,
];
$retain_id = $this->multiStepStorage
->getValueFromStore('', 'retain');
$retire_id = $this->multiStepStorage
->getValueFromStore('', 'retire');
$definitions = $this->userMerger
->getPropertyPlugins();
foreach ($definitions as $plugin_id) {
$settings = $this->multiStepStorage
->getValuesFromStore($plugin_id);
$batch['operations'][] = [
[
$this,
'performPropertyProcess',
],
[
$plugin_id,
$retire_id,
$retain_id,
$settings,
],
];
$this->multiStepStorage
->delete($plugin_id);
}
$batch['operations'][] = [
[
$this,
'performActionProcess',
],
[
$this->multiStepStorage
->getValueFromStore('', 'action'),
$retire_id,
$retain_id,
],
];
$this->multiStepStorage
->delete('');
batch_set($batch);
}
public function performActionProcess($plugin_id, $retire_id, $retain_id, &$context) {
if (isset($context['results']['skip'])) {
return;
}
try {
$retire = $this
->loadAccount($retire_id);
$retain = $this
->loadAccount($retain_id);
$this->userMerger
->applyAction($plugin_id, $retire, $retain);
} catch (UserMergeException $e) {
$context['results']['skip'] = TRUE;
$context['results']['error'] = $e
->getMessage();
}
}
public function performPropertyProcess($plugin_id, $retire_id, $retain_id, array $settings, &$context) {
if (isset($context['results']['skip'])) {
return;
}
try {
$retire = $this
->loadAccount($retire_id);
$retain = $this
->loadAccount($retain_id);
$this->userMerger
->applyProperty($plugin_id, $retire, $retain, $settings);
} catch (UserMergeException $e) {
$context['results']['skip'] = TRUE;
$context['results']['error'] = $e
->getMessage();
}
}
public function batchFinished($success, array $results, array $operations) {
if (isset($results['error'])) {
$this->messenger
->addError($results['error']);
return;
}
if ($success) {
$this->messenger
->addMessage($this
->t('Operations completed.'));
}
else {
$error_operation = reset($operations);
$this->messenger
->addError($this
->t('An error occurred while processing @operation with arguments: @args', [
'@operation' => $error_operation[0],
'@args' => print_r($error_operation[0], TRUE),
]));
}
}
protected function loadAccount($uid) {
$user = $this->userStorage
->load($uid);
if (!$user instanceof UserInterface) {
throw new UserMergeException('Cant not find user');
}
return $user;
}
}