first commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\ATE\Download;
|
||||
|
||||
use Exception;
|
||||
use WPML\FP\Obj;
|
||||
use WPML\TM\ATE\ReturnedJobsQueue;
|
||||
use WPML_TM_ATE_API;
|
||||
use WPML_TM_ATE_Jobs;
|
||||
|
||||
class Consumer {
|
||||
|
||||
/** @var WPML_TM_ATE_API $ateApi */
|
||||
private $ateApi;
|
||||
|
||||
/** @var WPML_TM_ATE_Jobs $ateJobs */
|
||||
private $ateJobs;
|
||||
|
||||
public function __construct( WPML_TM_ATE_API $ateApi, WPML_TM_ATE_Jobs $ateJobs ) {
|
||||
$this->ateApi = $ateApi;
|
||||
$this->ateJobs = $ateJobs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $job
|
||||
*
|
||||
* @return array|\stdClass|false
|
||||
* @throws Exception
|
||||
*/
|
||||
public function process( $job ) {
|
||||
$xliffContent = $this->ateApi->get_remote_xliff_content( Obj::prop('url', $job), $job );
|
||||
$wpmlJobId = $this->ateJobs->apply( $xliffContent );
|
||||
|
||||
if ( $wpmlJobId ) {
|
||||
$processedJob = Obj::assoc( 'jobId', $wpmlJobId, $job );
|
||||
|
||||
ReturnedJobsQueue::remove( $wpmlJobId );
|
||||
|
||||
return $processedJob;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\ATE\Download;
|
||||
|
||||
class Job {
|
||||
|
||||
/** @var int $ateJobId */
|
||||
public $ateJobId;
|
||||
|
||||
/** @var string $url */
|
||||
public $url;
|
||||
|
||||
/** @var int */
|
||||
public $ateStatus;
|
||||
|
||||
/**
|
||||
* This property is not part of the database data,
|
||||
* but it can be added when the job is downloaded
|
||||
* to provide more information to the UI.
|
||||
*
|
||||
* @var int $jobId
|
||||
*/
|
||||
public $jobId;
|
||||
|
||||
/** @var int */
|
||||
public $status = ICL_TM_IN_PROGRESS;
|
||||
|
||||
/**
|
||||
* @param \stdClass $item
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public static function fromAteResponse( \stdClass $item ) {
|
||||
$job = new self();
|
||||
$job->ateJobId = $item->ate_id;
|
||||
$job->url = $item->download_link;
|
||||
$job->ateStatus = (int) $item->status;
|
||||
$job->jobId = (int) $item->id;
|
||||
|
||||
return $job;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $row
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public static function fromDb( \stdClass $row ) {
|
||||
$job = new self();
|
||||
$job->ateJobId = $row->editor_job_id;
|
||||
$job->url = $row->download_url;
|
||||
|
||||
return $job;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\ATE\Download;
|
||||
|
||||
use Exception;
|
||||
use WPML\Collect\Support\Collection;
|
||||
use WPML\FP\Fns;
|
||||
use WPML\FP\Lst;
|
||||
use WPML\FP\Obj;
|
||||
use WPML\TM\ATE\Log\Entry;
|
||||
use WPML\TM\ATE\Log\EventsTypes;
|
||||
use WPML_TM_ATE_API;
|
||||
|
||||
class Process {
|
||||
/** @var Consumer $consumer */
|
||||
private $consumer;
|
||||
|
||||
/** @var WPML_TM_ATE_API $ateApi */
|
||||
private $ateApi;
|
||||
|
||||
public function __construct( Consumer $consumer, WPML_TM_ATE_API $ateApi ) {
|
||||
$this->consumer = $consumer;
|
||||
$this->ateApi = $ateApi;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $jobs
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function run( $jobs ) {
|
||||
$jobs = \wpml_collect( $jobs )->map( function ( $job ) {
|
||||
$processedJob = null;
|
||||
|
||||
try {
|
||||
$processedJob = $this->consumer->process( $job );
|
||||
|
||||
if ( ! $processedJob ) {
|
||||
global $iclTranslationManagement;
|
||||
$message = 'The translation job could not be applied.';
|
||||
|
||||
if ( $iclTranslationManagement->messages_by_type( 'error' ) ) {
|
||||
|
||||
$iclTranslationManagementError = implode( ' ', Lst::pluck( 'text', $iclTranslationManagement->messages_by_type( 'error' ) ) );
|
||||
$message .= ' ' . $iclTranslationManagementError;
|
||||
}
|
||||
|
||||
throw new Exception( $message );
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
|
||||
$this->logException( $e, $processedJob ?: $job );
|
||||
}
|
||||
|
||||
return $processedJob;
|
||||
} )->filter()->values();
|
||||
|
||||
$this->acknowledgeAte( $jobs );
|
||||
|
||||
return $jobs;
|
||||
}
|
||||
|
||||
private function acknowledgeAte( Collection $processedJobs ) {
|
||||
if ( $processedJobs->count() ) {
|
||||
$this->ateApi->confirm_received_job( $processedJobs->pluck( 'ateJobId' )->toArray() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Exception $e
|
||||
* @param Job|null $job
|
||||
*/
|
||||
private function logException( Exception $e, $job = null ) {
|
||||
$entry = new Entry();
|
||||
$entry->description = $e->getMessage();
|
||||
|
||||
if ( $job ) {
|
||||
$entry->ateJobId = Obj::prop('ateJobId', $job);
|
||||
$entry->wpmlJobId = Obj::prop('jobId', $job);
|
||||
$entry->extraData = [ 'downloadUrl' => Obj::prop('url', $job) ];
|
||||
}
|
||||
|
||||
if ( $e instanceof \Requests_Exception ) {
|
||||
$entry->eventType = EventsTypes::SERVER_XLIFF;
|
||||
} else {
|
||||
$entry->eventType = EventsTypes::JOB_DOWNLOAD;
|
||||
}
|
||||
|
||||
wpml_tm_ate_ams_log( $entry );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user