Skip to content

Commit dde516e

Browse files
committed
get ready to implement background job
1 parent 2ce44b7 commit dde516e

24 files changed

+194
-65
lines changed

blueprints/pages/form.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use tobimori\DreamForm\DreamForm;
66
use tobimori\DreamForm\Support\Htmx;
77

8-
return fn () => [
8+
return fn() => [
99
'title' => 'dreamform.form',
1010
'image' => [
1111
'icon' => 'survey',
@@ -97,6 +97,13 @@
9797
'type' => 'toggle',
9898
'help' => 'dreamform.form.continueOnError.help',
9999
'width' => '1/2'
100+
],
101+
'runWorkflowInQueue' => [
102+
'label' => 'dreamform.form.runWorkflowInQueue.label',
103+
'type' => class_exists('tobimori\Queues\Job') ? 'toggle' : 'hidden',
104+
'default' => 'true',
105+
'help' => 'dreamform.form.runWorkflowInQueue.help',
106+
'width' => '1/2'
100107
]
101108
]
102109
],

classes/Actions/AbortAction.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class AbortAction extends Action
1010
public const TYPE = 'abort';
1111

1212
/**
13-
* Returns the Blocks fieldset blueprint for the actions' settings
13+
* @inheritDoc
1414
*/
1515
public static function blueprint(): array
1616
{
@@ -44,7 +44,7 @@ public static function blueprint(): array
4444
}
4545

4646
/**
47-
* Run the action
47+
* @inheritDoc
4848
*/
4949
public function run(): void
5050
{
@@ -54,4 +54,12 @@ public function run(): void
5454
$this->success();
5555
}
5656
}
57+
58+
/**
59+
* @inheritDoc
60+
*/
61+
public function supportsQueues(): bool
62+
{
63+
return false;
64+
}
5765
}

classes/Actions/Action.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ abstract class Action extends Performer
2222
* Create a new Action instance.
2323
* @internal
2424
*/
25-
public function __construct(private Block $block, private SubmissionPage $submission, private bool $force = false)
26-
{
27-
}
25+
public function __construct(private Block $block, private SubmissionPage $submission, private bool $force = false) {}
2826

2927
/**
3028
* Returns the submission the performer is being run on
@@ -108,6 +106,14 @@ protected function silentCancel(string|null $message = null, array|bool|null $lo
108106
);
109107
}
110108

109+
/**
110+
* Whether the action supports background processing via queues
111+
*/
112+
public function supportsQueues(): bool
113+
{
114+
return true;
115+
}
116+
111117
/**
112118
* Finish the form submission early
113119
*/

classes/Actions/BrevoAction.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@
88
use Kirby\Toolkit\Str;
99
use Kirby\Toolkit\V;
1010
use tobimori\DreamForm\DreamForm;
11-
use tobimori\DreamForm\Models\FormPage;
1211

12+
use function intval;
13+
use function count;
14+
15+
/**
16+
* Action for subscribing a user to a Brevo newsletter list
17+
*
18+
* @see https://developers.brevo.com/docs/getting-started
19+
*/
1320
class BrevoAction extends Action
1421
{
1522
public const TYPE = 'brevo';
1623

1724
/**
18-
* Returns the Blocks fieldset blueprint for the actions' settings
25+
* @inheritDoc
1926
*/
2027
public static function blueprint(): array
2128
{
@@ -46,7 +53,7 @@ public static function blueprint(): array
4653
'list' => [
4754
'label' => t('dreamform.actions.brevo.list.label'),
4855
'type' => 'select',
49-
'options' => A::reduce(static::getLists(), fn ($prev, $list) => A::merge($prev, [
56+
'options' => A::reduce(static::getLists(), fn($prev, $list) => A::merge($prev, [
5057
"id-{$list['id']}" => $list['name']
5158
]), []),
5259
'required' => true,
@@ -63,7 +70,7 @@ public static function blueprint(): array
6370
'type' => count(static::getTemplates()) > 0 ? 'select' : 'info',
6471
'width' => '1/2',
6572
'required' => count(static::getTemplates()) > 0,
66-
'options' => A::reduce(static::getTemplates(), fn ($prev, $template) => A::merge($prev, [
73+
'options' => A::reduce(static::getTemplates(), fn($prev, $template) => A::merge($prev, [
6774
"id-{$template['id']}" => $template['name']
6875
]), []),
6976
'help' => count(static::getTemplates()) > 0
@@ -166,7 +173,7 @@ public function run(): void
166173
}
167174

168175
$listId = intval(Str::replace($list, 'id-', ''));
169-
$listEntry = A::find(static::getLists(), fn ($entry) => $entry['id'] === $listId);
176+
$listEntry = A::find(static::getLists(), fn($entry) => $entry['id'] === $listId);
170177

171178
$this->log(
172179
[
@@ -188,7 +195,7 @@ protected static function getLists(): array
188195
{
189196
$response = static::cache(
190197
'lists',
191-
fn () => static::request('GET', '/contacts/lists')?->json()
198+
fn() => static::request('GET', '/contacts/lists')?->json()
192199
);
193200

194201
return $response['lists'] ?? [];
@@ -201,7 +208,7 @@ protected static function getTemplates(): array
201208
{
202209
$response = static::cache(
203210
'templates',
204-
fn () => static::request('GET', '/smtp/templates?limit=1000')?->json()
211+
fn() => static::request('GET', '/smtp/templates?limit=1000')?->json()
205212
);
206213

207214
return $response['templates'] ?? [];
@@ -215,7 +222,7 @@ protected static function getAttributeFields(): array
215222
{
216223
$response = static::cache(
217224
'attributes',
218-
fn () => static::request('GET', '/contacts/attributes')?->json()
225+
fn() => static::request('GET', '/contacts/attributes')?->json()
219226
);
220227

221228
$attributes = $response['attributes'] ?? [];
@@ -262,14 +269,14 @@ public static function request(string $method, string $url, array $data = []): R
262269
{
263270
if ($method !== 'GET') {
264271
$params = [
265-
'data' => Json::encode(A::filter($data, fn ($value) => $value !== null)),
272+
'data' => Json::encode(A::filter($data, fn($value) => $value !== null)),
266273
'headers' => [
267274
'Content-Type' => 'application/json',
268275
]
269276
];
270277
}
271278

272-
return Remote::$method('https://api.brevo.com/v3/' . $url, A::merge(
279+
return Remote::$method("https://api.brevo.com/v3/{$url}", A::merge(
273280
$params ?? [],
274281
[
275282
'headers' => [
@@ -289,15 +296,15 @@ public static function isAvailable(): bool
289296
}
290297

291298
/**
292-
* Returns the actions' blueprint group
299+
* @inheritDoc
293300
*/
294301
public static function group(): string
295302
{
296303
return 'newsletter';
297304
}
298305

299306
/**
300-
* Returns the base log settings for the action
307+
* @inheritDoc
301308
*/
302309
protected function logSettings(): array|bool
303310
{

classes/Actions/ButtondownAction.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
use Kirby\Toolkit\V;
1111
use tobimori\DreamForm\DreamForm;
1212

13+
use function count;
14+
use function is_string;
15+
1316
/**
14-
* Action for subscribing a user to a Buttondown newsletter list.
15-
* Docs: https://api.buttondown.email/v1/docs
17+
* Action for subscribing a user to a Buttondown newsletter list
18+
*
19+
* @see https://docs.buttondown.com/api-introduction
1620
*/
1721
class ButtondownAction extends Action
1822
{
@@ -86,7 +90,7 @@ protected static function tagsBlueprint(): array
8690
'label' => ' ',
8791
'type' => 'multiselect',
8892
'width' => '2/3',
89-
'options' => A::map(static::tags(), fn ($tag) => [
93+
'options' => A::map(static::tags(), fn($tag) => [
9094
'text' => $tag['name'],
9195
'value' => $tag['id']
9296
]),
@@ -158,7 +162,7 @@ public function run(): void
158162
'metadata' => static::metadata(),
159163
'tags' => static::submissionTags(),
160164
'referrer_url' => $this->submission()->referer(),
161-
], fn ($value) => $value !== null);
165+
], fn($value) => $value !== null);
162166

163167
$logData = ['template' => ['email' => $email]];
164168

@@ -168,13 +172,13 @@ public function run(): void
168172
'utm_campaign' => $this->submission()->valueFor('utm_campaign')?->value(),
169173
'utm_medium' => $this->submission()->valueFor('utm_medium')?->value(),
170174
'utm_source' => $this->submission()->valueFor('utm_source')?->value()
171-
], fn ($value) => $value !== null)));
175+
], fn($value) => $value !== null)));
172176

173177
// some error occurred, check for next steps
174178
if ($subscribeRequest->code() !== 201) {
175179
// update subscriber data if email already exists
176180
if (!static::simpleMode() && $subscribeRequest->json()['code'] === 'email_already_exists') {
177-
$updateRequest = static::request('PATCH', "/subscribers/{$email}", array_filter($data, fn ($key) => $key !== 'email', ARRAY_FILTER_USE_KEY));
181+
$updateRequest = static::request('PATCH', "/subscribers/{$email}", array_filter($data, fn($key) => $key !== 'email', ARRAY_FILTER_USE_KEY));
178182

179183
// send reminder if subscriber is unactivated
180184
if ($updateRequest->json()['subscriber_type'] === 'unactivated') {
@@ -261,15 +265,15 @@ public static function isAvailable(): bool
261265
}
262266

263267
/**
264-
* Returns the actions' blueprint group
268+
* @inheritDoc
265269
*/
266270
public static function group(): string
267271
{
268272
return 'newsletter';
269273
}
270274

271275
/**
272-
* Returns the base log settings for the action
276+
* @inheritDoc
273277
*/
274278
protected function logSettings(): array|bool
275279
{

classes/Actions/ConditionalAction.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
namespace tobimori\DreamForm\Actions;
44

55
/**
6-
* Action for conditionally running other actions.
6+
* Action for conditionally running other actions
77
*/
88
class ConditionalAction extends Action
99
{
1010
public const TYPE = 'conditional';
1111

12+
/**
13+
* @inheritDoc
14+
*/
1215
public static function blueprint(): array
1316
{
1417
return [
@@ -122,11 +125,24 @@ public function conditionsMet(): bool
122125
return true;
123126
}
124127

128+
/**
129+
* @inheritDoc
130+
*/
125131
public function run(): void
126132
{
127133
$collection = $this->conditionsMet() ? $this->block()->thatActions() : $this->block()->elseActions();
128134
foreach ($this->submission()->createActions($collection->toBlocks()) as $action) {
129135
$action->run();
130136
}
131137
}
138+
139+
/**
140+
* @inheritDoc
141+
*/
142+
public function supportsQueues(): bool
143+
{
144+
// for now, let's execute anything inside conditions immediately
145+
// TODO: implement queue support for conditional actions
146+
return false;
147+
}
132148
}

classes/Actions/DiscordWebhookAction.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
use tobimori\DreamForm\DreamForm;
1010

1111
/**
12-
* Action for sending a message in a discord channel.
12+
* Action for sending a message in a discord channel
1313
*/
1414
class DiscordWebhookAction extends Action
1515
{
1616
public const TYPE = 'discord-webhook';
1717

1818
/**
19-
* Returns the Blocks fieldset blueprint for the actions' settings
19+
* @inheritDoc
2020
*/
2121
public static function blueprint(): array
2222
{
@@ -82,7 +82,7 @@ protected function content(): string
8282
}
8383

8484
/**
85-
* Run the action
85+
* @inheritDoc
8686
*/
8787
public function run(): void
8888
{
@@ -132,15 +132,15 @@ public function run(): void
132132
}
133133

134134
/**
135-
* Returns the actions' blueprint group
135+
* @inheritDoc
136136
*/
137137
public static function group(): string
138138
{
139139
return 'notifications';
140140
}
141141

142142
/**
143-
* Returns the base log settings for the action
143+
* @inheritDoc
144144
*/
145145
protected function logSettings(): array|bool
146146
{

0 commit comments

Comments
 (0)