yvoyer /
php-state
| 1 | <?php declare(strict_types=1); |
||
| 2 | /** |
||
| 3 | * This file is part of the php-state project. |
||
| 4 | * |
||
| 5 | * (c) Yannick Voyer <[email protected]> (http://github.com/yvoyer) |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace Star\Component\State; |
||
| 9 | |||
| 10 | use Star\Component\State\Callbacks\AlwaysThrowExceptionOnFailure; |
||
| 11 | use Star\Component\State\Callbacks\TransitionCallback; |
||
| 12 | use Star\Component\State\Event\StateEventStore; |
||
| 13 | use Star\Component\State\Event\TransitionWasFailed; |
||
| 14 | use Star\Component\State\Event\TransitionWasSuccessful; |
||
| 15 | use Star\Component\State\Event\TransitionWasRequested; |
||
| 16 | |||
| 17 | final class StateMachine |
||
|
0 ignored issues
–
show
Coding Style
Documentation
introduced
by
Loading history...
|
|||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var EventRegistry |
||
| 21 | */ |
||
| 22 | private $listeners; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var StateRegistry |
||
| 26 | */ |
||
| 27 | private $states; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $currentState; |
||
| 33 | |||
| 34 | 46 | public function __construct( |
|
| 35 | string $currentState, |
||
| 36 | StateRegistry $states, |
||
| 37 | EventRegistry $listeners |
||
| 38 | ) { |
||
| 39 | 46 | $this->listeners = $listeners; |
|
| 40 | 46 | $this->states = $states; |
|
| 41 | 46 | $this->setCurrentState($currentState); |
|
| 42 | 46 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $transitionName The transition name |
||
| 46 | * @param mixed $context |
||
| 47 | * @param TransitionCallback|null $callback |
||
| 48 | * |
||
| 49 | * @return string The next state to store on your context |
||
| 50 | * @throws InvalidStateTransitionException |
||
| 51 | * @throws NotFoundException |
||
| 52 | */ |
||
| 53 | 30 | public function transit(string $transitionName, $context, TransitionCallback $callback = null): string |
|
| 54 | { |
||
| 55 | 30 | if (! $callback) { |
|
| 56 | 29 | $callback = new AlwaysThrowExceptionOnFailure(); |
|
| 57 | } |
||
| 58 | |||
| 59 | 30 | $this->listeners->dispatch( |
|
| 60 | 30 | StateEventStore::BEFORE_TRANSITION, |
|
| 61 | 30 | new TransitionWasRequested($transitionName) |
|
| 62 | ); |
||
| 63 | |||
| 64 | 30 | $transition = $this->states->getTransition($transitionName); |
|
| 65 | 29 | $callback->beforeStateChange($context, $this); |
|
| 66 | |||
| 67 | 29 | $newState = $transition->getDestinationState(); |
|
| 68 | 29 | $allowed = $this->states->transitionStartsFrom($transitionName, $this->currentState); |
|
| 69 | 29 | if (! $allowed) { |
|
| 70 | 11 | $exception = InvalidStateTransitionException::notAllowedTransition( |
|
| 71 | 11 | $transitionName, |
|
| 72 | $context, |
||
| 73 | 11 | $this->currentState |
|
| 74 | ); |
||
| 75 | |||
| 76 | 11 | $this->listeners->dispatch( |
|
| 77 | 11 | StateEventStore::FAILURE_TRANSITION, |
|
| 78 | 11 | new TransitionWasFailed($transitionName, $exception) |
|
| 79 | ); |
||
| 80 | |||
| 81 | 11 | $newState = $callback->onFailure($exception, $context, $this); |
|
| 82 | } |
||
| 83 | |||
| 84 | 19 | $this->setCurrentState($newState); |
|
| 85 | |||
| 86 | 19 | $callback->afterStateChange($context, $this); |
|
| 87 | |||
| 88 | 19 | $this->listeners->dispatch( |
|
| 89 | 19 | StateEventStore::AFTER_TRANSITION, |
|
| 90 | 19 | new TransitionWasSuccessful($transitionName) |
|
| 91 | ); |
||
| 92 | |||
| 93 | 19 | return $this->currentState; |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $stateName |
||
| 98 | * @return bool |
||
| 99 | * @throws NotFoundException |
||
| 100 | */ |
||
| 101 | 34 | public function isInState(string $stateName): bool |
|
| 102 | { |
||
| 103 | 34 | if (! $this->states->hasState($stateName)) { |
|
| 104 | 1 | throw NotFoundException::stateNotFound($stateName); |
|
| 105 | } |
||
| 106 | |||
| 107 | 33 | return $this->currentState === $stateName; |
|
| 108 | } |
||
| 109 | |||
| 110 | 9 | public function hasAttribute(string $attribute): bool |
|
| 111 | { |
||
| 112 | 9 | return $this->states->hasAttribute($this->currentState, $attribute); |
|
| 113 | } |
||
| 114 | |||
| 115 | public function addListener(string $event, \Closure $listener): void |
||
| 116 | { |
||
| 117 | $this->listeners->addListener($event, $listener); |
||
| 118 | } |
||
| 119 | |||
| 120 | 3 | public function acceptTransitionVisitor(TransitionVisitor $visitor): void |
|
| 121 | { |
||
| 122 | 3 | $this->states->acceptTransitionVisitor($visitor); |
|
| 123 | 3 | } |
|
| 124 | |||
| 125 | 1 | public function acceptStateVisitor(StateVisitor $visitor): void |
|
| 126 | { |
||
| 127 | 1 | $this->states->acceptStateVisitor($visitor); |
|
| 128 | 1 | } |
|
| 129 | |||
| 130 | 46 | private function setCurrentState(string $state): void |
|
| 131 | { |
||
| 132 | 46 | $this->currentState = $state; |
|
| 133 | 46 | } |
|
| 134 | } |
||
| 135 |