You are here

public function SmartlingTranslator::isReadyForDownload in TMGMT Translator Smartling 8.2

Same name and namespace in other branches
  1. 8.4 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::isReadyForDownload()
  2. 8 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::isReadyForDownload()
  3. 8.3 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::isReadyForDownload()

Checks if file is ready for download.

Parameters

JobInterface $job:

Return value

bool

File

src/Plugin/tmgmt/Translator/SmartlingTranslator.php, line 470
Contains \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator.

Class

SmartlingTranslator
Smartling translator plugin.

Namespace

Drupal\tmgmt_smartling\Plugin\tmgmt\Translator

Code

public function isReadyForDownload(JobInterface $job) {
  $result = FALSE;
  try {
    $api = $this
      ->getApi($job
      ->getTranslator());
    $filename = $this
      ->getFileName($job);
    $locale = $job
      ->getRemoteTargetLanguage();
    $request_result = $api
      ->getStatus($filename, $locale);
    $authorized = intval($request_result['authorizedStringCount']);
    $completed = intval($request_result['completedStringCount']);
    $progress = $authorized + $completed > 0 ? (int) ($completed / ($authorized + $completed) * 100) : 0;
    $file_last_modified = 0;
    $job_last_modified = intval($job
      ->getChangedTime());
    $last_modified = $api
      ->lastModified($filename);
    foreach ($last_modified['items'] as $item) {
      if ($item['localeId'] == $locale) {
        $file_last_modified = $item['lastModified']
          ->getTimeStamp();
        break;
      }
    }

    // Chain of calls:
    // 1. Check status worker: SmartlingTranslator::isReadyForDownload().
    // 2. Download worker: SmartlingTranslator::isReadyForDownload() ->
    // SmartlingTranslator::downloadTranslation().
    //
    // In Drupal 7 connector we don't have second check "isReadyForDownload"
    // so there is a simpler condition - just if $file_last_modified GRATER
    // than $job_last_modified. In Drupal 8 connector we have a second call
    // of SmartlingTranslator::isReadyForDownload() so we need to take into
    // consideration this case.
    //
    // If $file_last_modified == $job_last_modified then it means that
    // this is second call of SmartlingTranslator::isReadyForDownload() for
    // this job (from JobDownload::processItem(), in other words it's happened
    // from download queue. In this case we should "lock" this check by
    // setting job's changed time as current time. Otherwise here will be
    // infinite loop of downloading process ($job_last_modified will always
    // be equal to $file_last_modified)).
    if ($file_last_modified >= $job_last_modified && $progress === 100) {

      // Case when $file_last_modified > $job_last_modified - the first check
      // "isReadyForDownload" from check status queue worker. Unlock the
      // condition for the second "isReadyForDownload" check from download
      // queue worker.
      if ($file_last_modified > $job_last_modified) {
        $job
          ->set('changed', $file_last_modified);
      }
      else {
        $job
          ->set('changed', time());
      }
      $job
        ->save();
      $result = TRUE;
    }
    else {
      Drupal::logger('tmgmt_smartling')
        ->warning(t('File @file is not ready for download.', [
        '@file' => $this
          ->getFileName($job),
      ]));
    }
  } catch (\Exception $e) {
    watchdog_exception('tmgmt_smartling', $e);
  }
  \Drupal::logger('tmgmt_smartling')
    ->info('Check status for file: @filename found @approved pending words and @completed completed ones', [
    '@filename' => $filename,
    '@approved' => @$request_result['authorizedStringCount'],
    '@completed' => @$request_result['completedStringCount'],
  ]);
  return $result;
}