You are here

class GoogleTranslatorTestController in TMGMT Translator Google 8

Returns autocomplete responses for block categories.

Hierarchy

Expanded class hierarchy of GoogleTranslatorTestController

File

tmgmt_google_test/src/Controller/GoogleTranslatorTestController.php, line 16
Contains \Drupal\block\Controller\CategoryAutocompleteController.

Namespace

Drupal\tmgmt_google_test\Controller
View source
class GoogleTranslatorTestController {

  /**
   * Mock service to get available languages.
   */
  public function availableLanguages(Request $request) {
    if ($response = $this
      ->validateKey($request)) {
      return $response;
    }
    $response = array(
      'data' => array(
        'languages' => array(
          array(
            'language' => 'en',
          ),
          array(
            'language' => 'de',
          ),
          array(
            'language' => 'fr',
          ),
        ),
      ),
    );
    return new JsonResponse($response);
  }

  /**
   * Key validator helper.
   */
  protected function validateKey(Request $request) {
    if ($request
      ->get('key') != 'correct key') {
      return $this
        ->trigger_response_error('usageLimits', 'keyInvalid', 'Bad Request');
    }
  }

  /**
   * Helper to trigger mok response error.
   *
   * @param string $domain
   * @param string $reason
   * @param string $message
   * @param string $locationType
   * @param string $location
   */
  public function trigger_response_error($domain, $reason, $message, $locationType = NULL, $location = NULL) {
    $response = array(
      'error' => array(
        'errors' => array(
          'domain' => $domain,
          'reason' => $reason,
          'message' => $message,
        ),
        'code' => 400,
        'message' => $message,
      ),
    );
    if (!empty($locationType)) {
      $response['error']['errors']['locationType'] = $locationType;
    }
    if (!empty($location)) {
      $response['error']['errors']['location'] = $location;
    }
    return new JsonResponse($response);
  }

  /**
   * Mok service to translate request.
   */
  public function translate(Request $request) {
    $this
      ->validateKey($request);
    if (!$request->query
      ->has('-q')) {
      $this
        ->trigger_response_error('global', 'required', 'Required parameter: q', 'parameter', 'q');
    }
    if (!$request->query
      ->has('source')) {
      $this
        ->trigger_response_error('global', 'required', 'Required parameter: source', 'parameter', 'source');
    }
    if (!$request->query
      ->has('target')) {
      $this
        ->trigger_response_error('global', 'required', 'Required parameter: target', 'parameter', 'target');
    }
    $translations = array(
      'de' => 'Hallo Welt & willkommen',
      'fr' => 'Bonjour tout le monde',
    );
    $response = array(
      'data' => array(
        'translations' => array(
          array(
            'translatedText' => $translations[$_GET['target']],
          ),
        ),
      ),
    );
    return new JsonResponse($response);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GoogleTranslatorTestController::availableLanguages public function Mock service to get available languages.
GoogleTranslatorTestController::translate public function Mok service to translate request.
GoogleTranslatorTestController::trigger_response_error public function Helper to trigger mok response error.
GoogleTranslatorTestController::validateKey protected function Key validator helper.