PHP code example of nwidart / laravel-broadway

1. Go to this page and download the library: Download nwidart/laravel-broadway 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/ */

    

nwidart / laravel-broadway example snippets


// CreatorEnricher.php

class CreatorEnricher implements MetadataEnricher
{
    /** @var int $creatorId */
    private $creatorId;


    /**
     * The constructor
     *
     * @param int $creatorId
     */
    public function __construct($creatorId = null)
    {
        $this->creatorId = $creatorId;
    }


    /**
     * @param Metadata $metadata
     * @return Metadata
     */
    public function enrich(Metadata $metadata)
    {
        if ($this->creatorId !== null) {
            $id = $this->creatorId;
        } else {
            $id = Auth::user()->id;
        }

        return $metadata->merge(Metadata::kv('createorId', $id));
    }
}

// YourServiceProvider.php

/**
 * Register the Metadata enrichers
 */
private function registerEnrichers()
{
    $enricher = new CreatorEnricher();
    $this->app['laravelbroadway.enricher.registry']->subscribe([$enricher]);
}

$this->app->bind(\Modules\Parts\Repositories\EventStorePartRepository::class, function ($app) {
    $eventStore = $app[\Broadway\EventStore\EventStore::class];
    $eventBus = $app[\Broadway\EventHandling\EventBus::class];
    
    $this->registerEnrichers();
    
    return new MysqlEventStorePartRepository($eventStore, $eventBus, $app[Connection::class], [$app[EventStreamDecorator::class]);
});

// PartsThatWhereCreatedProjector.php

public function applyPartWasRenamedEvent(PartWasRenamedEvent $event, DomainMessage $domainMessage)
{
	$metaData = $domainMessage->getMetadata()->serialize();
	$creator = User::find($metaData['creatorId']);    
	
	// Do something with the user
}
 php
    Nwidart\LaravelBroadway\LaravelBroadwayServiceProvider::class
 
 php
    Nwidart\LaravelBroadway\Broadway\CommandServiceProvider::class
    
 php
    Nwidart\LaravelBroadway\Broadway\EventServiceProvider::class
    
 php
    Nwidart\LaravelBroadway\Broadway\SerializersServiceProvider::class
    
 php
    Nwidart\LaravelBroadway\Broadway\EventStorageServiceProvider::class
    
 php
    Nwidart\LaravelBroadway\Broadway\ReadModelServiceProvider::class
    
 php
    Nwidart\LaravelBroadway\Broadway\MetadataEnricherServiceProvider::class
    
 php
    Nwidart\LaravelBroadway\Broadway\SupportServiceProvider::class
    

php artisan vendor:publish
 php
'event-store' => [
    'table' => 'event_store',
    'driver' => 'dbal',
    'connection' => 'default',
],
 php
$this->app->bind(\Modules\Parts\Repositories\EventStorePartRepository::class, function ($app) {
    $eventStore = $app[\Broadway\EventStore\EventStore::class];
    $eventBus = $app[\Broadway\EventHandling\EventBus::class];
    return new MysqlEventStorePartRepository($eventStore, $eventBus);
});
 php
$this->app->bind(\Modules\Parts\Repositories\ReadModelPartRepository::class, function ($app) {
    $serializer = $app[\Broadway\Serializer\Serializer::class];
    return new ElasticSearchReadModelPartRepository($app['Elasticsearch'], $serializer);
});
 php
$this->app->bind(\Modules\Parts\Repositories\ReadModelPartRepository::class, function ($app) {
    $serializer = $app[\Broadway\Serializer\Serializer::class];
    return new InMemoryReadModelPartRepository($app['Inmemory'], $serializer);
});
 php
$partCommandHandler = new PartCommandHandler($this->app[\Modules\Parts\Repositories\EventStorePartRepository::class]);
$someOtherCommandHandler = new SomeOtherCommandHandler($this->app[\Modules\Things\Repositories\EventStoreSomeRepository::class]);

$this->app['laravelbroadway.command.registry']->subscribe([
    $partCommandHandler,
    $someOtherCommandHandler
]);

// OR

$this->app['laravelbroadway.command.registry']->subscribe($partCommandHandler);
$this->app['laravelbroadway.command.registry']->subscribe($someOtherCommandHandler);
 php
$partsThatWereManfacturedProjector = new PartsThatWereManufacturedProjector($this->app[\Modules\Parts\Repositories\ReadModelPartRepository::class]);
$someOtherProjector = new SomeOtherProjector($this->app[\Modules\Things\Repositories\ReadModelSomeRepository::class]);

$this->app['laravelbroadway.event.registry']->subscribe([
    $partsThatWereManfacturedProjector,
    $someOtherProjector
]);

// OR

$this->app['laravelbroadway.event.registry']->subscribe($partsThatWereManfacturedProjector);
$this->app['laravelbroadway.event.registry']->subscribe($someOtherProjector);