Skip to content

Commit f3b856d

Browse files
committed
refactoring conditions and readme
1 parent d09bec9 commit f3b856d

File tree

4 files changed

+131
-43
lines changed

4 files changed

+131
-43
lines changed

README.md

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Scheduler based
1+
# Scheduler based Workflow for Laravel
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/elegantly/laravel-workflow.svg?style=flat-square)](https://packagist.org/packages/elegantly/laravel-workflow)
44
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/elegantengineeringtech/laravel-workflow/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/elegantengineeringtech/laravel-workflow/actions?query=workflow%3Arun-tests+branch%3Amain)
@@ -16,34 +16,97 @@ composer require elegantly/laravel-workflow
1616
You can publish and run the migrations with:
1717

1818
```bash
19-
php artisan vendor:publish --tag="laravel-workflow-migrations"
19+
php artisan vendor:publish --tag="workflow-migrations"
2020
php artisan migrate
2121
```
2222

2323
You can publish the config file with:
2424

2525
```bash
26-
php artisan vendor:publish --tag="laravel-workflow-config"
26+
php artisan vendor:publish --tag="workflow-config"
2727
```
2828

2929
This is the contents of the published config file:
3030

3131
```php
3232
return [
33+
34+
'queue' => env('WORKFLOW_QUEUE'),
35+
36+
'queue_connection' => env('WORKFLOW_QUEUE_CONNECTION'),
37+
3338
];
3439
```
3540

36-
Optionally, you can publish the views using
41+
## Usage
3742

38-
```bash
39-
php artisan vendor:publish --tag="laravel-workflow-views"
43+
### Defining your workflows
44+
45+
Define a workflow in a class like this one:
46+
47+
```php
48+
namespace App\Workflows;
49+
50+
use Carbon\CarbonInterval;
51+
use Elegantly\Workflow\Models\Workflow;
52+
use Elegantly\Workflow\WorkflowDefinition;
53+
use Elegantly\Workflow\WorkflowStep;
54+
use Illuminate\Support\Collection;
55+
56+
class WelcomeUserWorkflow extends WorkflowDefinition
57+
{
58+
public function __construct(
59+
public User $user
60+
) {
61+
//
62+
}
63+
64+
public function steps(Workflow $workflow): Collection
65+
{
66+
return collect()
67+
->put(
68+
'welcome-email',
69+
WorkflowStep::make($workflow)
70+
->action(function (): void {
71+
// send an email to the user
72+
})
73+
)
74+
->put(
75+
'export-user',
76+
WorkflowStep::make($workflow)
77+
->action(new ExportUserToCrmJob($this->user))
78+
)
79+
->put(
80+
'product-tour-email',
81+
WorkflowStep::make($workflow)
82+
->after([
83+
'welcome-email' => CarbonInterval::days(3)
84+
])
85+
->action(function (): void {
86+
// Send another email to your user
87+
})
88+
)
89+
->put(
90+
'send-promo-code',
91+
WorkflowStep::make($workflow)
92+
->after([
93+
'product-tour-email' => CarbonInterval::days(7),
94+
])
95+
->when(fn() => $this->user->hasNotPurchased())
96+
->action(function (): void {
97+
//
98+
})
99+
);
100+
}
101+
}
40102
```
41103

42-
## Usage
104+
### Running your workflow
43105

44106
```php
45-
$laravelWorkflow = new Elegantly\Workflow();
46-
echo $laravelWorkflow->echoPhrase('Hello, Elegantly!');
107+
use Elegantly\Workflow\Commands\RunWorkflowsCommand;
108+
109+
$schedule->command(RunWorkflowsCommand::class)->everyMinutes();
47110
```
48111

49112
## Testing

src/WorkflowDefinition.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
namespace Elegantly\Workflow;
44

55
use Elegantly\Workflow\Models\Workflow;
6+
use Illuminate\Queue\SerializesModels;
67
use Illuminate\Support\Collection;
78

89
abstract class WorkflowDefinition
910
{
11+
use SerializesModels;
12+
1013
/**
1114
* @return Collection<string, WorkflowStep>
1215
*/

src/WorkflowStep.php

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,29 @@ class WorkflowStep
1212
{
1313
/**
1414
* @param null|ShouldQueue|(Closure():void) $action
15-
* @param null|bool|(Closure():bool) $condition
15+
* @param array<int, bool|(Closure():bool)> $conditions
1616
*/
1717
final public function __construct(
1818
public Workflow $workflow,
1919
public null|ShouldQueue|Closure $action = null,
20-
public null|bool|Closure $condition = null,
20+
public array $conditions = [],
2121
) {
2222
//
2323
}
2424

2525
public function isReady(): bool
2626
{
27-
if (is_null($this->condition)) {
27+
if (empty($this->conditions)) {
2828
return true;
2929
}
3030

31-
return (bool) value($this->condition);
31+
foreach ($this->conditions as $condition) {
32+
if (! value($condition)) {
33+
return false;
34+
}
35+
}
36+
37+
return true;
3238
}
3339

3440
public static function make(Workflow $workflow): static
@@ -51,44 +57,60 @@ public function action(ShouldQueue|Closure $action): static
5157
public function when(
5258
bool|Closure $value
5359
): static {
54-
$this->condition = $value;
60+
$this->conditions[] = $value;
5561

5662
return $this;
5763
}
5864

5965
/**
60-
* @param string|array<string|int, string|CarbonInterval> $steps
66+
* @param string|array<string|int, string|CarbonInterval|bool|(Closure():bool)> $steps
6167
*/
6268
public function after(
6369
string|array $steps,
6470
): static {
6571

66-
return $this->when(
67-
fn () => collect(Arr::wrap($steps))
68-
->map(function ($delay, $step) {
69-
70-
if (is_string($delay)) {
71-
return (bool) $this
72-
->workflow
73-
->getItem($delay)
74-
?->isFinished();
75-
}
76-
77-
if (
78-
is_string($step) &&
79-
$delay instanceof CarbonInterval
80-
) {
81-
return (bool) $this
82-
->workflow
83-
->getItem($step)
84-
?->finished_at
85-
?->add($delay)
86-
?->isPast();
87-
}
88-
89-
return false;
90-
})
91-
->every(fn ($value) => $value === true)
92-
);
72+
foreach (Arr::wrap($steps) as $step => $delay) {
73+
if (
74+
is_int($step) &&
75+
is_bool($delay)
76+
) {
77+
$this->when($delay);
78+
}
79+
80+
if (
81+
is_int($step) &&
82+
$delay instanceof Closure
83+
) {
84+
$this->when($delay);
85+
}
86+
87+
if (
88+
is_int($step) &&
89+
is_string($delay)
90+
) {
91+
$this->when(
92+
fn () => (bool) $this
93+
->workflow
94+
->getItem($delay)
95+
?->isFinished()
96+
);
97+
}
98+
99+
if (
100+
is_string($step) &&
101+
$delay instanceof CarbonInterval
102+
) {
103+
$this->when(
104+
fn () => (bool) $this
105+
->workflow
106+
->getItem($step)
107+
?->finished_at
108+
?->add($delay)
109+
?->isPast()
110+
);
111+
}
112+
}
113+
114+
return $this;
93115
}
94116
}

tests/TestWorkflowDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public function steps(Workflow $workflow): Collection
4343
WorkflowStep::make($workflow)
4444
->after([
4545
'welcome' => CarbonInterval::minutes(10),
46-
'welcome-bis',
4746
])
47+
->after('welcome-bis')
4848
->action(function (): void {
4949
//
5050
})

0 commit comments

Comments
 (0)