-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStore.php
More file actions
169 lines (149 loc) · 4.65 KB
/
Store.php
File metadata and controls
169 lines (149 loc) · 4.65 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
164
165
166
167
168
169
<?php
namespace SyncEngine\Task;
use SyncEngine\Model\StorageModel;
use SyncEngine\Model\TaskModel;
use SyncEngine\Service\ExecuteContext;
use SyncEngine\Service\ExecuteData;
use SyncEngine\Structure\Data\ConfigData;
use SyncEngine\Structure\Data\ResourceData;
use SyncEngine\Task\Type\StorageTaskType;
class Store extends TaskModel
{
public function __construct()
{
parent::__construct();
$this->type = StorageTaskType::TYPE;
$this->icon = 'task-store';
$this->name = $this->trans( 'Store' );
$this->description = $this->trans( 'Get or set a storage' );
}
public function getFields(): array
{
return [
'key' => [
'label' => $this->trans( 'Key / Column name' ),
'type' => 'text', // @todo Column/Key selection field type.
'help' => [
$this->trans( 'Nested keys are supported: {example}', [ 'example' => 'key.nested_key', ] ),
$this->trans( 'Leave empty for root' ),
],
'taggable' => true,
],
'action' => [
'label' => $this->trans( 'Action' ),
'type' => 'select',
'default' => 'set',
'required' => true,
'choices' => [
'set' => $this->trans( 'Set storage' ),
'get' => $this->trans( 'Get storage' ),
],
],
'storage' => [
'label' => $this->trans( 'Storage' ),
'type' => 'entity',
'entity' => 'storage',
'actions' => [ 'edit', 'create' ],
'required' => true,
],
'path' => [
'label' => $this->trans( 'Storage column key/path' ),
'help' => $this->trans( 'Set the path where this value will be stored or leave empty. Use dots (.) to traverse into the storage.' ),
'type' => 'text',
'taggable' => true,
],
'method' => [
'label' => $this->trans( 'Method' ),
'type' => 'select',
'default' => 'replace',
'choices' => [
'replace' => $this->trans( 'Replace' ),
'merge' => $this->trans( 'Merge' ),
'append' => $this->trans( 'Append' ),
],
'conditions' => [ 'action' => 'set' ],
],
'not_found' => [
'label' => $this->trans( 'Not found action' ),
'help' => $this->trans( 'Action if the tag is not found' ),
'type' => 'select',
'default' => 'skip',
'choices' => [
'override' => $this->trans( 'Override with empty value' ),
'skip' => $this->trans( 'Skip task' ),
],
'conditions' => [ 'action' => 'get' ],
],
];
}
public function execute( ConfigData $config, ExecuteContext $context, ExecuteData $data ): ExecuteData
{
if ( empty( $config['storage'] ) ) {
$context->addError( $this->trans( 'No storage selected' ) );
return $data;
}
$storage = StorageModel::get( $config['storage'] );
if ( ! $storage ) {
$context->addError( $this->trans( 'Storage not found' ) );
return $data;
}
$key = $config['key'] ?? '';
$action = $config['action'] ?? false;
$path = $config['path'] ?? null;
$not_found = $config['not_found'] ?? '';
if ( 'get' === $action ) {
if ( $path ) {
if ( $storage->isRaw() ) {
$context->addError( $this->trans( 'Raw storage cannot contain paths' ) );
}
$value = $storage->getData( $path );
} else {
$value = $storage->getData();
}
if ( $value || 'override' === $not_found ) {
if ( $key ) {
$data->set( $value, $key );
} else {
$data = new ExecuteData( $value );
}
}
} else {
$value = $data->get( $key ?? null );
if ( null === $value ) {
if ( 'override' !== $not_found ) {
return $data; // @todo Enable not found for setter, currently disabled.
}
if ( ! $storage->isRaw() ) {
$data = [];
}
}
if ( ! is_iterable( $value ) && ! $path ) {
// @todo Trigger error instead of potentially losing data?
// Enforce raw type since it's not an array.
$storage->setType( 'raw' );
$storage->setData( [ 'value' => $value ] );
} else {
if ( $storage->isRaw() ) {
$context->addError( $this->trans( 'This is a raw storage, please select a different storage or change the storage type' ) );
}
switch ( $config['method'] ?? '' ) {
case 'append':
$resource = new ResourceData( $storage->getData( $path, [] ) );
$resource->append( $value );
$storage->setData( $resource->normalize(), $path );
break;
case 'merge':
$resource = new ResourceData( $storage->getData( $path, [] ) );
$resource->merge( $value );
$storage->setData( $resource->normalize(), $path );
break;
default:
$storage->setData( is_scalar( $value ) ? $value : ResourceData::create( $value )->normalize(), $path );
break;
}
}
$storage->persist( true );
}
return $data;
}
}