You are here

class LockrMigrateForm in Lockr 8.2

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

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 18

Namespace

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

  /** @var StateInterface */
  protected $state;

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

  /** @var StreamWrapperManagerInterface */
  protected $streamWrapperManager;
  public function __construct(StateInterface $state, ClientFactory $client_factory, StreamWrapperManagerInterface $stream_wrapper_manager) {
    $this->state = $state;
    $this->clientFactory = $client_factory;
    $this->streamWrapperManager = $stream_wrapper_manager;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => 'Migrate to Production',
    ];
    return $form;
  }

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $cert_file = $this->state
      ->get('lockr.cert');
    $cert_info = openssl_x509_parse(file_get_contents($cert_file));
    $subject = $cert_info['subject'];
    $dn = [
      'countryName' => $subject['C'],
      'stateOrProvinceName' => $subject['ST'],
      'localityName' => $subject['L'],
      'organizationName' => $subject['O'],
    ];
    $site_client = $this->clientFactory
      ->getSiteClient();
    try {
      $result = $site_client
        ->createCert($dn);
    } catch (LockrClientException $e) {
      watchdog_exception('lockr', $e);
      drupal_set_message('Please make sure that the current Lockr certificate is valid.', 'error');
      return;
    } catch (LockrServerException $e) {
      watchdog_exception('lockr', $e);
      drupal_set_message('Lockr encountered an unexpected server error, please try again.', 'error');
      return;
    }
    $this->streamWrapperManager
      ->registerWrapper('private', PrivateStream::class, PrivateStream::getType());
    $dir = 'private://lockr/prod';
    mkdir($dir, 0700, TRUE);
    $key_file = "{$dir}/key.pem";
    $key_fd = fopen($key_file, 'w');
    fwrite($key_fd, $result['key_text']);
    fclose($key_fd);
    chmod($key_file, 0600);
    $cert_file = "{$dir}/crt.pem";
    $cert_fd = fopen($cert_file, 'w');
    fwrite($cert_fd, $result['cert_text']);
    fclose($cert_fd);
    chmod($cert_file, 0600);
    $pair_file = "{$dir}/pair.pem";
    $pair_fd = fopen($pair_file, 'w');
    fwrite($pair_fd, $result['key_text']);
    fwrite($pair_fd, $result['cert_text']);
    fclose($pair_fd);
    chmod($pair_file, 0600);
    $private_stream = new PrivateStream();
    $private_stream
      ->setUri("{$dir}/pair.pem");
    $this->state
      ->set('lockr.cert', $private_stream
      ->realpath());
    $this->state
      ->set('lockr.custom', TRUE);
    _lockr_rmtree('private://lockr/dev');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LockrMigrateForm::$clientFactory protected property @var ClientFactory
LockrMigrateForm::$state protected property @var StateInterface
LockrMigrateForm::$streamWrapperManager protected property @var StreamWrapperManagerInterface
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