You are here

class LockrRegisterForm in Lockr 8.2

Same name and namespace in other branches
  1. 8.4 src/Form/LockrRegisterForm.php \Drupal\lockr\Form\LockrRegisterForm
  2. 8.3 src/Form/LockrRegisterForm.php \Drupal\lockr\Form\LockrRegisterForm
  3. 4.x src/Form/LockrRegisterForm.php \Drupal\lockr\Form\LockrRegisterForm

Hierarchy

Expanded class hierarchy of LockrRegisterForm

1 file declares its use of LockrRegisterForm
LockrAdminController.php in src/Controller/LockrAdminController.php

File

src/Form/LockrRegisterForm.php, line 18

Namespace

Drupal\lockr\Form
View source
class LockrRegisterForm implements ContainerInjectionInterface, FormInterface {

  /** @var ClientFactory */
  protected $clientFactory;

  /** @var EmailValidator */
  protected $emailValidator;

  /** @var ConfigFactoryInterface */
  protected $configFactory;
  public function __construct(ClientFactory $client_factory, EmailValidator $email_validator, ConfigFactoryInterface $config_factory) {
    $this->clientFactory = $client_factory;
    $this->emailValidator = $email_validator;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('lockr.client_factory'), $container
      ->get('email.validator'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'lockr_register_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['email'] = [
      '#type' => 'textfield',
      '#title' => 'Email address',
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => 'Sign up',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $email = $form_state
      ->getValue('email');
    if (!$this->emailValidator
      ->isValid($email)) {
      $form_state
        ->setErrorByName('email', 'Please enter a valid email address.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $email = $form_state
      ->getValue('email');
    $name = $this
      ->getSiteName();
    $site_client = $this->clientFactory
      ->getSiteClient();
    try {
      $site_client
        ->register($email, NULL, $name);
    } catch (LockrClientException $e) {
      if ($e->title === 'Missing header value' && $e->description === 'The Auth header is required.') {
        $form_state
          ->setRedirect('lockr.login');
        return;
      }
      elseif ($e->title === 'Partner mismatch') {
        $msg = "We didn't recognize your certificate, please ensure the provide path is a valid Lockr certificate.";
      }
      elseif ($e->title === 'Site exists') {
        $msg = 'This site is already registered. If you are experiencing issues, please contact support@lockr.io.';
      }
      else {
        $msg = 'An unknown error occurred, please try again later.';
      }
      drupal_set_message($msg, 'error');
      return;
    } catch (LockrServerException $e) {
      $msg = 'An unknown error occurred, please try again later.';
      drupal_set_message($msg, 'error');
      return;
    }
    drupal_set_message("That's it! You're signed up with Lockr; your keys are now safe.");
    $form_state
      ->setRedirect('entity.key.collection');
  }

  /**
   * Get the human readable name of the site.
   */
  protected function getSiteName() {
    return $this->configFactory
      ->get('system.site')
      ->get('name');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LockrRegisterForm::$clientFactory protected property @var ClientFactory
LockrRegisterForm::$configFactory protected property @var ConfigFactoryInterface
LockrRegisterForm::$emailValidator protected property @var EmailValidator
LockrRegisterForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
LockrRegisterForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LockrRegisterForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LockrRegisterForm::getSiteName protected function Get the human readable name of the site.
LockrRegisterForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LockrRegisterForm::validateForm public function Form validation handler. Overrides FormInterface::validateForm
LockrRegisterForm::__construct public function