brzuchal/saga

Saga Pattern Implementation

1.x-dev 2023-03-21 11:00 UTC

This package is auto-updated.

Last update: 2024-10-15 04:22:44 UTC


README

Install

composer require brzuchal/saga

Usage

namespace App;

use App\Events\OrderCreated;
use Brzuchal\Saga\Mapping\Saga;
use Brzuchal\Saga\Mapping\SagaMessageHandler;
use Brzuchal\Saga\Mapping\SagaStart;

#[Saga]
class OrderProcessing
{
    #[SagaStart,SagaMessageHandler(associationKey: 'orderId', property: 'id')]
    public function whenCreated(OrderCreated $event): void
    {
        // ...
    }
}

Configuration

use App\OrderProcessing;
use App\Events\OrderCreated;
use Brzuchal\Saga\Mapping\AttributeMappingDriver;
use Brzuchal\Saga\Mapping\SagaMetadataFactory;
use Brzuchal\Saga\Repository\SimpleSagaRepositoryFactory;
use Brzuchal\Saga\SagaManager;
use Brzuchal\Saga\Store\InMemorySagaStore;

$repositoryFactory = new SimpleSagaRepositoryFactory(
    new InMemorySagaStore(), 
    new SagaMetadataFactory([new AttributeMappingDriver()]),
);

$manager = new SagaManager($repositoryFactory->create(OrderProcessing::class));
$manager(new OrderCreated());