Process.php
4.16 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace WPML\TM\ATE\Sync;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\TM\API\Job\Map;
use WPML\TM\API\Jobs;
use WPML\TM\ATE\Download\Job;
use WPML\TM\ATE\Log\EventsTypes;
use WPML_TM_ATE_API;
use WPML_TM_ATE_Job_Repository;
use WPML\TM\ATE\Log\Storage;
use WPML\TM\ATE\Log\Entry;
use function WPML\FP\pipe;
class Process {
const LOCK_RELEASE_TIMEOUT = 1 * MINUTE_IN_SECONDS;
/** @var WPML_TM_ATE_API $api */
private $api;
/** @var WPML_TM_ATE_Job_Repository $ateRepository */
private $ateRepository;
public function __construct( WPML_TM_ATE_API $api, WPML_TM_ATE_Job_Repository $ateRepository ) {
$this->api = $api;
$this->ateRepository = $ateRepository;
}
/**
* @param Arguments $args
*
* @return Result
*/
public function run( Arguments $args ) {
$result = new Result();
if ( $args->page ) {
$result = $this->runSyncOnPages( $result, $args );
} else {
$includeManualAndLongstandingJobs = (bool) Obj::propOr( true , 'includeManualAndLongstandingJobs', $args);
$result = $this->runSyncInit( $result, $includeManualAndLongstandingJobs );
}
return $result;
}
/**
* This will run the sync on extra pages.
*
* @param Result $result
* @param Arguments $args
*
* @return Result
*/
private function runSyncOnPages( Result $result, Arguments $args ) {
$apiPage = $args->page - 1; // ATE API pagination starts at 0.
$data = $this->api->sync_page( $args->ateToken, $apiPage );
$jobs = Obj::propOr( [], 'items', $data );
$result->jobs = $this->handleJobs( $jobs );
if ( !$result->jobs ){
Storage::add( Entry::createForType(
EventsTypes::JOBS_SYNC,
[
'numberOfPages' => $args->numberOfPages,
'page' => $args->page,
'downloadQueueSize' => $result->downloadQueueSize,
'nextPage' => $result->nextPage,
]
) );
}
if ( $args->numberOfPages > $args->page ) {
$result->nextPage = $args->page + 1;
$result->numberOfPages = $args->numberOfPages;
$result->ateToken = $args->ateToken;
}
return $result;
}
/**
* This will run the first sync iteration.
* We send all the job IDs we want to sync.
*
* @param Result $result
* @param boolean $includeManualAndLongstandingJobs
*
* @return Result
*/
private function runSyncInit( Result $result, $includeManualAndLongstandingJobs = true ) {
$ateJobIds = $this->getAteJobIdsToSync( $includeManualAndLongstandingJobs );
if ( $ateJobIds ) {
$this->ateRepository->increment_ate_sync_count( $ateJobIds );
$data = $this->api->sync_all( $ateJobIds );
$jobs = Obj::propOr( [], 'items', $data );
$result->jobs = $this->handleJobs( $jobs );
if ( isset( $data->next->pagination_token, $data->next->pages_number ) ) {
$result->ateToken = $data->next->pagination_token;
$result->numberOfPages = $data->next->pages_number;
$result->nextPage = 1; // We start pagination at 1 to avoid carrying a falsy value.
}
}
return $result;
}
/**
* @param boolean $includeManualAndLongstandingJobs
*
* @return array
*/
private function getAteJobIdsToSync( $includeManualAndLongstandingJobs = true ) {
return $this->ateRepository
->get_jobs_to_sync( $includeManualAndLongstandingJobs )
->map_to_property( 'editor_job_id' );
}
/**
* @param array $items
*
* @return Job[] $items
*/
private function handleJobs( array $items ) {
$setStatus = function ( $status ) {
return pipe(
Fns::tap( Jobs::setStatus( Fns::__, $status ) ),
Obj::assoc('status', $status)
);
};
$updateStatus = Logic::cond( [
[
Relation::propEq( 'status', \WPML_TM_ATE_API::CANCELLED_STATUS ),
$setStatus( ICL_TM_NOT_TRANSLATED ),
],
[
Relation::propEq( 'status', \WPML_TM_ATE_API::SHOULD_HIDE_STATUS ),
$setStatus( ICL_TM_ATE_CANCELLED ),
],
[
Fns::always( true ),
Fns::identity(),
]
] );
return wpml_collect( $items )
->map( [ Job::class, 'fromAteResponse' ] )
->map( Obj::over( Obj::lensProp( 'jobId' ), Map::fromRid() ) ) // wpmlJobId returned by ATE endpoint represents RID column in wp_icl_translation_status
->map( $updateStatus )
->toArray();
}
}