PHP code example of juststeveking / workflow-engine
1. Go to this page and download the library: Download juststeveking/workflow-engine library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
juststeveking / workflow-engine example snippets
final class MemberRegistration implements WorkflowDefinition
{
public static function name(): string
{
return 'member_registration';
}
public function steps(): array
{
return [ChargeCard::class, ProvisionAccount::class];
}
}
final class ChargeCard implements WorkflowStep
{
public function handle(WorkflowContext $context): StepResult
{
// ...charge the card through your gateway...
return StepResult::await('payment_succeeded'); // pause until the webhook lands
}
public function timeoutSeconds(): ?int { return 3600; } // ...or fail if it never does
public function maxAttempts(): int { return 1; }
}
$instance = app(WorkflowEngine::class)->start('member_registration', (string) $member->id, 'member');
// ...hours later, the payment provider calls your webhook...
$engine->signal($instance->id, 'payment_succeeded', ['payment_id' => 'pay_123']);
use JustSteveKing\WorkflowEngine\Contracts\WorkflowDefinition;
use JustSteveKing\WorkflowEngine\Contracts\WorkflowStep;
use JustSteveKing\WorkflowEngine\Domain\StepResult;
use JustSteveKing\WorkflowEngine\Domain\WorkflowContext;
final class ChargeMemberStep implements WorkflowStep
{
public function handle(WorkflowContext $context): StepResult
{
$payment = Payments::charge($context->get('member_id'), $context->get('plan'));
// Pause here until the provider confirms the charge via webhook.
return StepResult::await('payment_succeeded', ['payment_ref' => $payment->ref]);
}
public function timeoutSeconds(): ?int { return 3600; } // fail if no signal within an hour
public function maxAttempts(): int { return 1; } // charging twice is worse than failing
}
final class ProvisionAccountStep implements WorkflowStep
{
public function handle(WorkflowContext $context): StepResult
{
Accounts::provision($context->get('member_id'), $context->get('payment_id'));
return StepResult::complete();
}
public function timeoutSeconds(): ?int { return null; }
public function maxAttempts(): int { return 3; }
}
final class MemberRegistrationWorkflow implements WorkflowDefinition
{
public static function name(): string { return 'member_registration'; }
public function steps(): array
{
return [ChargeMemberStep::class, ProvisionAccountStep::class];
}
}
use JustSteveKing\WorkflowEngine\Domain\WorkflowRegistry;
$this->app->make(WorkflowRegistry::class)->register(MemberRegistrationWorkflow::class);
use JustSteveKing\WorkflowEngine\Domain\WorkflowEngine;
$engine = app(WorkflowEngine::class);
$instance = $engine->start(
workflowName: 'member_registration',
aggregateId: (string) $member->id,
aggregateType: 'member',
initialContext: ['member_id' => $member->id, 'plan' => 'annual'],
);
$engine->advance($instance->id); // in production the queued job does this for you