ReviewFormSwitcher.php in User Merge 2.x
File
src/ReviewFormSwitcher.php
View source
<?php
namespace Drupal\usermerge;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\usermerge\Exception\UserMergeException;
class ReviewFormSwitcher implements ReviewFormSwitcherInterface {
protected $propertyPluginManager;
protected $formList = [];
public function __construct(PluginManagerInterface $property_manager) {
$this->propertyPluginManager = $property_manager;
$this
->buildFormList();
}
public function hasNext($class) : bool {
$keys = array_keys($this->formList);
$last_key = end($keys);
if ($this->formList[$last_key] == '\\' . $class) {
return FALSE;
}
return TRUE;
}
public function hasPrevious($class) : bool {
$list = $this->formList;
reset($list);
$first_key = key($list);
if ($this->formList[$first_key] == '\\' . $class) {
return FALSE;
}
return TRUE;
}
public function getFormFromProperty($property) : string {
if (!isset($this->formList[$property])) {
throw new UserMergeException('Provided property has not been found');
}
return $this->formList[$property];
}
public function getPreviousProperty($class) : string {
$previous = '';
foreach ($this->formList as $id => $review) {
if ($review == '\\' . $class) {
break;
}
$previous = $id;
}
return $previous;
}
public function getNextProperty($class) : string {
$next = '';
$get_next = FALSE;
foreach ($this->formList as $id => $review) {
if ($review == '\\' . $class) {
$get_next = TRUE;
continue;
}
if ($get_next) {
$next = $id;
break;
}
}
return $next;
}
private function buildFormList() : void {
$this->formList[''] = '\\Drupal\\usermerge\\Form\\PickAccountsForm';
$definitions = $this->propertyPluginManager
->getDefinitions();
foreach ($definitions as $definition) {
if (empty($definition['review'])) {
continue;
}
if (is_subclass_of($definition['review'], '\\Drupal\\usermerge\\Form\\MultiStepFormBase')) {
$this->formList[$definition['id']] = $definition['review'];
}
}
}
public function getPropertyFromForm($class) : string {
$property = '';
foreach ($this->formList as $id => $review) {
if ($review == '\\' . $class) {
$property = $id;
}
}
return $property;
}
}