PHP code example of smskin / laravel-saga
1. Go to this page and download the library: Download smskin/laravel-saga 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/ */
smskin / laravel-saga example snippets artisan vendor:publish --provider=SMSkin\LaravelSaga\Providers\ServiceProvider
$this->builder()
->correlatedById(EUserCreated::class, static function (EUserCreated $event) {
return $event->corrId;
})
$this->builder()
->correlatedBy(EUserBlocked::class, 'userId', static function (EUserBlocked $event) {
return $event->userId;
});
$this->builder()
->onInitEvent(CreateUserCommand::class, static function (CreateUserCommand $command) {
return (new SagaExampleContext($command->correlationId))
->setEmail($command->email);
});
$this->builder()
->initial()
->transitionTo(SagaExampleStates::USER_CREATING)
->activity(UserCreatingActivity::class)
->then(function () {
(new UserCommandService())->create(
$this->context->getId(),
$this->context->getEmail()
);
});
$this->builder()
->duringState(SagaExampleStates::USER_CREATING)
->on(EUserCreated::class)
->then(function () {
$event = $this->getHandledEvent();
$this->context->setUserId($event->userId);
})
->transitionTo(SagaExampleStates::USER_BLOCKING)
->then(function () {
(new UserCommandService())->block(
$this->context->getUserId()
);
});
$this->builder()
->duringState(SagaExampleStates::USER_BLOCKING)
->on(EUserBlocked::class)
->finalize()
->publish(function () {
return new ESagaExampleFinalized($this->context->getId());
});
php artisan saga:cache
php artisan saga:cache:clear
config/saga.php