/** * Get role of current step. * * @param Transition $transition The transition being in. * @param Item $item The entity being transits. * * @return Permission|null */ protected function getStepPermission(Transition $transition, Item $item) { $stepName = $item->getCurrentStepName(); $step = $transition->getWorkflow()->getStep($stepName); $permission = $step->getPermission(); return $permission; }
function it_creates_the_repository_based_transition_handler(Item $item, Workflow $workflow, StateRepository $stateRepository, EntityManager $entityManager, EntityRepository $entityRepository, Listener $listener) { $entityManager->getRepository('test')->willReturn($entityRepository); $item->isWorkflowStarted()->willReturn(false); $item->getEntity()->willReturn(static::$entity); $this->setListener($listener); $this->createTransitionHandler($item, $workflow, null, 'test', $stateRepository)->shouldHaveType('Netzmacht\\Workflow\\Handler\\RepositoryBasedTransitionHandler'); }
function it_does_not_match_if_step_role_is_not_granted(Transition $transition, Item $item, Context $context, User $user, Permission $permission, ErrorCollection $errorCollection) { $item->isWorkflowStarted()->willReturn(true); $item->getCurrentStepName()->willReturn('step'); $permission->__toString()->willReturn('workflow/permission'); $user->hasPermission($permission)->willReturn(false); $errorCollection->addError(Argument::cetera())->shouldBeCalled(); $this->match($transition, $item, $context, $errorCollection)->shouldReturn(false); }
/** * 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); } }
/** * Get entity. * * @return Entity */ public function getEntity() { return $this->item->getEntity(); }
/** * Get the entity of the item and protect entity type. * * @param Item $item Workflow item. * * @return Entity * * @hrows AssertionException If entity is not an Instance of */ protected function getEntity(Item $item) { $entity = $item->getEntity(); Assertion::isInstanceOf($entity, 'ContaoCommunityAlliance\\DcGeneral\\Data\\ModelInterface', 'Invalid entity given'); return $entity; }
/** * {@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; }
/** * Check if a specific transition is available. * * @param Item $item The workflow item. * @param Context $context Transition context. * @param ErrorCollection $errorCollection Error collection. * @param string $transitionName The transition name. * * @return bool */ public function isTransitionAvailable(Item $item, Context $context, ErrorCollection $errorCollection, $transitionName) { if (!$item->isWorkflowStarted()) { return $this->getStartTransition()->getName() === $transitionName; } $step = $this->getStep($item->getCurrentStepName()); if (!$step->isTransitionAllowed($transitionName)) { return false; } $transition = $this->getTransition($transitionName); return $transition->isAvailable($item, $context, $errorCollection); }
/** * {@inheritdoc} */ public function getWorkflowByItem(Item $item) { return $this->getWorkflow($item->getEntityId(), $item->getEntity()); }
/** * {@inheritdoc} */ public function match(Transition $transition, Item $item, Context $context, ErrorCollection $errorCollection) { $value = $this->getEntityValue($item->getEntity()); if (Comparison::compare($value, $this->value, $this->operator)) { return true; } $errorCollection->addError('transition.condition.property.failed', array($this->property, $value, $this->operator, $this->value)); return false; }
function it_transits_to_next_state(Form $form, Transition $transition, Item $item, Listener $listener, State $state) { $listener->onBuildForm(Argument::cetera())->shouldBeCalled(); $listener->onValidate(Argument::cetera())->willReturn(true); $listener->onPreTransit(Argument::cetera())->shouldBeCalled(); $listener->onPostTransit(Argument::cetera())->shouldBeCalled(); $item->transit($transition, Argument::type(static::CONTEXT_CLASS), Argument::type(static::ERROR_COLLECTION_CLASS), true)->shouldBeCalled()->willReturn($state); $transition->executeActions($item, Argument::type(static::CONTEXT_CLASS), Argument::type(static::ERROR_COLLECTION_CLASS))->willReturn(true)->shouldBeCalled(); $transition->executePostActions($item, Argument::type(static::CONTEXT_CLASS), Argument::type(static::ERROR_COLLECTION_CLASS))->willReturn(true)->shouldBeCalled(); $transition->buildForm($form, $item)->shouldBeCalled(); $transition->checkPreCondition($item, Argument::type(static::CONTEXT_CLASS), Argument::type(static::ERROR_COLLECTION_CLASS))->shouldBeCalled(); $this->validate($form); $this->transit()->shouldHaveType('Netzmacht\\Workflow\\Flow\\State'); }
function it_knows_if_transition_is_available_for_an_item(Item $item, Step $step, Context $context, Transition $transition, ErrorCollection $errorCollection) { $item->isWorkflowStarted()->willReturn(true); $item->getCurrentStepName()->willReturn('started'); $transition->getName()->willReturn('next'); $transition->isAvailable($item, $context, $errorCollection)->shouldBeCalled()->willReturn(true); $this->addTransition($transition); $step->getName()->willReturn('started'); $step->isTransitionAllowed('next')->willReturn(true); $this->addStep($step); $this->isTransitionAvailable($item, $context, $errorCollection, 'next')->shouldReturn(true); }
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'); }