This Symfony Bundle allow collecting dispatched message using Symfony Messenger.
composer require tienvx/pact-messenger-bundle
namespace App\MessageDispatcher;
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;
use Tienvx\Bundle\PactMessengerBundle\Service\EnvelopeCollectorInterface;
use Tienvx\Bundle\PactProviderBundle\Attribute\AsMessageDispatcher;
use Tienvx\Bundle\PactProviderBundle\Model\Message;
use Tienvx\Bundle\PactProviderBundle\MessageDispatcher\DispatcherInterface;
#[AsMessageDispatcher(description: 'User created message')]
class UserDispatcher implements DispatcherInterface
{
public function __construct(private EnvelopeCollectorInterface $collector)
{
}
public function dispatch(): ?Message
{
$envelope = $this->collector->getSingle(UserCreated::class);
if ($envelope) {
$transports = $envelope->last(TransportNamesStamp::class)?->getTransportNames() ?? [];
return new Message(serialize($envelope->getMessage()), 'text/plain', json_encode(['transports' => $transports]));
}
return null;
}
}