Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added Laravel Built-in Support for Conditional Execution with when and unless #6

Merged
merged 1 commit into from
Aug 12, 2024

Conversation

mreduar
Copy link
Contributor

@mreduar mreduar commented Aug 11, 2024

PR Description:

This PR introduces replace the method for conditional execution within the workflow management system. The when and unless method allows the execution of a callback and a default callback based on a provided condition, enhancing the flexibility and readability of workflow definitions.

Changes Include:

Added build in Laravel Conditionable trait for conditional execution.
Updated WorkflowTraits to utilize the Conditionable Trait for tracking events and registering observers conditionally.

Example Usage:

<?php

namespace App\Workflows;

use App\Actions\CalculateTotal;
use App\Actions\MakePayment;
use App\Actions\ValidateCartItems;
use App\Actions\SendEmailReceipt;
use Safemood\Workflow\WorkflowManager;

class PaymentWorkflow extends WorkflowManager
{
    public function handle()
    {
        $this->when(true, function () {
            $this->registerObservers([
                DummyModel::class => DummyModelObserver::class,
            ]);
        });

        $this->when(true, function () {
            $this->addBeforeActions([
                new DummyAction(),
                new DummyAction(),
            ]);
        });

        $this->unless(
            value: false,
            callback: fn () => $this->trackAllEvents(),
            default: fn () => $this->doSomething()
        );
    }
}

@Safemood Safemood self-assigned this Aug 12, 2024
@Safemood
Copy link
Owner

Thanks for the PR! This is a much better approach. I appreciate the improvements you've made.

@Safemood Safemood merged commit 5595dd1 into Safemood:main Aug 12, 2024
1 check passed
@Safemood Safemood changed the title Added Laravel Build in method for conditional execution Added Laravel Built-in Support for Conditional Execution with when and unless Aug 14, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants