You are here

public function ContextUploader::upload in TMGMT Translator Smartling 8.3

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.2 src/Context/ContextUploader.php \Drupal\tmgmt_smartling\Context\ContextUploader::upload()

Parameters

string $url:

string $filename:

array $proj_settings:

Return value

bool

Throws

\Drupal\tmgmt_smartling\Exceptions\EmptyContextParameterException

File

src/Context/ContextUploader.php, line 100

Class

ContextUploader

Namespace

Drupal\tmgmt_smartling\Context

Code

public function upload($url, $filename = '', $proj_settings = []) {
  $response = [];
  $api_wrapper = $this
    ->getApiWrapper($proj_settings);
  if (empty($url)) {
    $api_wrapper
      ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
      "message" => t('Context upload failed: context url is empty.'),
      "type" => "error",
    ]);
    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';
  $error_message = t('Error while uploading context for file @filename. See logs for more info.', [
    '@filename' => $filename,
  ])
    ->render();

  // 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, $filename);
      $this
        ->uploadContextMissingResources($smartling_context_directory, $proj_settings);
      if (!empty($response)) {
        $this->logger
          ->info('Context upload for file @filename completed successfully.', [
          '@filename' => $filename,
        ]);
        $api_wrapper
          ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
          "message" => t('Context upload for file @filename completed successfully.', [
            '@filename' => $filename,
          ])
            ->render(),
          "type" => "status",
        ]);
      }
      else {
        $api_wrapper
          ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
          "message" => $error_message,
          "type" => "error",
        ]);
      }
    }
    else {
      $this->logger
        ->error("Can't save context file: @path", [
        '@path' => $smartling_context_file,
      ]);
      $api_wrapper
        ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
        "message" => $error_message,
        "type" => "error",
      ]);
    }
  } catch (Exception $e) {
    $this->logger
      ->error($e
      ->getMessage());
    $api_wrapper
      ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
      "message" => $error_message,
      "type" => "error",
    ]);
  }
  return $response;
}