PHP code example of pstoute / laravel-workflow-conductor
1. Go to this page and download the library: Download pstoute/laravel-workflow-conductor 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/ */
pstoute / laravel-workflow-conductor example snippets
use Pstoute\WorkflowConductor\Models\Workflow;
class Automation extends Workflow
{
public function getTable(): string
{
return 'automations';
}
// Add your custom columns, relationships, etc.
}
use Pstoute\WorkflowConductor\WorkflowConductorServiceProvider;
class WorkflowConductorProvider extends WorkflowConductorServiceProvider
{
public function boot(): void
{
// Publish config
$this->publishes([
base_path('vendor/pstoute/laravel-workflow-conductor/config/workflow-conductor.php')
=> config_path('workflow-conductor.php'),
], 'workflow-conductor-config');
// Skip migrations - the app has its own tables
// Load routes and register built-in extensions
$this->loadRoutesFrom(base_path('vendor/pstoute/laravel-workflow-conductor/routes/webhooks.php'));
$this->registerBuiltInTriggers();
$this->registerBuiltInConditions();
$this->registerBuiltInActions();
}
}
use Pstoute\WorkflowConductor\Events\WorkflowCompleted;
Event::listen(WorkflowCompleted::class, function ($event) {
Log::info("Workflow {$event->workflow->name} completed");
});
namespace App\Workflows\Triggers;
use Pstoute\WorkflowConductor\Contracts\TriggerInterface;
use Pstoute\WorkflowConductor\Data\WorkflowContext;
class PaymentReceivedTrigger implements TriggerInterface
{
public function getIdentifier(): string
{
return 'payment.received';
}
public function getName(): string
{
return 'Payment Received';
}
public function getDescription(): string
{
return 'Triggered when a payment is received';
}
public function getConfigurationSchema(): array
{
return [
'type' => 'object',
'properties' => [
'min_amount' => [
'type' => 'number',
'description' => 'Minimum payment amount to trigger',
],
'currency' => [
'type' => 'string',
'description' => 'Currency code (e.g., USD)',
],
],
];
}
public function shouldTrigger(WorkflowContext $context, array $triggerConfig): bool
{
$payment = $context->get('payment');
if (!$payment) {
return false;
}
// Check minimum amount
$minAmount = $triggerConfig['min_amount'] ?? 0;
if ($payment->amount < $minAmount) {
return false;
}
// Check currency
$currency = $triggerConfig['currency'] ?? null;
if ($currency && $payment->currency !== $currency) {
return false;
}
return true;
}
public function getAvailableData(): array
{
return [
'payment' => 'The payment object',
'payment.amount' => 'Payment amount',
'payment.currency' => 'Payment currency',
'payment.user' => 'The user who made the payment',
];
}
}
use Pstoute\WorkflowConductor\Facades\Conductor;
use App\Workflows\Triggers\PaymentReceivedTrigger;
public function boot(): void
{
Conductor::registerTrigger(new PaymentReceivedTrigger());
}
use Pstoute\WorkflowConductor\Facades\Conductor;
use Pstoute\WorkflowConductor\Data\WorkflowContext;
// In your payment processing code
$context = new WorkflowContext([
'payment' => $payment,
'user' => $payment->user,
]);
Conductor::trigger('payment.received', $context);
namespace App\Workflows\Actions;
use Pstoute\WorkflowConductor\Contracts\ActionInterface;
use Pstoute\WorkflowConductor\Data\ActionResult;
use Pstoute\WorkflowConductor\Data\WorkflowContext;
use App\Services\SmsService;
class SendSmsAction implements ActionInterface
{
public function __construct(
protected SmsService $smsService
) {}
public function getIdentifier(): string
{
return 'send_sms';
}
public function getName(): string
{
return 'Send SMS';
}
public function getDescription(): string
{
return 'Send an SMS message via Twilio';
}
public function getConfigurationSchema(): array
{
return [
'type' => 'object',
'properties' => [
'to' => [
'type' => 'string',
'description' => 'Phone number to send to',
' }
}
public function supportsAsync(): bool
{
return true;
}
public function getTimeout(): int
{
return 30;
}
public function getOutputData(): array
{
return [
'message_sid' => 'The Twilio message SID',
'sent_to' => 'The phone number the message was sent to',
];
}
}
use Pstoute\WorkflowConductor\Facades\Conductor;
use App\Workflows\Actions\SendSmsAction;
public function boot(): void
{
Conductor::registerAction(app(SendSmsAction::class));
}
Conductor::create()
->name('Order SMS Notification')
->trigger('model.created', ['model' => App\Models\Order::class])
->action('send_sms', [
'to' => '{{ model.user.phone }}',
'message' => 'Your order #{{ model.number }} has been received!',
])
->save();
namespace App\Workflows\Conditions;
use Pstoute\WorkflowConductor\Contracts\ConditionInterface;
use Pstoute\WorkflowConductor\Data\WorkflowContext;
class BusinessHoursCondition implements ConditionInterface
{
public function getIdentifier(): string
{
return 'business_hours';
}
public function getName(): string
{
return 'Business Hours';
}
public function getDescription(): string
{
return 'Check if current time is within business hours';
}
public function getOperators(): array
{
return [
'is_business_hours' => 'Is during business hours',
'is_not_business_hours' => 'Is outside business hours',
];
}
public function evaluate(WorkflowContext $context, array $config): bool
{
$operator = $config['operator'] ?? 'is_business_hours';
$timezone = $config['timezone'] ?? config('app.timezone');
$startHour = $config['start_hour'] ?? 9;
$endHour = $config['end_hour'] ?? 17;
$workDays = $config['work_days'] ?? [1, 2, 3, 4, 5]; // Mon-Fri
$now = now($timezone);
$currentHour = $now->hour;
$currentDay = $now->dayOfWeek;
$isBusinessHours = in_array($currentDay, $workDays)
&& $currentHour >= $startHour
&& $currentHour < $endHour;
return $operator === 'is_business_hours' ? $isBusinessHours : !$isBusinessHours;
}
public function getConfigurationSchema(): array
{
return [
'type' => 'object',
'properties' => [
'operator' => [
'type' => 'string',
'enum' => ['is_business_hours', 'is_not_business_hours'],
],
'timezone' => [
'type' => 'string',
'description' => 'Timezone to check',
],
'start_hour' => [
'type' => 'integer',
'description' => 'Start hour (0-23)',
'default' => 9,
],
'end_hour' => [
'type' => 'integer',
'description' => 'End hour (0-23)',
'default' => 17,
],
],
];
}
}