PHP code example of webignition / symfony-messenger-delay-middleware

1. Go to this page and download the library: Download webignition/symfony-messenger-delay-middleware 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/ */

    

webignition / symfony-messenger-delay-middleware example snippets




declare(strict_types=1);

namespace App\MessageDispatcher;

use App\Message\FooMessage;
use App\Message\BarMessage;
use Symfony\Component\Messenger\MessageBusInterface;

final readonly class ExampleMessageDispatcher
{
    public function __construct(
        private MessageBusInterface $messageBus,
    ) {}
    
    public function dispatchFooMessage(): void
    {
        // Delayed by one second as per the previous example configuration.
        $this->messageBus->dispatch(new FooMessage());        
    }
    
    public function dispatchBarMessage(): void
    {
        // Delayed by thirty seconds as per the previous example configuration.
        $this->messageBus->dispatch(new BarMessage());        
    }
}



declare(strict_types=1);

namespace App\MessageDispatcher;

use App\Message\FooMessage;
use App\Message\BarMessage;
use Symfony\Component\Messenger\MessageBusInterface;
use webignition\SymfonyMessengerDelayMiddleware\NonDelayedStamp;

final readonly class ExampleMessageDispatcher
{
    public function __construct(
        private MessageBusInterface $messageBus,
    ) {}
    
    public function dispatchFooMessageWithoutDelay(): void
    {
        // FooMessage is dispatched immediately despite being configured to delay.
        $this->messageBus->dispatch(new FooMessage(), [new NonDelayedStamp()]);        
    }
    
    public function dispatchBarMessageWithoutDelay(): void
    {
        // BarMessage is dispatched immediately despite being configured to delay.
        $this->messageBus->dispatch(new BarMessage(), [new NonDelayedStamp()]);
    }
}