Queuety
Workflows

Conditional Branching

Workflows can skip to a named step based on the output of a previous step. This is done by returning a special _goto key in the step's return value.

Named steps

Give steps a name using the second argument to ->then():

use Queuety\Queuety;

Queuety::workflow( 'process_order' )
    ->then( ValidateOrderHandler::class, 'validate' )
    ->then( ChargePaymentHandler::class, 'charge' )
    ->then( FulfillOrderHandler::class, 'fulfill' )
    ->then( SendConfirmationHandler::class, 'confirm' )
    ->then( HandleFailureHandler::class, 'failure' )
    ->dispatch( [ 'order_id' => 123 ] );

Without an explicit name, steps are named by their index ('0', '1', '2', etc.).

The _goto key

A step can return _goto to jump to a named step, skipping everything in between:

use Queuety\Step;

class ValidateOrderHandler implements Step {
    public function handle( array $state ): array {
        $order = wc_get_order( $state['order_id'] );

        if ( ! $order || $order->get_status() === 'cancelled' ) {
            return [
                'error'  => 'Order is invalid or cancelled',
                '_goto'  => 'failure',
            ];
        }

        return [
            'order_total' => $order->get_total(),
            'customer_id' => $order->get_customer_id(),
        ];
    }

    public function config(): array {
        return [ 'needs_wordpress' => true ];
    }
}

When ValidateOrderHandler returns _goto => 'failure', the workflow skips charge, fulfill, and confirm, jumping directly to the failure step. The _goto key is consumed by the workflow engine and not persisted in the state.

Branching patterns

Skip on condition

class CheckInventoryHandler implements Step {
    public function handle( array $state ): array {
        $in_stock = check_inventory( $state['sku'] );

        if ( ! $in_stock ) {
            return [
                'reason' => 'Out of stock',
                '_goto'  => 'notify_backorder',
            ];
        }

        return [ 'inventory_reserved' => true ];
    }

    public function config(): array {
        return [];
    }
}

Early completion

Jump to the final step to skip unnecessary processing:

class CheckCacheHandler implements Step {
    public function handle( array $state ): array {
        $cached = get_transient( "report_{$state['user_id']}" );

        if ( $cached ) {
            return [
                'report_url' => $cached,
                '_goto'      => 'done',
            ];
        }

        return [ 'cache_miss' => true ];
    }

    public function config(): array {
        return [ 'needs_wordpress' => true ];
    }
}

Complete example

Queuety::workflow( 'content_pipeline' )
    ->then( CheckCacheHandler::class, 'check_cache' )
    ->then( FetchDataHandler::class, 'fetch' )
    ->then( CallLLMHandler::class, 'generate' )
    ->then( SaveResultHandler::class, 'save' )
    ->then( ReturnCachedHandler::class, 'done' )
    ->dispatch( [ 'user_id' => 42 ] );

If the cache check finds a hit, the workflow jumps from check_cache directly to done, skipping fetch, generate, and save.

On this page