Ejemplo n.º 1
0
 /**
  * Process a transition.
  * `\Zhibaihe\State\MachineException` is thrown when:
  * 1. the machine is not initialized;
  * 2. no transition is defined at current state;
  * 3. the transition is illegal at current state.
  *
  * @param  string $transition  The transition to be processed
  * @param  array  $arguments   The arguments for the transition
  * @return void
  *
  * @throws \Zhibaihe\State\MachineException
  */
 public function process($transition, $arguments = [])
 {
     if (!$this->ready()) {
         throw new MachineException("Machine not initialized.");
     }
     if (!array_key_exists($this->state, $this->transitions)) {
         throw new MachineException("No transitions defined for state '{$this->state}'.");
     }
     $transitions = $this->transitions[$this->state];
     if (!array_key_exists($transition, $transitions)) {
         throw new MachineException("Transition '{$transition}' at state '{$this->state}' is invalid.");
     }
     $to = $transitions[$transition];
     if ($this->policy->denies($transition, $this->state, $to, $arguments)) {
         return false;
     }
     $this->triggerWalkers($transition, $this->state, $to, $arguments);
     $this->triggerListeners($transition, $this->state, $to, $arguments);
     $this->state = $to;
     return true;
 }