Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add method to configure definition conditionally #53

Merged
merged 1 commit into from Jul 9, 2022

Conversation

ksassnowski
Copy link
Owner

This PR adds a when method to the WorkflowDefinition class to conditionally run a callback when configuring a definition. This allows users to conditionally add jobs to a workflow without having to litter definition with tons of if-statements.

This has been previously suggested in #2 where I initially declined the proposal. I have since then changed my mind on this and agree that this would be a useful addition. The method's API works slightly different than in the original PR but is otherwise pretty similar.

@morrislaptop I will add you as a co-author on this feature since you were the one who originally proposed it.

Usage

class MyWorkflow extends AbstractWorkflow
{
    public function __construct(private User $user)
    {
    }

    public function definition(): WorkflowDefinition
    {
        return Workflow::define('My conditional workflow')
            ->addJob(new JobThatShouldAlwaysRun())
            ->when($this->user->isProUser(), function (WorkflowDefinition $definition): void {
                $definition->addJob(new ProUserOnlyFeature());
            });
            // it's also possible to pass in a callable that returns a boolean as the condition
            ->when(fn (): bool => false, function (WorkflowDefinition $definition): void {
                $definition->addJob(new NeverGetsAdded());
            });
    }
}

* @param bool|callable(): bool $condition
* @param callable(WorkflowDefinition): void $callback
*/
public function when(callable|bool $condition, callable $callback): self
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we use the Conditionable trait in Illuminate\Support for this feature?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't even know about this trait. That's probably a better idea.

That's why I pay you the big (exposure) bucks!

@morrislaptop
Copy link

Nice! What about the dependencies?

@ksassnowski
Copy link
Owner Author

The callback gets passed the whole WorkflowDefinition object. So you can add dependencies just like you normally would.

@ksassnowski ksassnowski force-pushed the conditional_definition branch 2 times, most recently from 157512a to ece6b58 Compare July 9, 2022 09:55
@ksassnowski
Copy link
Owner Author

I've updated the PR to use the Conditionable trait instead. I also decided to remove the test cases as these would just be testing the trait.

@ksassnowski ksassnowski merged commit eb0e16e into master Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants