-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomationModel.php
More file actions
584 lines (538 loc) · 15.1 KB
/
AutomationModel.php
File metadata and controls
584 lines (538 loc) · 15.1 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<?php
namespace SyncEngine\Model;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use SyncEngine\Entity\Automation;
use SyncEngine\Entity\Trace;
use SyncEngine\Model\Abstract\EngineModel;
use SyncEngine\Model\Enum\AutomationEventType;
use SyncEngine\Model\Enum\TraceStatus;
use SyncEngine\Model\Interface\Supervisable;
use SyncEngine\Model\Interface\Taggable;
use SyncEngine\Model\Trait\Format;
use SyncEngine\Model\Trait\Supervisor;
use SyncEngine\Model\Trait\Tags;
use SyncEngine\Service\DataFormatter;
use SyncEngine\Service\ExecuteContext;
use SyncEngine\Service\Format\SlugFormatter;
use SyncEngine\Structure\Data\IterationData;
use SyncEngine\Task\Trigger;
/**
* @extends EngineModel<Automation>
*
* @method int getId()
* @method setId( int $id )
* @method string getName()
* @method setName( string $name )
* @method string getDescription()
* @method setDescription( string $description )
* @method string getEndpoint()
* @method Collection<int, Trace> getTraces()
* @method addTrace( Trace $trace )
* @method removeTrace( Trace $trace )
*/
class AutomationModel extends EngineModel implements Taggable, Supervisable
{
use Format;
use Tags;
use Supervisor;
public function __construct( ?Automation $automation = null )
{
parent::__construct( $automation );
}
public function validate(): bool
{
$success = parent::validate();
if ( $success ) {
// Parse endpoint slug.
$this->setEndpoint( $this->getEndpoint() );
}
return $success;
}
public function handleRequest( Request $request ): Response
{
return new Response();
}
public function getVariables(): array
{
return $this->getConfig( 'variables', [] ) ?: [];
}
public function getActions(): array
{
$actions = $this->getConfig( 'actions', [] );
return match( $actions ) {
'flow', 'routine' => [ [
'_class' => Trigger::_getClassLocator(),
'_ref' => $this->getRef() . '-action-' . $actions,
'override_data' => true,
'pass_data' => true,
'action' => $actions,
$actions => $this->getConfig( $actions ),
] ],
'tasks' => $this->getConfig( $actions ),
default => (array) $actions,
};
}
public function setActions( array $config ): void
{
// @todo Task validator?
$this->setConfig( $config, 'actions' );
}
public function getEventActions( string $event ): ?array
{
// @todo TaskModelCollection object.
return $this->getConfig( 'events.on_' . $event, [] );
}
public function setEndpoint( string $endpoint ): void
{
$this->entity->setEndpoint(
( new SlugFormatter( [ SlugFormatter::CASE => 'lower', SlugFormatter::SEPARATOR => '-' ] ) )->format( $endpoint )
);
}
public function reset(): void
{
$this->setRunning( false );
$this->setCurrentIteration( 0 );
}
public function isRunning(): bool
{
return (bool) $this->getData( 'running' );
}
public function setRunning( bool $running ): void
{
$this->setData( $running, 'running' );
}
public function isScheduled(): bool
{
// @todo Use query/repository instead?
return ! empty( $this->getTraces()?->findFirst( function( $id, Trace $trace ) {
return $trace->getStatus() === TraceStatus::SCHEDULED;
} ) );
}
public function getEventTimestamps(): array
{
return (array) $this->getData( 'events', [] );
}
public function getEventTimestamp( string|AutomationEventType|null $state = null ): int
{
return (int) $this->getData( 'events.' . AutomationEventType::create( $state )->value, 0 );
}
public function setEventTimestamp( string|AutomationEventType $state ): void
{
$state = AutomationEventType::create( $state );
if ( $state ) {
$this->setData( time(), 'events.' . $state->value );
}
}
public function getLimit(): int
{
return (int) $this->getConfig( 'limit' );
}
public function setLimit( int $limit ): void
{
$this->setConfig( $limit, 'limit' );
}
public function getOffset(): int
{
return (int) $this->getData( 'offset' );
}
public function setOffset(): void
{
$index = $this->getCurrentIteration() - 1;
$limit = $this->getLimit();
$offset = 0 < $index ? $index * $limit : 0;
$this->setData( $offset, 'offset' );
}
public function hasIterator(): bool
{
return ! empty( $this->getConfig( 'iterator' ) );
}
public function enableIterator(): static
{
$this->setConfig( true, 'iterator' );
return $this;
}
public function disableIterator(): static
{
$this->setConfig( false, 'iterator' );
return $this;
}
public function getIteration(): IterationData
{
if ( ! $this->hasIterator() ) {
return IterationData::fromArray( [
'current' => 0,
'index' => -1, // @todo implement index.
'limit' => 0,
'offset' => 0,
] );
}
// @todo DTO instead of array.
return IterationData::fromArray( [
'current' => $this->getCurrentIteration(),
'index' => $this->getCurrentIteration() - 1, // @todo implement index.
'limit' => $this->getLimit(),
'offset' => $this->getOffset(),
] );
}
public function getCurrentIteration(): int
{
return (int) $this->getData( 'iteration' );
}
public function setCurrentIteration( int $iteration ): void
{
$this->setData( $iteration, 'iteration' );
$this->setOffset();
}
public function nextIteration(): void
{
$iteration = $this->getCurrentIteration();
$this->setCurrentIteration( ++ $iteration );
}
public function endIterator(): void
{
$this->setCurrentIteration( 0 );
}
public function getInterval(): int
{
return (int) $this->getConfig( 'interval' );
}
public function getFields(): array
{
return [
'variables' => [
'icon' => 'variable',
'label' => $this->trans( 'Variables' ),
'description' => $this->trans( 'Define static variables to be used within the automation.' ),
'type' => 'params',
'collapsed' => true,
// 'taggable' => true, @todo Support variable tags. Requirement is to filter the available tags.
],
'_triggers' => [
'icon' => 'source',
'label' => $this->trans( 'Source' ),
'description' => $this->trans( 'Select the data source for this automation' ),
'collapsed' => true,
'fields' => [
'source' => [
'label' => '',
'type' => 'switch',
'inline' => true,
'required' => false,
'choices' => [
'request' => [
'text' => $this->trans( 'Request' ),
'icon' => 'source-request',
'help' => $this->trans( 'Extract data from the current request body. Cannot be used with scheduled automations.' ),
],
'retrieve' => [
'text' => $this->trans( 'Retrieve' ),
'icon' => 'retrieve',
'help' => $this->trans( 'Retrieve data from a webservice or other custom source.' ),
],
],
],
'__spacer' => [
'type' => 'separator',
'size' => 1,
'conditions' => [
'source' => [ 'operator' => 'not_empty' ],
],
],
'iterator' => [
'label' => [
'text' => $this->trans( 'Run automation in batches' ),
'icon' => 'repeat',
],
'type' => 'switch',
'conditions' => [
'source' => [ 'operator' => 'not_empty' ],
],
'fields' => [
'batch_method' => [
'label' => $this->trans( 'Batch method' ),
'type' => 'select',
'default' => 'remote',
'choices' => [
'remote' => $this->trans( 'Batches are made remotely using retrieve parameters' ),
'local' => $this->trans( 'Batches are created locally' ),
],
'conditions' => [
'iterator' => true,
],
],
'limit' => [
'label' => $this->trans( 'Limit batch size' ),
'help' => $this->trans( 'Limit the number of records to fetch/run at once.' ),
'type' => 'number',
'required' => true,
'conditions' => [
'iterator' => true,
],
/*'fields' => [
'async' => [
'label' => $this->trans( 'Run batches asynchronous' ),
'type' => 'checkbox',
],
],*/
],
'interval' => [
'label' => $this->trans( 'Interval' ),
'help' => $this->trans( 'Set an interval time between each batch.' ),
'type' => 'number',
'postfix' => $this->trans( 'Seconds' ),
'required' => false,
'conditions' => [
'iterator' => true,
],
],
],
],
'request' => [
'label' => $this->trans( 'Request' ),
'description' => $this->trans( 'Configure where to extract request data from.' ),
'icon' => 'source-request',
'conditions' => [
'source' => [
'compare' => 'request',
'operator' => 'contains'
],
],
'nested' => [
'format' => ( new DataFormatter() )->getFormatDecodeField(),
'param' => [
'label' => $this->trans( 'Request param' ),
'help' => [
$this->trans( 'Define the param name to use as the data source.' ),
$this->trans( 'Leave empty to use the full request data.' ),
],
'type' => 'text',
],
],
],
'retrieve' => [
'label' => $this->trans( 'Retrieve' ),
'icon' => 'retrieve',
'description' => $this->trans( 'Configure tasks to retrieve the data' ),
'type' => 'tasks',
'default' => [
[
'_class' => 'Retrieve',
],
],
'conditions' => [
'source' => [
'compare' => 'retrieve',
'operator' => 'contains'
],
],
],
'events' => [
'conditions' => [
'source' => [ 'operator' => 'not_empty' ],
],
'nested' => [
'error_on_empty' => [
'wrap' => true,
'label' => [
'text' => $this->trans( 'Event handler on empty source' ),
'icon' => 'event',
],
'toggle' => [
0 => [
'text' => $this->trans( 'Cancel' ),
'icon' => 'event-cancel',
],
1 => [
'text' => $this->trans( 'Error' ),
'icon' => 'event-error',
],
],
'description' => $this->trans(
'Optionally trigger error when there is no data available from the source.'
),
'type' => 'switch',
],
],
],
],
],
'_actions' => [
'icon' => 'actions',
'label' => $this->trans( 'Actions' ),
'description' => $this->trans( 'The actions that need to be done with the source data.' ),
'wrap' => true,
'collapsed' => true,
'fields' => [
'actions' => [
'type' => 'radio',
'inline' => true,
'choices' => [
'flow' => [
'text' => $this->trans( 'Flow' ),
'icon' => 'flow',
],
'routine' => [
'text' => $this->trans( 'Routine' ),
'icon' => 'routine',
],
'tasks' => [
'text' => $this->trans( 'Tasks' ),
'icon' => 'task',
],
],
],
'__spacer_actions' => [
'type' => 'separator',
'size' => 1,
'conditions' => [
'actions' => [ 'operator' => 'not_empty' ],
],
],
'flow' => [
'label' => $this->trans( 'Flow' ),
'type' => 'entity',
'entity' => 'flow',
'actions' => [ 'edit', 'create' ],
'conditions' => [
'actions' => 'flow',
],
],
'routine' => [
'label' => $this->trans( 'Routine' ),
'type' => 'entity',
'entity' => 'routine',
'actions' => [ 'edit', 'create' ],
'conditions' => [
'actions' => 'routine',
],
],
'tasks' => [
'icon' => 'task',
'label' => $this->trans( 'Tasks' ),
'description' => $this->trans( 'The actions that need to be done with the source data.' ),
'type' => 'tasks',
'conditions' => [
'actions' => 'tasks',
]
]
]
],
'events' => [
'icon' => 'event',
'label' => $this->trans( 'Events' ),
'description' => $this->trans( 'Select the behavior on various events in this automation.' ),
'collapsed' => true,
'nested' => [
'on_cancel' => [
'label' => [
'text' => $this->trans( 'On Cancel' ),
'icon' => 'event-cancel',
],
'description' => $this->trans( 'Actions to execute when the automation is cancelled.' ),
'collapsed' => true,
'type' => 'tasks',
],
'on_error' => [
'label' => [
'text' => $this->trans( 'On Error' ),
'icon' => 'event-error',
],
'description' => $this->trans( 'Actions to execute when the automation fails.' ),
'collapsed' => true,
'type' => 'tasks',
],
'on_success' => [
'label' => [
'text' => $this->trans( 'On Success' ),
'icon' => 'event-success',
],
'description' => $this->trans(
'Actions to execute when the automation completed successfully.'
),
'collapsed' => true,
'type' => 'tasks',
],
],
],
'response' => [
'icon' => 'response',
'label' => $this->trans( 'Response' ),
'description' => $this->trans( 'What this automation should respond in case of a HTTP request.' ),
'collapsed' => true,
'nested' => [
'type' => [
'label' => $this->trans( 'Type' ),
'type' => 'select',
'choices' => [
'' => $this->trans( 'Success and data' ),
'data' => $this->trans( 'Data only' ),
'success' => $this->trans( 'Success only' ),
'file' => $this->trans( 'File Download' ),
],
],
'file' => [
'nested' => [
'key' => [
'label' => $this->trans( 'Download file selector' ),
'type' => 'text',
'taggable' => true,
]
],
'conditions' => [
'type' => 'file',
],
],
],
],
];
}
public function getTags(): array
{
return [
'variables' => '_input',
'iteration' => [
'current' => 0,
'index' => 0,
'limit' => 0,
'offset' => 0,
],
'events' => [
'trigger' => [ 'timestamp' => 0 ],
'start' => [ 'timestamp' => 0 ],
'stop' => [ 'timestamp' => 0 ],
'success' => [ 'timestamp' => 0 ],
'error' => [ 'timestamp' => 0 ],
],
];
}
public function getTagsResource( $config = [], ?ExecuteContext $context = null ): array
{
$events = [
'trigger' => [ 'timestamp' => 0 ],
'start' => [ 'timestamp' => 0 ],
'stop' => [ 'timestamp' => 0 ],
'success' => [ 'timestamp' => 0 ],
'error' => [ 'timestamp' => 0 ],
];
foreach ( $this->getEventTimestamps() as $event => $timestamp ) {
$events[ $event ]['timestamp'] = $timestamp;
}
return [
'variables' => $this->getVariables(),
'iteration' => $this->getIteration(),
'events' => $events,
];
}
public function getSupportedSupervisors(): array
{
return [
'blueprint' => BlueprintModel::class,
];
}
public static function getEntityClass(): string
{
return Automation::class;
}
}