You are here

function tmgmt_smartling_download_file in TMGMT Translator Smartling 8.2

Same name and namespace in other branches
  1. 8.4 tmgmt_smartling.module \tmgmt_smartling_download_file()
  2. 8 tmgmt_smartling.module \tmgmt_smartling_download_file()
  3. 8.3 tmgmt_smartling.module \tmgmt_smartling_download_file()
3 calls to tmgmt_smartling_download_file()
PushCallbackController::callback in src/Controller/PushCallbackController.php
SmartlingTranslator::downloadTranslation in src/Plugin/tmgmt/Translator/SmartlingTranslator.php
Downloads translation file and applies it.
tmgmt_smartling_download_file_submit in ./tmgmt_smartling.module

File

./tmgmt_smartling.module, line 25
Contains

Code

function tmgmt_smartling_download_file(JobInterface $job) {
  try {
    $smartlingApi = $job
      ->getTranslatorPlugin()
      ->getApi($job
      ->getTranslator());
    $retrieval_type = $job
      ->getTranslator()
      ->getSetting('retrieval_type');
    $filename = $job
      ->getTranslatorPlugin()
      ->getFileName($job);
    $downloadParameters = new DownloadFileParameters();
    $downloadParameters
      ->set('retrievalType', $retrieval_type);
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    $xml = $smartlingApi
      ->downloadFile($filename, $job
      ->getRemoteTargetLanguage(), $downloadParameters);
  } catch (\Exception $e) {
    Drupal::logger('tmgmt_smartling')
      ->error($e
      ->getMessage());
    return FALSE;
  }
  $path = $job
    ->getSetting('scheme') . '://tmgmt_smartling_translations/' . $job
    ->getTranslatorPlugin()
    ->getFileName($job);
  $dirname = dirname($path);
  if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY) && ($file = file_save_data($xml, $path, FILE_EXISTS_REPLACE))) {
    $plugin = \Drupal::service('plugin.manager.tmgmt_file.format')
      ->createInstance($extension);
    if ($plugin) {

      // Validate the file on job.
      if (!$plugin
        ->validateImport($file
        ->getFileUri(), $job)) {
        $job
          ->addMessage('Failed to validate file @file. Import for job @job_id aborted.', [
          '@file' => $file
            ->getFileUri(),
          '@job_id' => $job
            ->id(),
        ], 'error');
        \Drupal::logger('tmgmt_smartling')
          ->error('Failed to validate file @file. Import for job @job_id aborted.', [
          '@file' => $file
            ->getFileUri(),
          '@job_id' => $job
            ->id(),
        ]);
        return FALSE;
      }
      else {
        try {

          // Compare old and new hashes in order to decide should we apply
          // translation or not. In other words we do not want to apply
          // downloaded translation if it's the same as it was.
          $old_hash = $job
            ->get('job_file_content_hash')
            ->getValue();
          $old_hash = !empty($old_hash[0]['value']) ? $old_hash[0]['value'] : '';
          $hash = md5($xml);
          if ($old_hash !== $hash) {
            $job
              ->set('job_file_content_hash', $hash);

            // Set active state for all job items of a job in order to be able
            // force translation. It allows to override existing translations
            // that might be in an "Accepted" state.
            // @see JobItem::addTranslatedData() method.
            foreach ($job
              ->getItems() as $item) {
              $item
                ->setState(JobItemInterface::STATE_ACTIVE);
            }

            // Validation successful, start import.
            $job
              ->addTranslatedData($plugin
              ->import($file
              ->getFileUri(), $job));
            $job
              ->addMessage('Successfully imported file.');
            \Drupal::logger('tmgmt_smartling')
              ->info('Translation for "@filename" was successfully downloaded and imported.', [
              '@filename' => $filename,
            ]);
          }
          else {
            $job
              ->addMessage('Import of downloaded file was skipped: downloaded and existing translations are equal.');
            \Drupal::logger('tmgmt_smartling')
              ->warning('Translation for "@filename" was successfully downloaded but import was skipped: downloaded and existing translations are equal.', [
              '@filename' => $filename,
            ]);
          }
        } catch (Exception $e) {
          $job
            ->addMessage('File import failed with the following message: @message', [
            '@message' => $e
              ->getMessage(),
          ], 'error');
          \Drupal::logger('tmgmt_smartling')
            ->error('File import failed with the following message: @message', [
            '@message' => $e
              ->getMessage(),
          ]);
          return FALSE;
        }
      }
    }
  }
  return TRUE;
}