# Workflow

The Workflow component provides the ability to define a runtime execution based on the workflow pattern. It allows to define a series of individual steps, each triggering responses with the goal to produce a final outcome.

WorkflowInterface describes the interface for the component in charge of defining a Workflow.

# Creating a Workflow

To create a workflow pass the workflow name:

use Chevere\Components\Workflow\Workflow;

$workflow = new Workflow('insert-user');

# Adding Steps

The withAdded method allows to append steps to the Workflow. For the code below, note how validate and insert steps reference arguments stored in the response of the fetch step.

Variables payload and useCache will be required to be provided by the Workflow runner.

use Chevere\Components\Workflow\Step;

$workflow = $workflow
    ->withAdded(
        fetch: (new Step('FetchAction'))
            ->withArguments(
                payload: '${payload}'
            ),
        validate: (new Step('ValidateAction'))
            ->withArguments(
                cache: '${useCache}',
                email: '${fetch:email}',
                username: '${fetch:username}',
            ),
        insert: (new Step('InsertAction'))
            ->withArguments(
                email: '${fetch:email}',
                username: '${fetch:username}',
            )
    );

# Adding Steps Before and After

The withAddedBefore and withAddedAfter methods allows to add steps before/after another step.

Code below modify $workflow to extend its functionality.

# From this:
fetch->validate->insert

# To this:
logging->fetch->validate->insert->updateCount->caching
use Chevere\Components\Workflow\Step;

$workflow = $workflow
    ->withAddedBefore(
        'fetch',
        logging: (new Step('LoggingAction'))
            ->withArguments(device: '${logDevice}'),
    )
    ->withAddedAfter(
        'insert',
        updateCount: (new Step('UpdateUserCountAction'))
            ->withArguments(add: 1),
        caching: (new Step('CachingAction'))
            ->withArguments(userId: '${insert:userId}')
    );

# Step

A Step define an Action with arguments to pass. StepInterface describes the interface for the component in charge of defining a Step.

use Chevere\Components\Workflow\Step;

new Step('SomeActionClassName');

For the code above, the argument passed must be a class name implementing the ActionInterface.

# Step Parameters

Parameters for the step are defined in the Action Parameters.

# Step Arguments

The withArguments method is used to define arguments passed on runtime to the action defined in the step.

use Chevere\Components\Workflow\Step;

(new Step('SomeAction'))
    ->withArguments(
        firstName: 'Rodolfo',
        lastName: 'Berrios'
    );

For the code above, arguments Rodolfo and Berrios will be passed to SomeAction when running the Workflow. These arguments will be matched against the Parameters defined at the Action.

# Referenced Arguments

Referenced arguments can be defined to bind arguments referencing Workflow variables or responses returned after running any previous step.

Expression Meaning
${var} A context argument
${stepName:key} The value of key for the response of named step stepName