You are here

public function LingotekFake::uploadDocument in Lingotek Translation 3.8.x

Same name and namespace in other branches
  1. 8 tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  2. 8.2 tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  3. 4.0.x tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  4. 3.0.x tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  5. 3.1.x tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  6. 3.2.x tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  7. 3.3.x tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  8. 3.4.x tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  9. 3.5.x tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  10. 3.6.x tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()
  11. 3.7.x tests/modules/lingotek_test/src/LingotekFake.php \Drupal\lingotek_test\LingotekFake::uploadDocument()

Uploads a document to the Lingotek service.

Parameters

string $title: The title of the document as it will be seen in the TMS.

string|array $content: The content of the document. It can be a json string or an array that will be json encoded.

string $locale: The Lingotek locale.

string $url: (optional) The document url in the site if any. This allows support for in-context review.

\Drupal\lingotek\LingotekProfileInterface $profile: (optional) The profile being used.

string $job_id: (optional) The job ID that will be associated.

string &$process_id: (optional) It will be filled with the process id on the TMS.

Return value

string The document ID assigned to the uploaded document.

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekInterface::uploadDocument

File

tests/modules/lingotek_test/src/LingotekFake.php, line 142

Class

LingotekFake

Namespace

Drupal\lingotek_test

Code

public function uploadDocument($title, $content, $locale, $url = NULL, LingotekProfileInterface $profile = NULL, $job_id = NULL, &$process_id = NULL) {
  if (\Drupal::state()
    ->get('lingotek.must_error_in_upload', FALSE)) {
    \Drupal::state()
      ->set('lingotek.must_error_in_upload', FALSE);
    throw new LingotekApiException('Error was forced.');
  }
  if (\Drupal::state()
    ->get('lingotek.must_payment_required_error_in_upload', FALSE)) {
    \Drupal::state()
      ->set('lingotek.must_payment_required_error_in_upload', FALSE);
    throw new LingotekPaymentRequiredException('Error was forced.');
  }
  if (is_array($content)) {
    $content = json_encode($content);
  }

  // If the upload is successful, we must return a valid hash.
  \Drupal::state()
    ->set('lingotek.uploaded_title', $title);
  \Drupal::state()
    ->set('lingotek.uploaded_content', $content);
  \Drupal::state()
    ->set('lingotek.uploaded_locale', $locale);
  \Drupal::state()
    ->set('lingotek.uploaded_url', $url);
  \Drupal::state()
    ->set('lingotek.uploaded_job_id', $job_id);
  \Drupal::state()
    ->set('lingotek.used_profile', $profile ? $profile
    ->id() : NULL);
  $count = \Drupal::state()
    ->get('lingotek.uploaded_docs', 0);
  $doc_id = 'dummy-document-hash-id';
  if ($count > 0) {
    $doc_id .= '-' . $count;
  }
  ++$count;
  \Drupal::state()
    ->set('lingotek.last_used_id', $count);
  \Drupal::state()
    ->set('lingotek.uploaded_docs', $count);

  // Save the timestamp of the upload.
  $timestamps = \Drupal::state()
    ->get('lingotek.upload_timestamps', []);
  $timestamps[$doc_id] = \Drupal::time()
    ->getRequestTime();
  \Drupal::state()
    ->set('lingotek.upload_timestamps', $timestamps);

  // Set a process id, same as doc.
  $process_id = $doc_id;
  return $doc_id;
}