-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomationModel.php
More file actions
208 lines (185 loc) · 4.48 KB
/
AutomationModel.php
File metadata and controls
208 lines (185 loc) · 4.48 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
<?php
namespace App\Model;
use App\Entity\Automation;
use App\Entity\Flow;
use App\Model\Interface\Configurable;
use App\Model\Interface\Exportable;
use App\Model\Interface\Persistable;
use App\Model\Trait\Config;
use App\Model\Trait\Data;
use App\Model\Trait\Entity;
use App\Model\Trait\Format;
use App\Model\Trait\Ref;
use App\Model\Trait\Relations;
use App\Service\Formatter;
use App\Service\Slug;
use App\Service\Tasks;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* @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 Flow getFlow()
* @method setFlow( Flow $flow )
*/
class AutomationModel implements Exportable, Configurable, Persistable
{
use Entity {
persist as private entityPersist;
}
use Ref;
use Config;
use Data;
use Relations;
use Format;
public function __construct( Automation $automation )
{
$this->entity = $automation;
$this->config = $automation->getConfig();
$this->data = $automation->getData();
}
public function persist( EntityManagerInterface $entityManager, $flush = false ): void
{
// Parse endpoint slug.
$this->setEndpoint( $this->getEndpoint() );
$this->entityPersist( $entityManager, $flush );
}
public function handleRequest( Request $request ): Response
{
return new Response();
}
public function setEndpoint( string $endpoint ): void
{
$this->entity->setEndpoint( ( new Slug() )->slugify( $endpoint ) );
}
public function isRunning(): bool
{
return (bool) $this->getData( 'running' );
}
public function setRunning( bool $running ): void
{
$this->setData( $running, 'running' );
}
public function getLimit(): int
{
return (int) $this->getConfig( 'limit' );
}
public function setLimit( int $limit ): void
{
$this->setConfig( $limit, 'limit' );
}
public function getOffset(): bool
{
return (bool) $this->getData( 'offset' );
}
public function setOffset(): void
{
$this->setData( $this->getIteration() * $this->getLimit(), 'offset' );
}
public function getIterator(): array
{
if ( ! $this->getConfig( 'iterator' ) ) {
return [];
}
return [
'current' => $this->getIteration(),
'limit' => $this->getLimit(),
'offset' => $this->getOffset(),
];
}
public function getIteration(): int
{
return (int) $this->getData( 'iteration' );
}
public function setIteration( int $iteration ): void
{
$this->setData( $iteration, 'iteration' );
$this->setOffset();
}
public function nextIteration(): void
{
$iteration = $this->getIteration();
$this->setIteration( ++ $iteration );
}
public function endIterator(): void
{
$this->setIteration( 0 );
}
public static function getFields(): array
{
// @todo Implement fields.
return [
'source' => [
'label' => 'Source data for this automation',
'fields' => [
'source' => [
'label' => '',
'type' => 'switch',
'inline' => true,
'choices' => [
'request' => 'Request',
'tasks' => 'Retrieve',
//'dataset' => 'Dataset', @todo Choice conditionals.
],
],
'request' => [
'label' => 'Request',
'conditionals' => [
'source' => [ 'request' ],
],
'nested' => [
'format' => ( new Formatter() )->getFormatDecodeField(),
'param' => [
'label' => 'Request param',
'type' => 'text',
],
],
],
'source_tasks' => [
'label' => 'Retrieve tasks',
'type' => 'tasks',
'default' => [
[
'_class' => Tasks::getTask( 'Retrieve' )->getClassName(),
],
],
'conditionals' => [
'source' => [ 'tasks' ],
],
],
],
],
'iterator' => [
'label' => 'Run automation in batches',
'type' => 'switch',
'fields' => [
'limit' => [
'label' => 'Limit batch size',
'help' => 'Limit the number of records to fetch/run at once.',
'type' => 'number',
'required' => true,
'conditionals' => [
'iterator' => true,
],
'fields' => [
'async' => [
'label' => 'Run batches asynchronous',
'type' => 'checkbox',
],
],
],
],
],
];
}
public static function getEntityClass(): string
{
return Automation::class;
}
}