You are here

public function MiniorangeOAuthClientCustomerSetup::submitForm in OAuth2 Login 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/MiniorangeOAuthClientCustomerSetup.php, line 186
Contains \Drupal\miniorange_oauth_client\Form\MiniorangeOAuthClientCustomerSetup.

Class

MiniorangeOAuthClientCustomerSetup

Namespace

Drupal\oauth2_login\Form

Code

public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $username = trim($form['miniorange_oauth_client_customer_setup_username']['#value']);
  $phone = $form['miniorange_oauth_client_customer_setup_phone']['#value'];
  $password = trim($form['miniorange_oauth_client_customer_setup_password']['#value']['pass1']);
  if (strlen($password) < 6) {
    \Drupal::messenger()
      ->addMessage(t('Password is too short.'), 'error');
    return;
  }
  if (empty($username) || empty($password)) {
    \Drupal::messenger()
      ->addMessage(t('The <b><u>Email </u></b> and <b><u>Password</u></b> fields are mandatory.'), 'error');
    return;
  }
  if (!valid_email_address($username)) {
    \Drupal::messenger()
      ->addMessage(t('The email address <i>' . $username . '</i> is not valid.'), 'error');
    return;
  }
  $customer_config = new MiniorangeOAuthClientCustomer($username, $phone, $password, NULL);
  $check_customer_response = json_decode($customer_config
    ->checkCustomer());
  if ($check_customer_response->status == 'CUSTOMER_NOT_FOUND') {
    \Drupal::configFactory()
      ->getEditable('oauth2_login.settings')
      ->set('miniorange_oauth_client_customer_admin_email', $username)
      ->save();
    \Drupal::configFactory()
      ->getEditable('oauth2_login.settings')
      ->set('miniorange_oauth_client_customer_admin_phone', $phone)
      ->save();
    \Drupal::configFactory()
      ->getEditable('oauth2_login.settings')
      ->set('miniorange_oauth_client_customer_admin_password', $password)
      ->save();
    $send_otp_response = json_decode($customer_config
      ->sendOtp());
    if ($send_otp_response->status == 'SUCCESS') {
      \Drupal::configFactory()
        ->getEditable('oauth2_login.settings')
        ->set('miniorange_oauth_client_tx_id', $send_otp_response->txId)
        ->save();
      $current_status = 'VALIDATE_OTP';
      \Drupal::configFactory()
        ->getEditable('oauth2_login.settings')
        ->set('miniorange_oauth_client_status', $current_status)
        ->save();
      \Drupal::messenger()
        ->addMessage(t('Verify email address by entering the passcode sent to @username', [
        '@username' => $username,
      ]));
    }
  }
  elseif ($check_customer_response->status == 'CURL_ERROR') {
    \Drupal::messenger()
      ->addMessage(t('cURL is not enabled. Please enable cURL'), 'error');
  }
  else {
    $customer_keys_response = json_decode($customer_config
      ->getCustomerKeys());
    if (json_last_error() == JSON_ERROR_NONE) {
      \Drupal::configFactory()
        ->getEditable('oauth2_login.settings')
        ->set('miniorange_oauth_client_customer_id', $customer_keys_response->id)
        ->save();
      \Drupal::configFactory()
        ->getEditable('oauth2_login.settings')
        ->set('miniorange_oauth_client_customer_admin_token', $customer_keys_response->token)
        ->save();
      \Drupal::configFactory()
        ->getEditable('oauth2_login.settings')
        ->set('miniorange_oauth_client_customer_admin_email', $username)
        ->save();
      \Drupal::configFactory()
        ->getEditable('oauth2_login.settings')
        ->set('miniorange_oauth_client_customer_admin_phone', $phone)
        ->save();
      \Drupal::configFactory()
        ->getEditable('oauth2_login.settings')
        ->set('miniorange_oauth_client_customer_api_key', $customer_keys_response->apiKey)
        ->save();
      $current_status = 'PLUGIN_CONFIGURATION';
      \Drupal::configFactory()
        ->getEditable('oauth2_login.settings')
        ->set('miniorange_oauth_client_status', $current_status)
        ->save();
      \Drupal::messenger()
        ->addMessage(t('Successfully retrieved your account.'));
    }
    elseif ($check_customer_response->status == 'TRANSACTION_LIMIT_EXCEEDED') {
      \Drupal::messenger()
        ->addMessage(t('An error has been occured. Please try after some time or contact us at <a href="mailto:drupalsupport@xecurify.com" target="_blank">drupalsupport@xecurify.com</a>.'), 'error');
      return;
    }
    else {
      \Drupal::messenger()
        ->addMessage(t('Invalid credentials.'), 'error');
      return;
    }
  }
}