PHP code example of alessandrominoccheri / laravel-broadway
1. Go to this page and download the library: Download alessandrominoccheri/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/ */
alessandrominoccheri / 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
}