-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetrieve.php
More file actions
102 lines (87 loc) · 3.02 KB
/
Retrieve.php
File metadata and controls
102 lines (87 loc) · 3.02 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
<?php
namespace SyncEngine\Task;
use SyncEngine\Model\ConnectionModel;
use SyncEngine\Model\WebserviceModel;
use SyncEngine\Service\ExecuteContext;
use SyncEngine\Service\ExecuteData;
use SyncEngine\Structure\Data\ConfigData;
use SyncEngine\Task\Abstract\AbstractRequest;
use SyncEngine\Task\Type\RequestTaskType;
use SyncEngine\Webservice\Helper\Result;
class Retrieve extends AbstractRequest
{
public function __construct()
{
parent::__construct();
$this->type = RequestTaskType::TYPE;
$this->icon = 'task-retrieve';
$this->name = $this->trans( 'Retrieve' );
$this->description = $this->trans( 'Retrieve your data from your specific connection' );
}
function getFields(): array
{
return [
'key' => [
'label' => $this->trans( 'Key / Column name' ),
'help' => [
$this->trans( 'The data column name to pass to the webservice' ),
$this->trans( 'Nested keys are supported: {example}', [ 'example' => 'key.nested_key', ] ),
$this->trans( 'Leave empty for root' ),
],
'type' => 'text', // @todo Column/Key selection field type.
'taggable' => true,
],
'connection' => [
'label' => $this->trans( 'Connection' ),
'type' => 'entity',
'entity' => 'connection',
'config' => 'webservice:retrieve',
'actions' => [ 'edit', 'create' ],
],
'response' => [
'label' => $this->trans( 'Response data' ),
'nested' => $this->getResponseFields(),
],
];
}
public function execute( ConfigData $config, ExecuteContext $context, ExecuteData $data ): ExecuteData
{
$connectionConfig = $config['connection'];
$result = null;
$key = $config['key'] ?? null;
$package = $data->get( $key );
if ( $key && empty( $package ) ) {
// Only log error if a key was defined.
$context->addLog( $this->trans( 'Data not found: {key}', [ 'key' => $key ] ), [ 'data' => $data ] );
} elseif ( ! is_scalar( $package ) ) {
$package = $data->normalize( $package );
}
$time_start = microtime( true );
try {
if ( ! empty( $connectionConfig['id'] ) ) {
$connection = ConnectionModel::get( $connectionConfig['id'] );
$result = $connection->handleRetrieve( $connectionConfig, $context, $package );
} else {
// @todo Custom webservice without Connection?
$webservice = WebserviceModel::get( $connectionConfig['_class'] );
$result = $webservice->retrieve( $connectionConfig, $package );
}
/** @var Result $result */
// Do not translate for storage.
$context->addLog( 'Response info', $result );
} catch ( \Throwable $e ) {
$time_end = microtime( true );
$info = [
'data' => $package,
'time_start' => $time_start,
'time_end' => $time_end,
'time_duration' => $time_end - $time_start,
];
$context->addError( $e, $info );
}
$responseConfig = $config['response'] ?? [];
$return = $this->handleResult( $result, (array) $responseConfig, $data );
// @todo Option to include in current storage?
return ExecuteData::create( $return );
}
}