/** * Checks if current workflow item allows transition * * @param WorkflowData $value * @param TransitionIsAllowed $constraint */ public function validate($value, Constraint $constraint) { /** @var WorkflowItem $workflowItem */ $workflowItem = $constraint->getWorkflowItem(); $transitionName = $constraint->getTransitionName(); $workflow = $this->registry->getWorkflow($workflowItem->getWorkflowName()); $errors = new ArrayCollection(); $result = false; try { $result = $workflow->isTransitionAllowed($workflowItem, $transitionName, $errors, true); } catch (InvalidTransitionException $e) { switch ($e->getCode()) { case InvalidTransitionException::UNKNOWN_TRANSITION: $errors->add(array('message' => $constraint->unknownTransitionMessage, 'parameters' => array('{{ transition }}' => $transitionName))); break; case InvalidTransitionException::NOT_START_TRANSITION: $errors->add(array('message' => $constraint->notStartTransitionMessage, 'parameters' => array('{{ transition }}' => $transitionName))); break; case InvalidTransitionException::STEP_HAS_NO_ALLOWED_TRANSITION: $errors->add(array('message' => $constraint->stepHasNotAllowedTransitionMessage, 'parameters' => array('{{ transition }}' => $transitionName, '{{ step }}' => $workflowItem->getCurrentStep()->getName()))); break; } } if (!$result) { if ($errors->count()) { foreach ($errors as $error) { $this->context->addViolation($error['message'], $error['parameters']); } } else { $this->context->addViolation($constraint->someConditionsNotMetMessage); } } }
/** * @param WorkflowItem $workflowItem * @return WorkflowItem * @throws WorkflowException */ public function updateAclIdentities(WorkflowItem $workflowItem) { $workflow = $this->workflowRegistry->getWorkflow($workflowItem->getWorkflowName()); $definition = $workflowItem->getDefinition(); $currentStepName = $workflowItem->getCurrentStep()->getName(); $aclIdentities = array(); foreach ($definition->getEntityAcls() as $entityAcl) { if ($entityAcl->getStep()->getName() == $currentStepName) { $attributeName = $entityAcl->getAttribute(); $attribute = $workflow->getAttributeManager()->getAttribute($attributeName); $entity = $workflowItem->getData()->get($attributeName); if (!$entity) { continue; } if (!is_object($entity)) { throw new WorkflowException(sprintf('Value of attribute "%s" must be an object', $attributeName)); } $aclIdentity = new WorkflowEntityAclIdentity(); $aclIdentity->setAcl($entityAcl)->setEntityClass($attribute->getOption('class'))->setEntityId($this->doctrineHelper->getSingleEntityIdentifier($entity)); $aclIdentities[] = $aclIdentity; } } $workflowItem->setAclIdentities($aclIdentities); return $workflowItem; }
/** * Bind entities to workflow item * * @param WorkflowItem $workflowItem * @return bool Returns true if new entities were bound */ public function bindEntities(WorkflowItem $workflowItem) { $workflowData = $workflowItem->getData(); if (!$workflowData->isModified()) { return false; } $workflow = $this->workflowRegistry->getWorkflow($workflowItem->getWorkflowName()); $bindAttributeNames = $workflow->getAttributeManager()->getBindEntityAttributeNames(); $entitiesToBind = $workflowData->getValues($bindAttributeNames); return $workflowItem->syncBindEntities($this->createBindEntities($entitiesToBind)); }
/** * Get workflow instance. * * string - workflow name * WorkflowItem - getWorkflowName() method will be used to get workflow * Workflow - will be returned by itself * * @param string|Workflow|WorkflowItem $workflowIdentifier * @throws WorkflowException * @return Workflow */ public function getWorkflow($workflowIdentifier) { if (is_string($workflowIdentifier)) { return $this->workflowRegistry->getWorkflow($workflowIdentifier); } elseif ($workflowIdentifier instanceof WorkflowItem) { return $this->workflowRegistry->getWorkflow($workflowIdentifier->getWorkflowName()); } elseif ($workflowIdentifier instanceof Workflow) { return $workflowIdentifier; } throw new WorkflowException('Can\'t find workflow by given identifier.'); }
/** * Custom options: * - "attribute_fields" - required, list of attributes form types options * - "workflow_item" - optional, instance of WorkflowItem entity * - "workflow" - optional, instance of Workflow * - "disable_attribute_fields" - optional, a flag to disable all attributes fields * * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setRequired(array('workflow_item')); $resolver->setDefaults(array('workflow' => function (Options $options, $workflow) { if (!$workflow) { $workflowName = $options['workflow_item']->getWorkflowName(); $workflow = $this->workflowRegistry->getWorkflow($workflowName); } return $workflow; })); $resolver->setOptional(array('attribute_fields', 'attribute_default_values', 'init_actions', 'workflow')); $resolver->setDefaults(array('data_class' => 'Oro\\Bundle\\WorkflowBundle\\Model\\WorkflowData', 'disable_attribute_fields' => false, 'attribute_fields' => array(), 'attribute_default_values' => array())); $resolver->setAllowedTypes(array('workflow_item' => 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowItem', 'workflow' => 'Oro\\Bundle\\WorkflowBundle\\Model\\Workflow', 'attribute_fields' => 'array', 'attribute_default_values' => 'array', 'init_actions' => 'Oro\\Bundle\\WorkflowBundle\\Model\\Action\\ActionInterface')); }
public function testHasActiveWorkflowByEntityClassNoWorkflow() { $entityClass = '\\stdClass'; $managerRegistry = $this->createManagerRegistryMock(); $workflowAssembler = $this->createWorkflowAssemblerMock(); $configProvider = $this->createConfigurationProviderMock(); $configProvider->expects($this->once())->method('hasConfig')->with($entityClass)->will($this->returnValue(false)); $workflowRegistry = new WorkflowRegistry($managerRegistry, $workflowAssembler, $configProvider); $this->assertFalse($workflowRegistry->hasActiveWorkflowByEntityClass($entityClass)); }
/** * @return Workflow */ public function getWorkflow() { return $this->workflowRegistry->getWorkflow($this->getWorkflowName()); }
/** * Check for workflow instances * * @param string $entityClass * @return bool */ public function hasWorkflows($entityClass) { return count($this->workflowRegistry->getWorkflowsByEntityClass($entityClass)) > 0; }
public function testGetWorkflowsByEntityClass() { $entityClass = '\\stdClass'; $workflowName = 'test_workflow'; $workflowDefinition = new WorkflowDefinition(); $workflowDefinition->setName($workflowName); /** @var Workflow $workflow */ $workflow = $this->getMockBuilder('Oro\\Bundle\\WorkflowBundle\\Model\\Workflow')->disableOriginalConstructor()->getMock(); $workflowDefinitionRepository = $this->createWorkflowDefinitionRepositoryMock(); $workflowDefinitionRepository->expects($this->once())->method('findByEntityClass')->with($entityClass)->will($this->returnValue(array($workflowDefinition))); $managerRegistry = $this->createManagerRegistryMock($workflowDefinitionRepository); $workflowAssembler = $this->createWorkflowAssemblerMock($workflowDefinition, $workflow); $workflowRegistry = new WorkflowRegistry($managerRegistry, $workflowAssembler); $expectedWorkflows = array($workflowName => $workflow); $actualWorkflows = $workflowRegistry->getWorkflowsByEntityClass($entityClass); $this->assertEquals($expectedWorkflows, $actualWorkflows); }