You are here

public function SmartlingTranslator::isReadyForDownload in TMGMT Translator Smartling 8.3

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.2 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 676
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 {
    $settings = $job
      ->getTranslator()
      ->getSettings();
    $api_wrapper = $this
      ->getApiWrapper($settings);
    $filename = $this
      ->getFileName($job);
    $locale = $job
      ->getRemoteTargetLanguage();
    $request_result = $api_wrapper
      ->getApi('file')
      ->getStatus($filename, $locale);
    $progress = $this->translationProgressCalculator
      ->calculate($request_result, $settings["auto_authorize_locales"]);
    $file_last_modified = 0;
    $job_last_modified = intval($job
      ->getChangedTime());
    $last_modified = $api_wrapper
      ->getApi('file')
      ->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 {
      $this->logger
        ->warning(t('File @file is not ready for download.', [
        '@file' => $this
          ->getFileName($job),
      ])
        ->render());
    }
    $this->logger
      ->info('Check status for file: @filename found @approved pending words and @completed completed ones', [
      '@filename' => $filename,
      '@approved' => $request_result['authorizedStringCount'],
      '@completed' => $request_result['completedStringCount'],
    ]);
  } catch (SmartlingApiException $e) {
    $this
      ->handleSmartlingApiException($e, $job);
  } catch (\Exception $e) {
    watchdog_exception('tmgmt_smartling', $e);
  }
  return $result;
}