FixJob.php
3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace WPML\TM\ATE\REST;
use WPML\TM\ATE\ReturnedJobsQueue;
use WP_REST_Request;
use WPML\Rest\Adaptor;
use WPML\TM\REST\Base;
use WPML_TM_ATE_AMS_Endpoints;
use \WPML_TM_ATE_API;
use \WPML_TM_ATE_Jobs;
use \WPML_TM_Jobs_Repository;
use WPML\FP\Obj;
use WPML\TM\ATE\API\RequestException;
use WPML\TM\ATE\Log\Entry;
use WPML\TM\ATE\Log\EventsTypes;
class FixJob extends Base {
/**
* @var WPML_TM_ATE_Jobs
*/
private $ateJobs;
/**
* @var WPML_TM_ATE_API
*/
private $ateApi;
/**
* @var WPML_TM_Jobs_Repository
*/
private $jobsRepository;
const PARAM_ATE_JOB_ID = 'ateJobId';
const PARAM_WPML_JOB_ID = 'jobId';
public function __construct( Adaptor $adaptor, WPML_TM_ATE_API $ateApi, WPML_TM_ATE_Jobs $ateJobs ) {
parent::__construct( $adaptor );
$this->ateApi = $ateApi;
$this->ateJobs = $ateJobs;
$this->jobsRepository = wpml_tm_get_jobs_repository();
}
/**
* @return array
*/
public function get_routes() {
return [
[
'route' => WPML_TM_ATE_AMS_Endpoints::FIX_JOB,
'args' => [
'methods' => 'GET',
'callback' => [ $this, 'fix_job' ],
],
],
];
}
/**
* @param WP_REST_Request $request
*
* @return array
*/
public function get_allowed_capabilities( WP_REST_Request $request ) {
return [
'manage_options',
'manage_translations',
'translate',
];
}
/**
* @param WP_REST_Request $request
*
* @return bool[]
*/
public function fix_job( WP_REST_Request $request ) {
try {
$ateJobId = $request->get_param( self::PARAM_ATE_JOB_ID );
$wpmlJobId = $request->get_param( self::PARAM_WPML_JOB_ID );
$processedJobResult = $this->process( $ateJobId, $wpmlJobId );
if ( $processedJobResult ) {
return [ 'completed' => true, 'error' => false ];
}
} catch ( \Exception $e ) {
$this->logException( $e, [ 'ateJobId' => $ateJobId, 'wpmlJobId' => $wpmlJobId ] );
return [ 'completed' => false, 'error' => true ];
}
return [ 'completed' => false, 'error' => false ];
}
/**
* Processes the job status.
*
* @param $ateJobId
* @param $wpmlJobId
*
* @return bool
* @throws RequestException
*/
public function process( $ateJobId, $wpmlJobId ) {
$ateJob = $this->ateApi->get_job( $ateJobId )->$ateJobId;
$xliffUrl = Obj::prop('translated_xliff', $ateJob);
if ( $xliffUrl ) {
$xliffContent = $this->ateApi->get_remote_xliff_content( $xliffUrl, [ 'jobId' => $wpmlJobId, 'ateJobId' => $ateJobId ] );
$receivedWpmlJobId = $this->ateJobs->apply( $xliffContent );
if ( $receivedWpmlJobId && intval( $receivedWpmlJobId ) !== intval( $wpmlJobId ) ) {
$error_message = sprintf( 'The received wpmlJobId (%s) does not match (%s).', $receivedWpmlJobId, $wpmlJobId );
throw new \Exception( $error_message );
}
if ( $receivedWpmlJobId ) {
ReturnedJobsQueue::remove( $wpmlJobId );
return true;
}
}
return false;
}
/**
* @param \Exception $e
* @param array|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('wpmlJobId', $job);
$entry->extraData = [ 'downloadUrl' => Obj::prop('url', $job) ];
}
if ( $e instanceof RequestException ) {
$entry->eventType = EventsTypes::SERVER_XLIFF;
} else {
$entry->eventType = EventsTypes::JOB_DOWNLOAD;
}
wpml_tm_ate_ams_log( $entry );
}
}