Skip to content

Commit

Permalink
Doc: Add example recipe (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek authored Nov 20, 2023
1 parent 37971ee commit 3335c0b
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion doc/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,49 @@ Creating your own recipes
You can create your own recipes by implementing `BenTools\ETL\Recipe\Recipe`
or using a callable with the same signature.

Example for displaying a progress bar when using the Symfony framework:
### Example 1. Stop the workflow when a max number of items has been reached

```php
use BenTools\ETL\EtlExecutor;
use BenTools\ETL\EtlState;
use BenTools\ETL\EventDispatcher\Event\ExtractEvent;

use const PHP_INT_MAX;

final class MaxItemsRecipe extends Recipe
{
public function __construct(
private readonly int $maxItems = PHP_INT_MAX,
) {
}

public function decorate(EtlExecutor $executor): EtlExecutor
{
return $executor
->withContext(['maxItems' => $this->maxItems])
->onExtract($this);
}

public function __invoke(ExtractEvent $event): void
{
if ($event->state->nbExtractedItems >= $event->state->context['maxItems']) {
$event->state->nextTick(fn (EtlState $state) => $state->skip());
}
}
}
```

Usage:

```php
use function BenTools\ETL\withRecipe;

$etl = withRecipe(new MaxItemsRecipe(10)); // Set to 10 items max by default
$report = $etl->process(['foo', 'bar', 'baz'], context: ['maxItems' => 2]); // Optionally overwrite here
var_dump($report->output); // ['foo', 'bar']
```

### Example 2. Display a progress bar when using the Symfony framework:

```php
use BenTools\ETL\EtlExecutor;
Expand Down

0 comments on commit 3335c0b

Please sign in to comment.