You are here

public function BucketJobManager::handle in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8.3 src/Smartling/BucketJobManager.php \Drupal\tmgmt_smartling\Smartling\BucketJobManager::handle()

Parameters

array $jobs:

Translator $translator:

Return value

array

File

src/Smartling/BucketJobManager.php, line 85
BucketJobManager.

Class

BucketJobManager
Class BucketJobManager.

Namespace

Drupal\tmgmt_smartling\Smartling

Code

public function handle(array $jobs, Translator $translator) {
  $this->settings = $translator
    ->getSettings();
  $this->apiWrapper
    ->setSettings($this->settings);
  $job_uid = NULL;
  $job_name = $this
    ->getName();
  $response = $this->apiWrapper
    ->listJobs($job_name, [
    JobStatus::AWAITING_AUTHORIZATION,
    JobStatus::IN_PROGRESS,
    JobStatus::COMPLETED,
  ]);

  // Try to find the latest created bucket job.
  if (!empty($response['items'])) {
    $job_uid = $response['items'][0]['translationJobUid'];
  }

  // If there is no existing bucket job then create new one.
  if (empty($job_uid)) {
    $job_uid = $this->apiWrapper
      ->createJob($job_name, (string) t('Bucket job: contains updated content.'));

    // If there is a CANCELED/CLOSED bucket job then we have to come with new
    // job name in order to avoid "Job name is already taken" error.
    if (empty($job_uid)) {
      $job_name = $this
        ->getName(' ' . date('H:i:s'));
      $job_uid = $this->apiWrapper
        ->createJob($job_name, (string) t('Bucket job: contains updated content.'));
    }
  }
  if (empty($job_uid)) {
    $this->logger
      ->error(t("Queueing file upload into the bucket job failed: can't find/create job.")
      ->render());
    return [
      'batch_uid' => FALSE,
      'batch_execute_on_job' => FALSE,
    ];
  }
  $batch_uid = $this->apiWrapper
    ->createBatch($job_uid, $this->settings['auto_authorize_locales']);
  if (empty($batch_uid)) {
    $this->logger
      ->error(t("Queueing file upload into the bucket job failed: can't create batch.")
      ->render());
    return [
      'batch_uid' => FALSE,
      'batch_execute_on_job' => FALSE,
    ];
  }
  $jobs_in_batch = [];
  $last_job = end($jobs);
  foreach ($jobs as $log_job) {
    $jobs_in_batch[] = $log_job
      ->id();
  }
  Drupal::getContainer()
    ->get('logger.channel.smartling')
    ->info(t('Batch info (track entity changes): uid = "@batch_uid", jobs count = "@jobs_count", jobs = "@jobs_in_batch", execute on job = "@batch_execute_on_job"', [
    '@batch_uid' => $batch_uid,
    '@jobs_count' => count($jobs_in_batch),
    '@jobs_in_batch' => implode(', ', $jobs_in_batch),
    '@batch_execute_on_job' => $last_job
      ->id(),
  ])
    ->render());
  return [
    'batch_uid' => $batch_uid,
    'batch_execute_on_job' => $last_job
      ->id(),
  ];
}