You are here

public function LingotekSetupController::handshake in Lingotek Translation 3.8.x

Same name and namespace in other branches
  1. 8 src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  2. 8.2 src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  3. 4.0.x src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  4. 3.0.x src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  5. 3.1.x src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  6. 3.2.x src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  7. 3.3.x src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  8. 3.4.x src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  9. 3.5.x src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  10. 3.6.x src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
  11. 3.7.x src/Controller/LingotekSetupController.php \Drupal\lingotek\Controller\LingotekSetupController::handshake()
1 string reference to 'LingotekSetupController::handshake'
lingotek.routing.yml in ./lingotek.routing.yml
lingotek.routing.yml

File

src/Controller/LingotekSetupController.php, line 34

Class

LingotekSetupController
Returns responses for lingotek module setup routes.

Namespace

Drupal\lingotek\Controller

Code

public function handshake(Request $request) {
  if (Request::METHOD_POST === $request
    ->getMethod()) {
    $body = Json::decode($request
      ->getContent());
    if (isset($body['access_token'])) {
      $config = \Drupal::configFactory()
        ->getEditable('lingotek.settings');
      $config
        ->set('account.access_token', $body['access_token']);
      $config
        ->set('account.use_production', TRUE);
      $config
        ->save();
      $account_info = $this
        ->fetchAccountInfo();
      $this
        ->saveAccountInfo($account_info);
      $this
        ->messenger()
        ->addStatus($this
        ->t('Your account settings have been saved.'));
      $this->logger
        ->notice('Account connected to Lingotek.');
      return new JsonResponse([
        'status' => TRUE,
        'message' => 'Account connected to Lingotek.',
      ]);
    }
    else {
      return new JsonResponse([
        'status' => FALSE,
        'message' => 'Account not connected to Lingotek.',
      ]);
    }
  }
  elseif (Request::METHOD_GET === $request
    ->getMethod()) {

    // Is a GET.
    $config = \Drupal::config('lingotek.settings');
    if ($config
      ->get('account.access_token')) {

      // No need to show the username and token if everything worked correctly
      // Just go to the community page
      return $this
        ->redirect('lingotek.setup_community');
    }

    // Is a GET, but don't have the token yet.
    return [
      '#type' => 'markup',
      '#markup' => $this
        ->t('Connecting... Please wait to be redirected'),
      '#attached' => [
        'library' => [
          'lingotek/lingotek.connect',
        ],
      ],
    ];
  }
}