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

Feature: Added ...transitionArgs to default transition constructor call to allow arguments use for custom default transition #269

Conversation

IlliaVeremiev
Copy link
Contributor

@IlliaVeremiev IlliaVeremiev commented Oct 29, 2024

Foreword

Custom Transition can be initialized with arguments from transitionTo parameters.
Custom default Transition can be registeren in the configuration for transitions between all states, but can not be initialized with arguments as non default transition.

Requirement

When calling transitionTo method I would like to pass additional arguments that can be used in StateChanged event listener.

Suggestion

Add ...transitionArgs to $defaultTransition(...) constructor call.

Use case

We need to handle status change in event listener, but depending on where transitionTo called from, in some cases we should notify user about event, but in some cases not. We can create custom DefaultTransition with additional silent parameter.

// CustomDefaultTransitionWithAttributes.php

class CustomDefaultTransitionWithAttributes extends DefaultTransition
{
    public function __construct($model, string $field, State $newState, public bool $silent = false)
    {
        parent::__construct($model, $field, $newState);
    }
}

// model-states.php

return [
    'default_transition' => CustomDefaultTransitionWithAttributes::class
];

// OrderStateChangedListener.php

class OrderStateChangedListener
{
    public function handle(StatusChange $event): void
    {
        // do some logic
        if (! $event->transition->silent) {
            // send notification
        }
    }
}

// Business logic layer

$order->status->transitionTo($status->value); // Sends notification

$order->status->transitionTo($status->value, false);  // Sends notification

$order->status->transitionTo($status->value, true); // Doesn't send notification, but still performs logic

@Briareos17
Copy link

@freekmurze This realy needed

@freekmurze
Copy link
Member

Could you add an example of this new functionality to the docs?

@IlliaVeremiev IlliaVeremiev force-pushed the feature/allow-transition-arguments-for-custom-default-transition branch from b96b227 to 82b4f6f Compare December 15, 2024 01:02
@IlliaVeremiev
Copy link
Contributor Author

Hi @freekmurze,
Updated docs with short description and usage example

@freekmurze
Copy link
Member

Could you rebase against to current main to see if the tests run properly?

…ll to allow arguments use for custom default transition
@IlliaVeremiev IlliaVeremiev force-pushed the feature/allow-transition-arguments-for-custom-default-transition branch from 82b4f6f to 152906f Compare December 16, 2024 15:33
@IlliaVeremiev
Copy link
Contributor Author

@freekmurze done

@freekmurze freekmurze merged commit 627ceb8 into spatie:main Dec 16, 2024
17 checks passed
@freekmurze
Copy link
Member

Thanks!

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.

3 participants