You are here

class LockrMigrateForm in Lockr 4.x

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

Form handler for Lockr move to production.

Hierarchy

Expanded class hierarchy of LockrMigrateForm

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

File

src/Form/LockrMigrateForm.php, line 22

Namespace

Drupal\lockr\Form
View source
class LockrMigrateForm implements ContainerInjectionInterface, FormInterface {
  use StringTranslationTrait;

  /**
   * Simple config factory.
   *
   * @var ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Lockr library client.
   *
   * @var Lockr
   */
  protected $lockr;

  /**
   * Constructs a new LockrMigrateForm.
   *
   * @param ConfigFactoryInterface $config_factory
   *   The simple config factory.
   * @param Lockr $lockr
   *   The Lockr library client.
   * @param TranslationInterface $translation
   *   The Drupal translator.
   */
  public function __construct(ConfigFactoryInterface $config_factory, Lockr $lockr, TranslationInterface $translation) {
    $this->configFactory = $config_factory;
    $this->lockr = $lockr;
    $this
      ->setStringTranslation($translation);
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $build_info = $form_state
      ->getBuildInfo();
    $info = $build_info['args'][0];
    $form['instructions'] = [
      '#preix' => '<p>',
      '#markup' => $this
        ->t('Click the button below to deploy this site to production. This should only be done in your production environment as it writes a new certificate to the file system.'),
      '#suffix' => '</p>',
    ];
    $form['#attached']['library'][] = 'lockr/move_to_prod';
    $form['#attached']['drupalSettings']['lockr'] = [
      'accounts_host' => 'https://accounts.lockr.io',
      'keyring_id' => $info['keyring']['id'],
    ];
    $form['client_token'] = [
      '#type' => 'hidden',
      '#required' => TRUE,
    ];
    $form['move_to_prod'] = [
      '#type' => 'button',
      '#value' => $this
        ->t('Migrate to Production'),
      '#attributes' => [
        'class' => [
          'move-to-prod',
        ],
      ],
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
      '#attributes' => [
        'class' => [
          'move-to-prod-submit',
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $client_token = $form_state
      ->getValue('client_token');
    $dn = [
      'countryName' => 'US',
      'stateOrProvinceName' => 'Washington',
      'localityName' => 'Tacoma',
      'organizationName' => 'Lockr',
    ];
    try {
      $result = $this->lockr
        ->createCertClient($client_token, $dn);
      CertWriter::writeCerts('prod', $result);
      $config = $this->configFactory
        ->getEditable('lockr.settings');
      $config
        ->set('custom', TRUE);
      $config
        ->set('cert_path', 'private://lockr/prod/pair.pem');
      $config
        ->save();
    } catch (\Exception $e) {

      // XXX: probably log and/or show message
      throw $e;
      return;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LockrMigrateForm::$configFactory protected property Simple config factory.
LockrMigrateForm::$lockr protected property Lockr library client.
LockrMigrateForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
LockrMigrateForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LockrMigrateForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LockrMigrateForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LockrMigrateForm::validateForm public function Form validation handler. Overrides FormInterface::validateForm
LockrMigrateForm::__construct public function Constructs a new LockrMigrateForm.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.