Skip to content

Commit

Permalink
turn POPO into callable to allow more flexibility during transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
jarektkaczyk committed Jul 22, 2018
1 parent 92551a7 commit 3f13284
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Fsm.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public function getCurrentState() : string
* Process action and return new state after transition.
*
* @param string $action
* @param mixed $payload
* @return string
*/
public function process(string $action) : string
public function process(string $action, $payload = null) : string
{
if (!$this->isActionValid($action)) {
throw new InvalidActionException(
Expand All @@ -86,7 +87,11 @@ public function process(string $action) : string

$transition = $this->transitions[$this->state][$action];

$this->stateful_object->setState($transition->to_state);
if (is_callable($transition)) {
$transition($this->stateful_object, $payload);
} else {
$this->stateful_object->setState($transition->to_state);
}

return $this->state = $transition->to_state;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Sofa\StateMachine;

/**
* simple POPO to handle State Machine transitions
* Simple POPO to handle State Machine transitions
*/
class Transition
{
Expand All @@ -25,4 +25,13 @@ public static function make(string $from_state, string $action, string $to_state
{
return new static($from_state, $action, $to_state);
}

// Extend this class and implement __invoke method to have more flexibility in your transition.
//
// public function __invoke(StateMachineInterface $stateful_object, $payload)
// {
// // do more here if necessary
//
// $stateful_object->setState($this->to_state);
// }
}

0 comments on commit 3f13284

Please sign in to comment.