From 33d837a4f6fc5206fdb9f45134ccbfe4e82b97d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Santib=C3=A1=C3=B1ez?= Date: Sun, 21 Mar 2021 15:51:18 -0500 Subject: [PATCH] Changed key for afterTransitionHooks callbacks - Updated tests --- README.md | 15 ++++++++------- src/StateMachines/StateMachine.php | 6 +++--- .../StatusWithAfterTransitionHookStateMachine.php | 8 ++++---- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 706c7fe..3fdc0c9 100644 --- a/README.md +++ b/README.md @@ -404,10 +404,11 @@ We can also add custom hooks/callbacks that will be executed before/after a tran To do so, we must override the `beforeTransitionHooks()` and `afterTransitionHooks()` methods in our state machine accordingly. -Both transition hooks methods must return a keyed array with the state and an array of callbacks/closures +Both transition hooks methods must return a keyed array with the state as key, and an array of callbacks/closures to be executed. -> NOTE: The keys for the array must be the `$from` states. +> NOTE: The keys for beforeTransitionHooks() must be the `$from` states. +> NOTE: The keys for afterTransitionHooks() must be the `$to` states. Example @@ -431,12 +432,12 @@ class StatusStateMachine extends StateMachine public function afterTransitionHooks(): array { return [ - 'approved' => [ - function ($to, $model) { - // Dispatch some job AFTER "approved changes to $to" + 'processed' => [ + function ($from, $model) { + // Dispatch some job AFTER "$from transitioned to processed" }, - function ($to, $model) { - // Send mail AFTER "approved changes to $to" + function ($from, $model) { + // Send mail AFTER "$from transitioned to processed" }, ], ]; diff --git a/src/StateMachines/StateMachine.php b/src/StateMachines/StateMachine.php index 289f651..c7db802 100644 --- a/src/StateMachines/StateMachine.php +++ b/src/StateMachines/StateMachine.php @@ -129,11 +129,11 @@ public function transitionTo($from, $to, $customProperties = [], $responsible = $this->model->recordState($field, $from, $to, $customProperties, $responsible, $changedAttributes); } - $afterTransitionHooks = $this->afterTransitionHooks()[$from] ?? []; + $afterTransitionHooks = $this->afterTransitionHooks()[$to] ?? []; collect($afterTransitionHooks) - ->each(function ($callable) use ($to) { - $callable($to, $this->model); + ->each(function ($callable) use ($from) { + $callable($from, $this->model); }); $this->cancelAllPendingTransitions(); diff --git a/tests/TestStateMachines/SalesOrders/StatusWithAfterTransitionHookStateMachine.php b/tests/TestStateMachines/SalesOrders/StatusWithAfterTransitionHookStateMachine.php index dadcf90..b223015 100644 --- a/tests/TestStateMachines/SalesOrders/StatusWithAfterTransitionHookStateMachine.php +++ b/tests/TestStateMachines/SalesOrders/StatusWithAfterTransitionHookStateMachine.php @@ -30,16 +30,16 @@ public function defaultState(): ?string public function afterTransitionHooks(): array { return [ - 'pending' => [ - function($to, $model) { + 'approved' => [ + function($from, $model) { $model->total = 200; $model->save(); }, - function($to, $model) { + function($from, $model) { $model->notes = 'after'; $model->save(); }, - function ($to, $model) { + function ($from, $model) { AfterTransitionJob::dispatch(); }, ]