jozi/laravel-rabbitevents-sourcing

Integration between nuwber/rabbitevents and spatie/laravel-event-sourcing packages

1.0.2 2021-08-02 03:42 UTC

This package is auto-updated.

Last update: 2024-04-29 04:40:55 UTC


README

A simple integration between nuwber/rabbitevents and spatie/laravel-event-sourcing. Both are used to facilitate event sourcing and intraservice communication using RabbitMQ topic exchanges.

Installation

Using composer:

composer require jozi/laravel-rabbitevents-sourcing

Usage/Examples

All stored and published events extends the StoredRabbitEvent class. These events will be handled for both event sourcing (spatie/laravel-event-sourcing) and publishing to RabbitMQ (nuwber/rabbitevents).

For this class, a string $eventKey is explictly required for the event to be published. The $eventKey is the same as RabbitMQ's routing key.

use Jozi\Events\StoredRabbitEvent;

class AccountCreated extends StoredRabbitEvent
{
    public $eventKey = 'account.created';

    /** @var array */
    public $accountAttributes;

    public function __construct(array $accountAttributes)
    {
        $this->accountAttributes = $accountAttributes;
    }

    public function toPublish(): array
    {
        return $this->accountAttributes;
    }
}

After an event has been defined, you may invoke it using the publish_event helper function. It is just a simple wrapper for invoking both event and publish as a one-liner.

class Account extends Model
{
    protected $guarded = [];

    public static function createWithAttributes(array $attributes): Account
    {
        /*
         * Let's generate a uuid.
         */
        $attributes['uuid'] = (string) Uuid::uuid4();

        /*
         * The account will be created inside this event using the generated uuid.
         */
        publish_event(new AccountCreated($attributes));

        /*
         * The uuid will be used the retrieve the created account.
         */
        return static::getByUuid($attributes['uuid']);
    }
}

Acknowledgements

License

MIT