Laravel Workflow

safemood/laravel-workflow image

Laravel Workflow stats

Downloads
5
Stars
36
Open Issues
0
Forks
0

View on GitHub →

Laravel package that simplifies workflows with clear actions definition and event tracking.

Laravel Workflow

Laravel Workflow combines feature details into one class, allowing for action definition and event tracking, simplifying understanding and revealing hidden logic.

Installation

You can install the package via Composer:

composer require safemood/laravel-workflow

Create a Workflow

You can create a workflow using the artisan command:

php artisan make:workflow PaymentWorkflow

Create Actions

You can create an action using the artisan command:

php artisan make:action ValidateCartItems
<?php
 
namespace App\Actions;
 
use Safemood\Workflow\Action;
 
class ValidateCartItems extends Action
{
public function handle(array &$context)
{
// Simulate extra validation logic
if (empty($context['cart'])) {
throw new \Exception('Cart is empty');
}
 
// you can pass data to the next action if you want
$context['validated'] = true;
 
}
}

Basic Example

Once you have set up your workflows and actions, you can define your business logic and orchestrate them within your Laravel application.

Define Workflow Logic

In your PaymentWorkflow class, you define the sequence of actions and conditions that make up your workflow:

<?php
 
namespace App\Workflows;
 
use App\Actions\CalculateTotal;
use App\Actions\MakePayment;
use App\Actions\ValidateCartItems;
use App\Events\PaymentProcessed;
use App\Jobs\SendEmails;
use App\Observers\UserObserver;
use App\Models\Order;
use Safemood\Workflow\WorkflowManager;
 
class PaymentWorkflow extends Workflow
{
public function handle()
{
// Actions to be executed before the main action
$this->addBeforeActions([
new ValidateCartItems(),
new CalculateTotal()
]);
 
// The main action of the workflow
$this->addMainAction(new MakePayment());
 
// Actions to be executed after the main action
$this->addAfterAction(new SendEmails()); // Normal laravel Job in this example
 
// Observers to register for specific entities
$this->registerObservers([
Order::class => OrderObserver::class,
]);
 
// Good Debugging or if you want to understand what is happining during the workflow execution:
 
$this->trackEvents([
PaymentProcessed::class
]);
 
// $this->trackAllEvents(); // or
 
// $this->trackEventsIn('App\Events\\');
 
 
}
}

Execute Workflow

<?php
 
namespace App\Http\Controllers;
 
use App\Workflows\PaymentWorkflow;
use Illuminate\Http\Request;
 
class PaymentController extends Controller
{
public function payment(Request $request)
{
// Example context data representing a user's cart and user information
$context = [
'cart' => [
['id' => 1, 'name' => 'Product A', 'price' => 100, 'quantity' => 2],
['id' => 2, 'name' => 'Product B', 'price' => 50, 'quantity' => 1]
],
'user_id' => 123
];
 
// Execute the PaymentWorkflow with the provided context
$paymentWorkflow = (new PaymentWorkflow)->run($context);
 
// Check if the workflow execution was successful
$success = $paymentWorkflow->passes();
 
// Check if the workflow execution failed
$failure = $paymentWorkflow->failed();
 
// Dump the workflow for debugging
// $paymentWorkflow->dd();
 
// Handle the response based on the workflow outcome
if ($success) {
return $paymentWorkflow->successResponse();
}
 
return $paymentWorkflow->failureResponse();
}
}

Conditional Action Execution

You can use the when method to conditionally execute an action.

<?php
 
namespace App\Workflows;
 
use App\Actions\CalculateTotal;
use App\Actions\MakePayment;
use App\Actions\ValidateCartItems;
use App\Actions\SendEmailReceipt;
use Safemood\Workflow\Workflow;
 
class PaymentWorkflow extends Workflow
{
public function handle()
{
$this->when(false, function () {
$this->trackAllEvents();
});
 
$this->when(true, function () {
$this->registerObservers([
DummyModel::class => DummyModelObserver::class,
]);
});
 
$this->when(true, function () {
$this->addBeforeActions([
new DummyAction(),
new DummyAction(),
]);
});
}
}
Safemood photo

Lover of anything technology-related.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Safemood Laravel Workflow Related Articles

Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum