getStateFrom() public method

get the state this transition points from
public getStateFrom ( ) : State
return State
Ejemplo n.º 1
0
 /**
  * add an outgoing transition from this state.
  *
  * TRICKY: this method should be package visibility only,
  * so don't use directly. it is used to set the bidirectional association
  * for State and Transition from a Transition instance on the state the transition will be allowed to 
  * run from ('state from').
  *
  * @param Transition $transition            
  * @return boolan yes in case the transition was not on the State already or in case of an invalid transition
  */
 public function addTransition(Transition $transition)
 {
     $output = false;
     // check all existing transitions.
     if (!$this->hasTransition($transition->getName()) && $transition->getStateFrom()->getName() == $this->getName() && !$this->isFinal() && !$this->isRegex()) {
         $output = true;
         $this->transitions[] = $transition;
     }
     return $output;
 }
 /**
  * Add the transition, after it has previously been checked that is did not
  * contain states with a regex.
  *
  * @param Transition $transition   
  * @return boolean true in case it was added. false otherwise         
  */
 protected function addTransitionWithoutRegex(Transition $transition)
 {
     // don't allow transitions from a final state
     if ($transition->getStateFrom()->isFinal()) {
         return false;
     }
     // add the transition only if it already exists (no overwrites)
     if ($this->getTransition($transition->getName()) !== null) {
         return false;
     }
     $this->transitions[$transition->getName()] = $transition;
     $from = $transition->getStateFrom();
     // adds state only if it does not already exist (no overwrites)
     $this->addState($from);
     /* 
      * transitions create bidirectional references to the States
      * when they are made, but here the States that are set on the machine
      * can actually be different instances from different transitions (eg:
      * a->b and a->c => we now have two State instances of a)
      * we therefore need to merge the transitions on the existing states.
      * The LoaderArray class does this for us by default, but we do it here
      * too, just in case a client decides to call the 'addTransition' method
      * directly without a loader.
      */
     //get the state known to the machine, to prevent possible bug where client
     //creates 2 different state instances with different configs. we won't know
     //how to resolve this anyway, so just pick the existing state (first in wins)
     $state = $this->getState($from->getName());
     // adds transition only if it does not already exist (no overwrites)
     $state->addTransition($transition);
     $to = $transition->getStateTo();
     // adds state only if it dooes not already exist (no overwrites)
     $this->addState($to);
     return true;
 }
 /**
  * A template method that Stores a failed transition in the storage facility for
  * historical/analytical purposes.
  *
  * @param Identifier $identifier            
  * @param Transition $transition            
  * @param \Exception $e            
  */
 public function setFailedTransition(Identifier $identifier, Transition $transition, \Exception $e)
 {
     // check if it is persisted, otherwise we cannot get the current state
     $message = new \stdClass();
     $message->code = $e->getCode();
     $message->transition = $transition->getName();
     $message->message = $e->getMessage();
     $message->file = $e->getFile();
     $message->line = $e->getLine();
     if ($this->isPersisted($identifier)) {
         /*
         a transition can fail even after a state has been set in the transition process,
         for example when executing the code in the entry action of the new state,
         making the transition partly failed.
         the history will then show a succesful transition to the new state first,
         and here we will then add the failure of the transition with the current state (which is the 'to' state of the transition)
         and with the failure message.
         In case that the transition failed before the state has been set
         then this will be put in the history of transitions with the 'from' state as the current state.
         */
         $state = $this->getState($identifier);
     } else {
         //no current state available in persistence layer.
         //this is exceptional and should not happen when configured correctly and
         //if the machine has been 'added' or if a transition has been (partly) mande.
         //therefore, it must be the from state..
         $state = $transition->getStateFrom()->getName();
     }
     $message->state = $state;
     $this->addHistory($identifier, $state, $message, true);
 }
 /**
  * @test
  */
 public function shouldExpectExceptionsWhenCallingPublicMethodsWithNonDefaultConstructorValues()
 {
     $from = new State('a');
     $to = new State('b');
     $rule = 'izzum\\rules\\BOGUS';
     $command = 'izzum\\command\\BOGUS';
     $object = new Context(new Identifier(Identifier::NULL_ENTITY_ID, Identifier::NULL_STATEMACHINE));
     $transition = new Transition($from, $to, null, $rule, $command);
     $this->assertEquals($from . '_to_' . $to, $transition->getName());
     $this->assertEquals($from, $transition->getStateFrom());
     $this->assertEquals($to, $transition->getStateTo());
     $this->assertContains($transition->getName(), $transition->toString());
     try {
         $command = $transition->getCommand($object);
         $this->fail('should not come here');
     } catch (Exception $e) {
         $this->assertEquals(Exception::COMMAND_CREATION_FAILURE, $e->getCode());
     }
     try {
         $rule = $transition->getRule($object);
         $this->fail('should not come here');
     } catch (Exception $e) {
         $this->assertEquals(Exception::RULE_CREATION_FAILURE, $e->getCode());
     }
 }