/**
  * @param TransitionInterface $transition
  *
  * @return int
  */
 protected function calculcateScore(TransitionInterface $transition)
 {
     $score = 0;
     if ($transition->getEventName()) {
         $score += 2;
     }
     if ($transition->getConditionName()) {
         ++$score;
     }
     return $score;
 }
 /**
  * @param \ArrayAccess   $context
  * @param EventInterface $event
  */
 protected function doCheckTransitions(\ArrayAccess $context, EventInterface $event = null)
 {
     try {
         $transitions = $this->currentState->getTransitions();
         $activeTransitions = new ActiveTransitionFilter($transitions, $this->getSubject(), $context, $event);
         $this->selectedTransition = $this->transitonSelector->selectTransition($activeTransitions);
         if ($this->selectedTransition) {
             $targetState = $this->selectedTransition->getTargetState();
             if ($this->currentState != $targetState) {
                 $this->lastState = $this->currentState;
                 $this->currentState = $targetState;
                 $this->notify();
                 $this->selectedTransition = null;
                 $this->lastState = null;
             }
             $this->checkTransitions();
         }
     } catch (\Exception $exception) {
         $message = 'Exception was thrown when doing a transition from current state "' . $this->currentState->getName() . '"';
         if ($this->currentEvent instanceof NamedInterface) {
             $message .= ' with event "' . $this->currentEvent->getName() . '"';
         }
         throw new \RuntimeException($message, 0, $exception);
     }
 }
 /**
  * @param TransitionInterface $sourceTransition
  *
  * @throws \InvalidArgumentException
  *
  * @return \Metabor\Statemachine\Transition
  */
 protected function createTransition(TransitionInterface $sourceTransition)
 {
     $targetStateName = $sourceTransition->getTargetState()->getName();
     $targetState = $this->findOrCreateState($targetStateName);
     $this->mergeMetadata($sourceTransition->getTargetState(), $targetState);
     $eventName = $sourceTransition->getEventName();
     if ($sourceTransition->getConditionName()) {
         if ($sourceTransition instanceof Transition) {
             $condition = $sourceTransition->getCondition();
         } else {
             throw new \InvalidArgumentException('Overwrite this method to implement a different type!');
         }
     } else {
         $condition = null;
     }
     return new Transition($targetState, $eventName, $condition);
 }
 /**
  *
  * @param ArrayAccess    $context
  * @param EventInterface $event
  */
 protected function doCheckTransitions(ArrayAccess $context, EventInterface $event = null)
 {
     $transitions = $this->currentState->getTransitions();
     $activeTransitions = new ActiveTransitionFilter($transitions, $this->getSubject(), $context, $event);
     $this->selectedTransition = $this->transitonSelector->selectTransition($activeTransitions);
     if ($this->selectedTransition) {
         $this->beforeTransition($this->getSubject(), $this->selectedTransition->getTargetState()->getName());
         $this->currentState = $this->selectedTransition->getTargetState();
         $this->notify();
         $this->selectedTransition = null;
         $this->checkTransitions();
     }
 }
 /**
  * @param StateInterface      $state
  * @param TransitionInterface $transition
  */
 protected function addTransition(StateInterface $state, TransitionInterface $transition)
 {
     $sourceStateVertex = $this->createStatusVertex($state);
     $targetStateVertex = $this->createStatusVertex($transition->getTargetState());
     $edge = $sourceStateVertex->createEdgeTo($targetStateVertex);
     $label = $this->getTransitionLabel($state, $transition);
     if ($label) {
         if (method_exists($edge, 'setLayoutAttribute')) {
             $edge->setLayoutAttribute('label', $label);
         } else {
             $edge->setAttribute('graphviz.label', $label);
         }
     }
     $eventName = $transition->getEventName();
     if ($eventName) {
         $event = $state->getEvent($eventName);
         if ($event instanceof \ArrayAccess) {
             $layout = $this->getLayoutOptions($event, $this->eventLayout);
             if (method_exists($edge, 'setLayout')) {
                 $edge->setLayout($layout);
             } else {
                 foreach ($layout as $name => $value) {
                     $edge->setAttribute('graphviz.' . $name, $value);
                 }
             }
         }
     }
 }
Exemple #6
0
 /**
  *
  * @param StateInterface      $state
  * @param TransitionInterface $transition
  */
 protected function addTransition(StateInterface $state, TransitionInterface $transition)
 {
     $sourceStateVertex = $this->createStatusVertex($state);
     $targetStateVertex = $this->createStatusVertex($transition->getTargetState());
     $edge = $sourceStateVertex->createEdgeTo($targetStateVertex);
     $label = $this->getTransitionLabel($state, $transition);
     if ($label) {
         $edge->setLayoutAttribute('label', $label);
     }
     $eventName = $transition->getEventName();
     if ($eventName) {
         $event = $state->getEvent($eventName);
         if ($event instanceof \ArrayAccess) {
             $layout = $this->getLayoutOptions($event, $this->eventLayout);
             $edge->setLayout($layout);
         }
     }
 }
 /**
  * @param TransitionInterface $sourceTransition
  *
  * @throws \InvalidArgumentException
  *
  * @return \Metabor\Statemachine\Transition
  */
 protected function createTransition(TransitionInterface $sourceTransition)
 {
     $targetStateName = $sourceTransition->getTargetState()->getName();
     $targetState = $this->findOrCreateState($targetStateName);
     $this->mergeMetadata($sourceTransition->getTargetState(), $targetState);
     $eventName = $sourceTransition->getEventName();
     $condition = $this->createCondition($sourceTransition);
     $transition = new Transition($targetState, $eventName, $condition);
     if ($sourceTransition instanceof WeightedInterface) {
         $transition->setWeight($sourceTransition->getWeight());
     }
     return $transition;
 }