PHP code example of tienvx / pact-messenger-bundle

1. Go to this page and download the library: Download tienvx/pact-messenger-bundle 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/ */

    

tienvx / pact-messenger-bundle example snippets



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'])
        );
    }
}