You are here

public function LingotekApi::addDocument in Lingotek Translation 8

Same name and namespace in other branches
  1. 8.2 src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  2. 4.0.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  3. 3.0.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  4. 3.1.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  5. 3.2.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  6. 3.3.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  7. 3.4.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  8. 3.5.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  9. 3.6.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  10. 3.7.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()
  11. 3.8.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addDocument()

Overrides LingotekApiInterface::addDocument

File

src/Remote/LingotekApi.php, line 81
Contains \Drupal\lingotek\Remote\LingotekApi.

Class

LingotekApi

Namespace

Drupal\lingotek\Remote

Code

public function addDocument($args) {
  try {
    $this->logger
      ->debug('Lingotek::addDocument (POST /api/document) called with ' . var_export($args, TRUE));
    $response = $this->lingotekClient
      ->post('/api/document', $args, TRUE);
  } catch (\Exception $e) {
    $this->logger
      ->error('Error adding document: %message.', [
      '%message' => $e
        ->getMessage(),
    ]);
    throw new LingotekApiException('Error adding document: ' . $e
      ->getMessage());
  }
  if ($response
    ->getStatusCode() == Response::HTTP_ACCEPTED) {
    $data = json_decode($response
      ->getBody(), TRUE);
    $this->logger
      ->debug('addDocument response received, code %code and body %body', [
      '%code' => $response
        ->getStatusCode(),
      '%body' => (string) $response
        ->getBody(TRUE),
    ]);
    if (!empty($data['properties']['id'])) {
      return $data['properties']['id'];
    }
  }

  // TODO: log warning
  return FALSE;
}