-
Notifications
You must be signed in to change notification settings - Fork 74
Add ability to specify from/to arrays within transitions #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| vendor | ||
| bin | ||
| composer.lock | ||
| .idea | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,4 +204,24 @@ function it_returns_possible_transitions($object, $callbackFactory, CallbackInte | |
|
|
||
| $this->getPossibleTransitions()->shouldReturn(array('create', 'confirm')); | ||
| } | ||
|
|
||
| function it_can_accept_specific_from_to_transition_mappings($object, $dispatcher, $callbackFactory, CallbackInterface $guard) | ||
| { | ||
| $transition = $this->config['transitions']['create']; | ||
|
|
||
| $this->config['transitions']['create'] = array( | ||
| $transition | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you only test that an array of 1 transition works the same as before.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 great catch, I absolutely will. |
||
| ); | ||
|
|
||
| $object->getState()->shouldBeCalled()->willReturn('checkout'); | ||
| $object->setState(Argument::any())->shouldNotBeCalled(); | ||
|
|
||
| $dispatcher->dispatch(SMEvents::TEST_TRANSITION, Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled(); | ||
|
|
||
| $callbackFactory->get($this->config['callbacks']['guard']['guard-confirm'])->shouldBeCalled()->willReturn($guard); | ||
|
|
||
| $guard->__invoke(Argument::type('SM\\Event\\TransitionEvent'))->shouldBeCalled()->willReturn(true); | ||
|
|
||
| $this->can('create')->shouldReturn(true); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,23 +83,16 @@ public function __construct( | |
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function can($transition) | ||
| public function can($transitionName) | ||
| { | ||
| if (!isset($this->config['transitions'][$transition])) { | ||
| throw new SMException(sprintf( | ||
| 'Transition "%s" does not exist on object "%s" with graph "%s"', | ||
| $transition, | ||
| get_class($this->object), | ||
| $this->config['graph'] | ||
| )); | ||
| } | ||
| $transition = $this->getTransition($transitionName); | ||
|
|
||
| if (!in_array($this->getState(), $this->config['transitions'][$transition]['from'])) { | ||
| if (!$transition) { | ||
| return false; | ||
| } | ||
|
|
||
| $can = true; | ||
| $event = new TransitionEvent($transition, $this->getState(), $this->config['transitions'][$transition], $this); | ||
| $event = new TransitionEvent($transitionName, $this->getState(), $transition, $this); | ||
| if (null !== $this->dispatcher) { | ||
| $this->dispatcher->dispatch(SMEvents::TEST_TRANSITION, $event); | ||
|
|
||
|
|
@@ -112,23 +105,25 @@ public function can($transition) | |
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function apply($transition, $soft = false) | ||
| public function apply($transitionName, $soft = false) | ||
| { | ||
| if (!$this->can($transition)) { | ||
| if (!$this->can($transitionName)) { | ||
| if ($soft) { | ||
| return false; | ||
| } | ||
|
|
||
| throw new SMException(sprintf( | ||
| 'Transition "%s" cannot be applied on state "%s" of object "%s" with graph "%s"', | ||
| $transition, | ||
| $transitionName, | ||
| $this->getState(), | ||
| get_class($this->object), | ||
| $this->config['graph'] | ||
| )); | ||
| } | ||
|
|
||
| $event = new TransitionEvent($transition, $this->getState(), $this->config['transitions'][$transition], $this); | ||
| $transition = $this->getTransition($transitionName); | ||
|
|
||
| $event = new TransitionEvent($transitionName, $this->getState(), $transition, $this); | ||
|
|
||
| if (null !== $this->dispatcher) { | ||
| $this->dispatcher->dispatch(SMEvents::PRE_TRANSITION, $event); | ||
|
|
@@ -140,7 +135,7 @@ public function apply($transition, $soft = false) | |
|
|
||
| $this->callCallbacks($event, 'before'); | ||
|
|
||
| $this->setState($this->config['transitions'][$transition]['to']); | ||
| $this->setState($transition['to']); | ||
|
|
||
| $this->callCallbacks($event, 'after'); | ||
|
|
||
|
|
@@ -232,4 +227,36 @@ protected function callCallbacks(TransitionEvent $event, $position) | |
| } | ||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * @param $transitionName | ||
| * | ||
| * @return bool|mixed | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 agreed; it's more semantic this way. Thanks again. I'll include this in an update later today/tonight. |
||
| * @throws SMException | ||
| */ | ||
| private function getTransition($transitionName) | ||
| { | ||
| if (!isset($this->config['transitions'][$transitionName])) { | ||
| throw new SMException(sprintf( | ||
| 'Transition "%s" does not exist on object "%s" with graph "%s"', | ||
| $transitionName, | ||
| get_class($this->object), | ||
| $this->config['graph'] | ||
| )); | ||
| } | ||
|
|
||
| $transition = $this->config['transitions'][$transitionName]; | ||
|
|
||
| if (array_key_exists('from', $transition)) { | ||
| $transition = array($transition); | ||
| } | ||
|
|
||
| $state = $this->getState(); | ||
|
|
||
| $results = array_filter($transition, function($value) use ($state) { | ||
| return in_array($state, $value['from']); | ||
| }); | ||
|
|
||
| return count($results) > 0 ? array_shift($results) : false; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put it in your own
.git/info/excludefile?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can... generally don't b/c some projects do check this in. 👍