View source
<?php
namespace Drupal\tmgmt_smartling\Plugin\QueueWorker;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\tmgmt_smartling\Context\ContextUploader;
use Psr\Log\LoggerInterface;
use Drupal\Core\Config\ConfigFactory;
class ContextUpload extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected $contextUploader;
protected $queue;
protected $logger;
protected $config;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ContextUploader $context_uploader, QueueInterface $queue, LoggerInterface $logger, ConfigFactory $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->contextUploader = $context_uploader;
$this->queue = $queue;
$this->logger = $logger;
$this->config = $config_factory
->get('tmgmt.translator.smartling');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('tmgmt_smartling.utils.context.uploader'), $container
->get('queue')
->get('smartling_context_upload', TRUE), $container
->get('logger.channel.smartling'), $container
->get('config.factory'));
}
public function processItem($data) {
if (!$data['job_id']) {
return;
}
$job_id = $data['job_id'];
$url = $data['url'];
$filename = $data['filename'];
$job = \Drupal::entityTypeManager()
->getStorage('tmgmt_job')
->load($job_id);
if ($job && $job
->hasTranslator()) {
$settings = $job
->getTranslator()
->getSettings();
}
else {
$this->logger
->warning("Job with ID=@id has no translator plugin.", [
'@id' => $job_id,
]);
return;
}
if (empty($settings['user_id']) || empty($settings['project_id']) || empty($settings['token_secret'])) {
$this->logger
->warning('Skipping uploading context for the job with id = @id because credentials are not correct.', [
'@id' => $job_id,
]);
return;
}
if (!$this->contextUploader
->isReadyAcceptContext($filename, $settings)) {
$data['counter'] = isset($data['counter']) ? $data['counter'] + 1 : 1;
$this->queue
->createItem($data);
return;
}
try {
$this->contextUploader
->upload($url, $filename, $settings);
} catch (\Exception $e) {
$this->logger
->error($e
->getMessage());
return [];
}
}
}