You are here

public function ContextUploader::upload in TMGMT Translator Smartling 8.2

Same name and namespace in other branches
  1. 8.4 src/Context/ContextUploader.php \Drupal\tmgmt_smartling\Context\ContextUploader::upload()
  2. 8 src/Context/ContextUploader.php \Drupal\tmgmt_smartling\Context\ContextUploader::upload()
  3. 8.3 src/Context/ContextUploader.php \Drupal\tmgmt_smartling\Context\ContextUploader::upload()

Parameters

string $url:

string $filename:

array $proj_settings:

Return value

bool

Throws

EmptyContextParameterException

File

src/Context/ContextUploader.php, line 84

Class

ContextUploader

Namespace

Drupal\tmgmt_smartling\Context

Code

public function upload($url, $filename = '', $proj_settings = []) {
  $response = [];
  if (empty($url)) {
    throw new EmptyContextParameterException('Context url must be a non-empty field.');
  }
  $smartling_context_directory = $proj_settings['scheme'] . '://tmgmt_smartling_context';
  $smartling_context_file = $smartling_context_directory . '/' . str_replace('.', '_', $filename) . '.html';

  // Upload context body.
  try {
    $html = $this
      ->getContextualizedPage($url, $proj_settings);

    // Save context file.
    if (file_prepare_directory($smartling_context_directory, FILE_CREATE_DIRECTORY) && ($file = file_save_data($html, $smartling_context_file, FILE_EXISTS_REPLACE))) {
      $response = $this
        ->uploadContextBody($url, $file, $proj_settings);
      $this
        ->uploadContextMissingResources($smartling_context_directory, $proj_settings);
      if (!empty($response)) {
        $this->logger
          ->info('Context upload for file @filename completed successfully.', [
          '@filename' => $filename,
        ]);
      }
    }
    else {
      $this->logger
        ->error("Can't save context file: @path", [
        '@path' => $smartling_context_file,
      ]);
    }
  } catch (SmartlingApiException $e) {
    $this->logger
      ->error($e
      ->getMessage());
    return [];
  } catch (SmartlingBaseException $e) {
    $this->logger
      ->error($e
      ->getMessage());
    return [];
  }
  return $response;
}