You are here

class MultiStepStorage in User Merge 2.x

Class MultiStepStorage.

@package Drupal\usermerge

Hierarchy

Expanded class hierarchy of MultiStepStorage

1 string reference to 'MultiStepStorage'
usermerge.services.yml in ./usermerge.services.yml
usermerge.services.yml
1 service uses MultiStepStorage
usermerge.multi_step_storage in ./usermerge.services.yml
Drupal\usermerge\MultiStepStorage

File

src/MultiStepStorage.php, line 14

Namespace

Drupal\usermerge
View source
class MultiStepStorage implements MultiStepStorageInterface {

  /**
   * Storage for form data.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStore
   */
  protected $store;

  /**
   * The user storage.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * MultiStepFormBase constructor.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The temp store factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->store = $temp_store_factory
      ->get('usermerge');
    $this->userStorage = $entity_type_manager
      ->getStorage('user');
  }

  /**
   * {@inheritDoc}
   */
  public function setValues($property, array $values) : void {

    // Remove not needed values.
    $skip_values = [
      'form_build_id',
      'form_token',
      'form_id',
    ];
    foreach ($skip_values as $skip_value) {
      if (isset($values[$skip_value])) {
        unset($values[$skip_value]);
      }
    }

    // Skip values which are not a string.
    foreach ($values as $key => $value) {
      if (is_object($value)) {
        unset($values[$key]);
      }
    }
    $this->store
      ->set($property, $values);
  }

  /**
   * {@inheritDoc}
   */
  public function getValueFromStore($property, $name) {
    $values = $this->store
      ->get($property);
    if (isset($values[$name])) {
      return $values[$name];
    }
    return NULL;
  }

  /**
   * {@inheritDoc}
   */
  public function getValuesFromStore($property) {
    $values = $this->store
      ->get($property);
    if (is_null($values)) {
      $values = [];
    }
    return $values;
  }

  /**
   * {@inheritDoc}
   */
  public function getRetiredAccount() {
    $values = $this->store
      ->get('');
    if (!isset($values['retire'])) {
      return NULL;
    }
    $user = $this->userStorage
      ->load($values['retire']);
    if ($user instanceof UserInterface) {
      return $user;
    }
    return NULL;
  }

  /**
   * {@inheritDoc}
   */
  public function getRetainedAccount() {
    $values = $this->store
      ->get('');
    if (!isset($values['retain'])) {
      return NULL;
    }
    $user = $this->userStorage
      ->load($values['retain']);
    if ($user instanceof UserInterface) {
      return $user;
    }
    return NULL;
  }

  /**
   * {@inheritDoc}
   *
   * @throws \Drupal\Core\TempStore\TempStoreException
   */
  public function delete($property) : void {
    $this->store
      ->delete($property);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MultiStepStorage::$store protected property Storage for form data.
MultiStepStorage::$userStorage protected property The user storage.
MultiStepStorage::delete public function Overrides MultiStepStorageInterface::delete
MultiStepStorage::getRetainedAccount public function Get retained account. Overrides MultiStepStorageInterface::getRetainedAccount
MultiStepStorage::getRetiredAccount public function Get retired account. Overrides MultiStepStorageInterface::getRetiredAccount
MultiStepStorage::getValueFromStore public function Get default value. Overrides MultiStepStorageInterface::getValueFromStore
MultiStepStorage::getValuesFromStore public function Get stored values. Overrides MultiStepStorageInterface::getValuesFromStore
MultiStepStorage::setValues public function Save form values in store. Overrides MultiStepStorageInterface::setValues
MultiStepStorage::__construct public function MultiStepFormBase constructor.