/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $config = $this->config('acquia_connector.settings');
     $identifier = $config->get('identifier');
     $key = $config->get('key');
     $client = \Drupal::service('acquia_connector.client');
     $error = NULL;
     try {
         $data = $client->nspiCall('/agent-migrate-api/subscription/migration/environments', array('identifier' => $identifier), $key);
     } catch (ConnectorException $e) {
         if ($e->isCustomized()) {
             acquia_connector_report_restapi_error($e->getCustomMessage('code'), $e->getCustomMessage());
             return $this->redirect('acquia_connector.settings');
         }
         $error = $this->t('Server error, please submit again.');
     }
     if (!empty($data['result'])) {
         // Response is in $data['result'].
         $result = $data['result'];
         if (!empty($result['is_error'])) {
             $error = $this->t('Server error, unable to retrieve environments for migration');
         } elseif (!empty($result['body']['error'])) {
             $error = $result['body']['error'];
         } elseif (empty($result['body']['environments'])) {
             $error = $this->t('Server error, unable to retrieve environments for migration');
         }
     }
     if ($error) {
         drupal_set_message($error, 'error');
         return $this->redirect('acquia_connector.settings');
     }
     foreach ($result['body']['environments'] as $stage => $env) {
         $result['body']['environments'][$stage]['secret'] = base64_decode($env['secret']);
     }
     $form['envs'] = array('#type' => 'value', '#value' => $result['body']['environments']);
     $envs = array();
     foreach (array_keys($result['body']['environments']) as $env) {
         $envs[$env] = Unicode::ucfirst($env);
     }
     if (count($result['body']['environments']) > 1) {
         $form['environment'] = array('#type' => 'select', '#title' => $this->t('Select environment for migration'), '#options' => $envs, '#description' => $this->t('Select which environment your site should be migrated to. Only environments that are running trunk or branch can be overwritten by migration. Environments running a tag are not included.'));
     } else {
         $form['environment'] = array('#markup' => $this->t('Available environment for migration: %env', array('%env' => array_pop($envs))));
     }
     $form['migrate_files'] = array('#type' => 'checkbox', '#title' => $this->t('Migrate files directory'), '#description' => $this->t('Include files directory and all files in migration. If you are experiencing migration errors it is recommended you do not send the files directory.'), '#default_value' => $config->get('migrate.files'));
     $form['reduce_db_size'] = array('#type' => 'checkbox', '#title' => $this->t('Reduce database export size'), '#description' => $this->t('Reduce the database export size by excluding the data from cache, session, and watchdog tables. If you are experiencing migration errors this is recommended. Table structure will still be exported.'), '#default_value' => 0);
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Migrate'));
     $form['actions']['cancel'] = array('#type' => 'submit', '#value' => $this->t('Cancel'), '#submit' => ['::submitMigrateCancel']);
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     try {
         $response = $this->client->nspiCall('/agent-api/subscription', array('identifier' => trim($form_state->getValue('acquia_identifier'))), trim($form_state->getValue('acquia_key')));
     } catch (ConnectorException $e) {
         // Set form error to prevent switching to the next page.
         if ($e->isCustomized()) {
             // Allow to connect with expired subscription
             if ($e->getCustomMessage('code') == Subscription::EXPIRED) {
                 $form_state->setValue('subscription', 'Expired subscription.');
                 return;
             }
             acquia_connector_report_restapi_error($e->getCustomMessage('code'), $e->getCustomMessage());
             $form_state->setErrorByName('');
         } else {
             $form_state->setErrorByName('', t('Server error, please submit again.'));
         }
         return;
     }
     $response = $response['result'];
     if (empty($response['body']['subscription_name'])) {
         $form_state->setErrorByName('acquia_identifier', t('No subscriptions were found.'));
     } else {
         $form_state->setValue('subscription', $response['body']['subscription_name']);
     }
 }
 /**
  * Complete migration tasks.
  */
 public function complete(&$migration)
 {
     $config = \Drupal::config('acquia_connector.settings');
     $identifier = $config->get('identifier');
     $key = $config->get('key');
     $client = \Drupal::service('acquia_connector.client');
     $body = array('identifier' => $identifier);
     if (isset($migration['redirect']) && is_array($migration['redirect']['data'])) {
         $body += $migration['redirect']['data'];
     }
     try {
         $data = $client->nspiCall('/agent-migrate-api/subscription/migration/complete', $body, $key);
     } catch (ConnectorException $e) {
         if ($e->getCustomMessage('code')) {
             acquia_connector_report_restapi_error($e->getCustomMessage('code'), $e->getCustomMessage());
             $migration['error'] = TRUE;
             return;
         }
         $migration['error'] = t("Server error, please submit again.");
         return;
     }
     // Response is in $data['result'].
     $result = $data['result'];
     if (!empty($result['success'])) {
         $migration['network_url'] = $result['network_url'];
     } else {
         $migration['error'] = $result['error'];
     }
     return $migration;
 }