PHP code example of m1x0n / eventsauce-mongo-message-repository

1. Go to this page and download the library: Download m1x0n/eventsauce-mongo-message-repository 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/ */

    

m1x0n / eventsauce-mongo-message-repository example snippets


$mongoDbName = 'mydb';
$eventsCollectionName = 'events';

// Initialize mongo client and select target database
$client = new MongoDB\Client(
    null,
    [
        'username' => 'user',
        'password' => 'secret'
    ]
);

$database = $client->selectDatabase($mongoDbName);

// Configure message repository or see MongoDbMessageRepositoryFactory
$messageRepository = new \EventSauceExtensions\MongoDbMessageRepository(
    $database,
    new \EventSauceExtensions\MongoDbMessageSerializer(
        new \EventSauce\EventSourcing\Serialization\ConstructingMessageSerializer()
    ),
    $eventsCollectionName
);

// MongoDbMessageSerializer also supports upcasting.
// Upcasting is possible by passing upcaster itself as second argument 

// This is mostly for example and can be replaced with MessageBus dispatcher like RabbitMQ.
// See eventsauce/rabbitmq-bundle-bindings
$messageDispatcher = new \EventSauce\EventSourcing\SynchronousMessageDispatcher();

// MyAggregateClass must implement
$bulbsAggregateRepository = new \EventSauce\EventSourcing\ConstructingAggregateRootRepository(
    MyAggreateClass::class,
    $messageRepository,
    $messageDispatcher
);

$database
    ->selectCollection('events')
    ->createIndex(
    [
        'aggregate_root_id' => 1,
        'aggregate_root_version' => 1,
    ],
    [
        'unique' => true
    ]
);