BackgroundTaskViewModel.php
1.75 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
<?php
namespace WPML\BackgroundTask;
use WPML\Core\BackgroundTask\Model\TaskEndpointInterface;
use WPML\Core\BackgroundTask\Model\BackgroundTask;
use function WPML\Container\make;
class BackgroundTaskViewModel {
/**
* Prepares the endpoint data for the react component to consume it.
*
* @param BackgroundTask $backgroundTask
* @param bool $getLock
*
* @return ?array{
* isPaused: bool,
* progressTotal: int,
* progressDone: int,
* payload: object,
* taskId: int,
* taskStatus: string,
* isCompleted: bool,
* description: string,
* taskType: string,
* hasLock: bool
* }
**/
public static function get( $backgroundTask, $getLock = false ) {
$className = $backgroundTask->getTaskType();
if ( ! class_exists( $className ) ) {
return;
}
/** @var TaskEndpointInterface $endpointInstance */
$endpointInstance = make( $className );
$endpointLock = make( 'WPML\Utilities\Lock', [ ':name' => $backgroundTask->getTaskType() ] );
$hasLock = $getLock ? $endpointLock->create( $endpointInstance->getLockTime() ) : false;
return [
'isPaused' => $backgroundTask->isStatusPaused(),
'progressTotal' => $backgroundTask->getTotalCount(),
'progressDone' => $backgroundTask->getCompletedCount(),
'payload' => $backgroundTask->getPayload(),
'taskId' => $backgroundTask->getTaskId(),
'taskStatus' => $backgroundTask->getStatusName(),
'isCompleted' => $backgroundTask->isStatusCompleted(),
'description' => $endpointInstance->getDescription( wpml_collect( $backgroundTask->getPayload() ) ),
'taskType' => $backgroundTask->getTaskType(),
'isDisplayed' => $endpointInstance->isDisplayed(),
'hasLock' => $hasLock,
];
}
}