Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Dispatching two events when a message is sent & handled #30650

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

Merged
merged 1 commit into from
Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Messenger\Envelope;

/**
* Event is dispatched before a message is sent to the transport.
*
* The event is *only* dispatched if the message will actually
* be sent to at least one transport. If the message is sent
* to multiple transports, the message is dispatched only one time.
*
* @author Ryan Weaver <[email protected]>
*/
class SendMessageToTransportsEvent extends Event
{
private $envelope;

public function __construct(Envelope $envelope)
{
$this->envelope = $envelope;
}

public function getEnvelope(): Envelope
{
return $this->envelope;
}

public function setEnvelope(Envelope $envelope)
{
$this->envelope = $envelope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
use Symfony\Component\Messenger\Stamp\SentStamp;
Expand All @@ -30,10 +32,12 @@ class SendMessageMiddleware implements MiddlewareInterface
use LoggerAwareTrait;

private $sendersLocator;
private $eventDispatcher;

public function __construct(SendersLocatorInterface $sendersLocator)
public function __construct(SendersLocatorInterface $sendersLocator, EventDispatcherInterface $eventDispatcher = null)
{
$this->sendersLocator = $sendersLocator;
$this->eventDispatcher = $eventDispatcher;
$this->logger = new NullLogger();
}

Expand All @@ -58,7 +62,15 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
/** @var RedeliveryStamp|null $redeliveryStamp */
$redeliveryStamp = $envelope->last(RedeliveryStamp::class);

foreach ($this->sendersLocator->getSenders($envelope, $handle) as $alias => $sender) {
$senders = \iterator_to_array($this->sendersLocator->getSenders($envelope, $handle));

if (null !== $this->eventDispatcher && \count($senders) > 0) {
$event = new SendMessageToTransportsEvent($envelope);
$this->eventDispatcher->dispatch($event);
$envelope = $event->getEnvelope();
}

foreach ($senders as $alias => $sender) {
// on redelivery, only deliver to the given sender
if (null !== $redeliveryStamp && !$redeliveryStamp->shouldRedeliverToSender(\get_class($sender), $alias)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\Messenger\Tests\Middleware;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent;
use Symfony\Component\Messenger\Middleware\SendMessageMiddleware;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
Expand Down Expand Up @@ -202,4 +204,24 @@ public function testItSkipsReceivedMessages()

$this->assertNull($envelope->last(SentStamp::class), 'it does not add sent stamp for received messages');
}

public function testItDispatchesTheEventOnceTime()
{
$envelope = new Envelope(new DummyMessage('original envelope'));

$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher->expects($this->once())
->method('dispatch')
->with(new SendMessageToTransportsEvent($envelope));

$sender1 = $this->getMockBuilder(SenderInterface::class)->getMock();
$sender2 = $this->getMockBuilder(SenderInterface::class)->getMock();

$middleware = new SendMessageMiddleware(new SendersLocator([DummyMessage::class => [$sender1, $sender2]]), $dispatcher);

$sender1->expects($this->once())->method('send')->willReturn($envelope);
$sender2->expects($this->once())->method('send')->willReturn($envelope);

$middleware->handle($envelope, $this->getStackMock(false));
}
}