tmgmt_extension_suit-8.x-3.4/src/Plugin/QueueWorker/JobDownload.php

src/Plugin/QueueWorker/JobDownload.php
<?php

namespace Drupal\tmgmt_extension_suit\Plugin\QueueWorker;

use Drupal\file\Entity\File;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Entity\JobItem;
use Drupal\tmgmt_extension_suit\ExtendedTranslatorPluginInterface;

/**
 * Executes interface translation queue tasks.
 *
 * @QueueWorker(
 *   id = "tmgmt_extension_suit_download",
 *   title = @Translation("Job translation download"),
 *   cron = {"time" = 30}
 * )
 */
class JobDownload extends QueueWorkerLockedBase {

  /**
   * {@inheritdoc}
   */
  protected function doProcessItem(array $data) {
    $tjid = isset($data['tjid']) ? $data['tjid'] : $data['id'];
    $tjiid = isset($data['tjiid']) ? $data['tjiid'] : NULL;
    $fid = isset($data['fid']) ? $data['fid'] : NULL;

    try {
      $job = Job::load($tjid);
      $jobItem = NULL;
      $file = NULL;

      if (empty($job)) {
        $this->logger->error(t('Downloading translation for a job :job_id is failed: non-existent job. This job has been deleted from admin UI but queue item is still in the queue.', [
          ':job_id' => $tjid,
        ])->render());
        return;
      }

      if (!empty($tjiid)) {
        $jobItem = Jobitem::load($tjiid);

        if (empty($jobItem)) {
          $this->logger->error(t('Downloading translation for a job :job_id and jobItem :job_item_id is failed: non-existent job item. This job item has been deleted from admin UI but queue item is still in the queue.',
            [
              ':job_id' => $tjid,
              ':job_item_id' => $tjiid,
            ])->render());
          return;
        }
      }

      if (!empty($fid)) {
        $file = File::load($fid);

        if (empty($file)) {
          $this->logger->error(t('Downloading translation for a job :job_id and file :file_id is failed: non-existent file. This file has been deleted from admin UI but queue item is still in the queue.',
            [
              ':job_id' => $tjid,
              ':file_id' => $fid,
            ])->render());
          return;
        }
      }

      $plugin = $job->getTranslator()->getPlugin();

      if ($plugin instanceof ExtendedTranslatorPluginInterface) {
        // TMGMT Job translation.
        if (!empty($job) && empty($file)) {
          if ($plugin->isReadyForDownload($job)) {
            $plugin->downloadTranslation($job, $jobItem);
          }
        }

        // Attachment file translation.
        if (!empty($job) && !empty($file)) {
          if ($plugin->isAttachmentReadyForDownload($job, $file)) {
            $plugin->downloadAttachmentTranslation($job, $file);
          }
        }
      }
    }
    catch (\Exception $e) {
      $this->logger->error($e->getMessage());
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc