You are here

class ReviewFormSwitcher in User Merge 2.x

Class ReviewFormSwitcher.

@package Drupal\usermerge

Hierarchy

Expanded class hierarchy of ReviewFormSwitcher

1 file declares its use of ReviewFormSwitcher
ReviewFormSwitcherTest.php in tests/src/Unit/ReviewFormSwitcherTest.php
1 string reference to 'ReviewFormSwitcher'
usermerge.services.yml in ./usermerge.services.yml
usermerge.services.yml
1 service uses ReviewFormSwitcher
usermerge.review_form_switcher in ./usermerge.services.yml
Drupal\usermerge\ReviewFormSwitcher

File

src/ReviewFormSwitcher.php, line 13

Namespace

Drupal\usermerge
View source
class ReviewFormSwitcher implements ReviewFormSwitcherInterface {

  /**
   * Property plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $propertyPluginManager;

  /**
   * List of forms used in multi step review.
   *
   * @var array
   */
  protected $formList = [];

  /**
   * MergeAccountsForm constructor.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $property_manager
   *   Property plugin manager.
   */
  public function __construct(PluginManagerInterface $property_manager) {
    $this->propertyPluginManager = $property_manager;
    $this
      ->buildFormList();
  }

  /**
   * {@inheritDoc}
   */
  public function hasNext($class) : bool {
    $keys = array_keys($this->formList);
    $last_key = end($keys);
    if ($this->formList[$last_key] == '\\' . $class) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritDoc}
   */
  public function hasPrevious($class) : bool {
    $list = $this->formList;
    reset($list);
    $first_key = key($list);
    if ($this->formList[$first_key] == '\\' . $class) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritDoc}
   */
  public function getFormFromProperty($property) : string {
    if (!isset($this->formList[$property])) {
      throw new UserMergeException('Provided property has not been found');
    }
    return $this->formList[$property];
  }

  /**
   * {@inheritDoc}
   */
  public function getPreviousProperty($class) : string {
    $previous = '';
    foreach ($this->formList as $id => $review) {
      if ($review == '\\' . $class) {
        break;
      }
      $previous = $id;
    }
    return $previous;
  }

  /**
   * {@inheritDoc}
   */
  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;
  }

  /**
   * Build form list.
   */
  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'];
      }
    }
  }

  /**
   * {@inheritDoc}
   */
  public function getPropertyFromForm($class) : string {
    $property = '';
    foreach ($this->formList as $id => $review) {
      if ($review == '\\' . $class) {
        $property = $id;
      }
    }
    return $property;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReviewFormSwitcher::$formList protected property List of forms used in multi step review.
ReviewFormSwitcher::$propertyPluginManager protected property Property plugin manager.
ReviewFormSwitcher::buildFormList private function Build form list.
ReviewFormSwitcher::getFormFromProperty public function Get class name. Overrides ReviewFormSwitcherInterface::getFormFromProperty
ReviewFormSwitcher::getNextProperty public function Get next property. Overrides ReviewFormSwitcherInterface::getNextProperty
ReviewFormSwitcher::getPreviousProperty public function Get previous property. Overrides ReviewFormSwitcherInterface::getPreviousProperty
ReviewFormSwitcher::getPropertyFromForm public function Get id of property. Overrides ReviewFormSwitcherInterface::getPropertyFromForm
ReviewFormSwitcher::hasNext public function Check if we have the next form. Overrides ReviewFormSwitcherInterface::hasNext
ReviewFormSwitcher::hasPrevious public function Check if we have the previous form. Overrides ReviewFormSwitcherInterface::hasPrevious
ReviewFormSwitcher::__construct public function MergeAccountsForm constructor.