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 support for listener shouldQueue method #247

Merged
merged 1 commit into from
Jul 25, 2023
Merged

Add support for listener shouldQueue method #247

merged 1 commit into from
Jul 25, 2023

Conversation

dmason30
Copy link
Contributor

@dmason30 dmason30 commented Jul 23, 2023

This PR adds support for the below functionality that exists on traditional listeners.

Conditionally Queueing Listeners

Sometimes, you may need to determine whether a listener should be queued based on some data that are only available at runtime. To accomplish this, a shouldQueue method may be added to a listener to determine whether the listener should be queued. If the shouldQueue method returns false, the listener will not be executed:

    <?php

    namespace App\Listeners;

    use App\Events\OrderCreated;
    use Illuminate\Contracts\Queue\ShouldQueue;

    class RewardGiftCard implements ShouldQueue
    {
        /**
         * Reward a gift card to the customer.
         */
        public function handle(OrderCreated $event): void
        {
            // ...
        }

        /**
         * Determine whether the listener should be queued.
         */
        public function shouldQueue(OrderCreated $event): bool
        {
            return $event->order->subtotal >= 5000;
        }
    }

Source: https://laravel.com/docs/10.x/events#conditionally-queueing-listeners

Copy link
Owner

@lorisleiva lorisleiva left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution and for providing a test. 🍺

@lorisleiva lorisleiva merged commit 02b7731 into lorisleiva:main Jul 25, 2023
8 checks passed
@dmason30 dmason30 deleted the add-listener-should-queue branch July 25, 2023 09:55
jaulz pushed a commit to jaulz/laravel-actions that referenced this pull request Jul 27, 2023
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.

2 participants