View source
<?php
namespace Drupal\usermerge;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\user\UserInterface;
class UserMerger implements UserMergerInterface {
protected $propertyPluginManager;
protected $actionPluginManager;
public function __construct(PluginManagerInterface $property_manager, PluginManagerInterface $action_manager) {
$this->propertyPluginManager = $property_manager;
$this->actionPluginManager = $action_manager;
}
public function merge(UserInterface $retire_user, UserInterface $retain_user, $action_id = 'action_block') {
$this
->applyAction($action_id, $retire_user, $retain_user);
foreach ($this
->getPropertyPlugins() as $plugin_id) {
$this
->applyProperty($plugin_id, $retire_user, $retain_user);
}
return $retain_user;
}
public function applyAction($plugin_id, UserInterface $retire_user, UserInterface $retain_user) {
$this->actionPluginManager
->createInstance($plugin_id)
->process($retire_user, $retain_user);
}
public function applyProperty($plugin_id, UserInterface $retire_user, UserInterface $retain_user, array $settings = []) {
$this->propertyPluginManager
->createInstance($plugin_id)
->process($retire_user, $retain_user, $settings);
}
public function getPropertyPlugins() {
$plugin_ids = [];
foreach ($this->propertyPluginManager
->getDefinitions() as $plugin_id => $definition) {
$plugin_ids[] = $plugin_id;
}
return $plugin_ids;
}
}