/** * Guard that requested transition is allowed. * * @param string $transitionName Transition to be processed. * * @throws WorkflowException If Transition is not allowed. * * @return void */ private function guardAllowedTransition($transitionName) { if (!$this->isWorkflowStarted()) { if (!$transitionName || $transitionName === $this->getWorkflow()->getStartTransition()->getName()) { return; } throw new WorkflowException(sprintf('Not allowed to process transition "%s". Workflow "%s" not started for item "%s"', $transitionName, $this->workflow->getName(), $this->item->getEntityId())); } $step = $this->getCurrentStep(); if (!$step->isTransitionAllowed($transitionName)) { throw new WorkflowException(sprintf('Not allowed to process transition "%s". Transition is not allowed in step "%s"', $transitionName, $step->getName())); } }
/** * Guard that already started workflow is the same which is tried to be ran now. * * @param Item $item Current workflow item. * @param Workflow $workflow Selected workflow. * * @throws WorkflowException If item workflow is not the same as current workflow. * * @return void */ private function guardSameWorkflow(Item $item, Workflow $workflow) { if ($item->isWorkflowStarted() && $item->getWorkflowName() != $workflow->getName()) { $message = sprintf('Item "%s" already process workflow "%s" and cannot be handled by "%s"', $item->getEntityId(), $item->getWorkflowName(), $workflow->getName()); throw new WorkflowException($message); } }
/** * {@inheritdoc} */ public function getWorkflowByItem(Item $item) { return $this->getWorkflow($item->getEntityId(), $item->getEntity()); }
/** * {@inheritdoc} * * @SuppressWarnings(PHPMD.UnusedLocalVariable) * @SuppressWarnings(PHPMD.EvalExpression) */ public function match(Transition $transition, Item $item, Context $context, ErrorCollection $errorCollection) { if (!$this->compiled) { return false; } // Vars are created so that eval can access them. $entity = $item->getEntity(); $entityId = $item->getEntityId(); // @codingStandardsIgnoreStart if (eval($this->compiled)) { // @codingStandardsIgnoreEnd return true; } $errorCollection->addError($this->message, array($this->expression)); return false; }
function it_throws_than_matches_workflow_is_not_same_as_current(Workflow $workflow, Item $item, EntityId $entityId, Transition $transition, Step $step) { $step->getName()->willReturn('start'); $step->isTransitionAllowed('next')->willReturn(true); $item->getEntityId()->willReturn($entityId); $item->getEntity()->willReturn(static::$entity); $item->isWorkflowStarted()->willReturn(true); $item->getCurrentStepName()->willReturn('start'); $item->getWorkflowName()->willReturn('workflow_a'); $workflow->match($entityId, static::$entity)->willReturn(true); $workflow->getStep('start')->willReturn($step); $workflow->getTransition('next')->willReturn($transition); $workflow->getName()->willReturn('workflow_b'); $entityId->getProviderName()->willReturn(static::ENTITY_PROVIDER_NAME); $entityId->__toString()->willReturn(static::ENTITY_PROVIDER_NAME . '::' . static::ENTITY_ID); $this->shouldThrow('Netzmacht\\Workflow\\Flow\\Exception\\WorkflowException')->duringHandle($item, 'next'); }