You are here

LockrRegisterForm.php in Lockr 8.2

Namespace

Drupal\lockr\Form

File

src/Form/LockrRegisterForm.php
View source
<?php

namespace Drupal\lockr\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Egulias\EmailValidator\EmailValidator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Lockr\Exception\LockrClientException;
use Lockr\Exception\LockrServerException;
use Drupal\lockr\ClientFactory;
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');
  }

}

Classes

Namesort descending Description
LockrRegisterForm