You are here

public function LingotekDownloaderQueueWorker::processItem in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()
  2. 4.0.x src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()
  3. 3.0.x src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()
  4. 3.1.x src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()
  5. 3.2.x src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()
  6. 3.3.x src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()
  7. 3.5.x src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()
  8. 3.6.x src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()
  9. 3.7.x src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()
  10. 3.8.x src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php \Drupal\lingotek\Plugin\QueueWorker\LingotekDownloaderQueueWorker::processItem()

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

src/Plugin/QueueWorker/LingotekDownloaderQueueWorker.php, line 25

Class

LingotekDownloaderQueueWorker
Provides base functionality for the Lingotek Queue Workers.

Namespace

Drupal\lingotek\Plugin\QueueWorker

Code

public function processItem($data) {
  $locale = $data['locale'];
  $entity_type_id = $data['entity_type_id'];
  $entity_id = $data['entity_id'];
  $document_id = $data['document_id'];
  $entity = \Drupal::entityTypeManager()
    ->getStorage($entity_type_id)
    ->load($entity_id);
  if ($entity instanceof ConfigEntityInterface) {

    /** @var \Drupal\lingotek\LingotekConfigTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.config_translation');
  }
  elseif ($entity instanceof ContentEntityInterface) {

    /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.content_translation');
  }
  if (empty($translation_service)) {
    $message = new FormattableMarkup('Can not download - entity (@instance) is not supported instance of a class', [
      '@instance' => gettype($entity),
    ]);
    \Drupal::logger('lingotek')
      ->error($message);
    throw new \Exception($message);
  }
  $translation_service
    ->setTargetStatus($entity, $locale, Lingotek::STATUS_READY);
  $download = $translation_service
    ->downloadDocument($entity, $locale);
  if ($download === FALSE || $download === NULL) {
    $message = new FormattableMarkup('No download for target @locale happened in document @document on @entity @bundle @id.', [
      '@locale' => $locale,
      '@document' => $document_id,
      '@entity' => $entity
        ->label(),
      '@id' => $entity
        ->id(),
      '@bundle' => $entity
        ->bundle(),
    ]);
    \Drupal::logger('lingotek')
      ->error($message);
    throw new \Exception($message);
  }
}