Skip to content

Latest commit

 

History

History
69 lines (52 loc) · 2.31 KB

README.md

File metadata and controls

69 lines (52 loc) · 2.31 KB

Pact Messenger Bundle Build Status Coverage Status Version PHP Version

This Symfony Bundle allow collecting dispatched message using Symfony Messenger.

Installation

composer require tienvx/pact-messenger-bundle

Documentation

namespace App\MessageDispatcher;

use App\Message\UserCreated;
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) {
            return null;
        }
        $message = $envelope->getMessage();
        if (!$message instanceof UserCreated) {
            return null;
        }

        return new Message(
            \json_encode([
                'class' => UserCreated::class,
                'id' => $message->userId,
            ]),
            'application/json',
            json_encode(['contentType' => 'application/json'])
        );
    }
}

License

MIT