Programming Language PHP
Namespace Oro\Bundle\WorkflowBundle\Entity
Class WorkflowItem
Total Examples 9
9 code examples of PHP Oro\Bundle\WorkflowBundle\Entity\WorkflowItem extracted from open source projects
/**
* @throws WorkflowException
*/
public function updateEntityRestrictions(WorkflowItem $workflowItem): WorkflowItem
{
$definition = $workflowItem->getDefinition();
$currentStepName = $workflowItem->getCurrentStep()->getName();
$restrictionIdentities = [];
foreach ($definition->getRestrictions() as $restriction) {
if ($restriction->getStep() && $restriction->getStep()->getName() === $currentStepName) {
$attributeName = $restriction->getAttribute();
$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));
}
$restrictionIdentity = new WorkflowRestrictionIdentity();
$restrictionIdentity->setRestriction($restriction)
->setEntityId($this->doctrineHelper->getSingleEntityIdentifier($entity));
$restrictionIdentities[] = $restrictionIdentity;
}
}
$workflowItem->setRestrictionIdentities($restrictionIdentities);
return $workflowItem;
}
/**
* Transits a workflow item without checking for preconditions and conditions.
*
* @param string|Transition $transition
*
* @throws InvalidTransitionException
* @throws WorkflowException
*/
public function transitUnconditionally(WorkflowItem $workflowItem, $transition)
{
$transition = $this->transitionManager->extractTransition($transition);
$this->checkTransitionValid($transition, $workflowItem, true);
$transitionRecord = $this->createTransitionRecord($workflowItem, $transition);
$transition->transitUnconditionally($workflowItem);
$workflowItem->addTransitionRecord($transitionRecord);
$this->aclManager->updateAclIdentities($workflowItem);
$this->restrictionManager->updateEntityRestrictions($workflowItem);
}
/**
* Transit workflow item.
*
* @param string|Transition $transition
*
* @throws ForbiddenTransitionException
* @throws InvalidTransitionException
* @throws WorkflowException
*/
public function transit(WorkflowItem $workflowItem, $transition, Collection $errors = null)
{
$transition = $this->transitionManager->extractTransition($transition);
$this->checkTransitionValid($transition, $workflowItem, true);
$transitionRecord = $this->createTransitionRecord($workflowItem, $transition);
$transition->transit($workflowItem, $errors);
$workflowItem->addTransitionRecord($transitionRecord);
$this->aclManager->updateAclIdentities($workflowItem);
$this->restrictionManager->updateEntityRestrictions($workflowItem);
}
/**
* Makes transition without checking for preconditions and conditions.
*/
public function transitUnconditionally(WorkflowItem $workflowItem): void
{
$stepTo = $this->getStepTo();
$workflowItem->setCurrentStep($workflowItem->getDefinition()->getStepByName($stepTo->getName()));
if ($this->action) {
$this->action->execute($workflowItem);
}
}
/**
* Deserialize data of WorkflowItem
*/
private function deserialize(WorkflowItem $workflowItem): void
{
// Pass serializer into $workflowItem to make lazy loading of workflow item data.
$workflowItem->setSerializer($this->getSerializer(), $this->format);
// Set related entity
$relatedEntity = $this->doctrineHelper->getEntityReference(
$workflowItem->getDefinition()->getRelatedEntity(),
$workflowItem->getEntityId()
);
$workflowItem->setEntity($relatedEntity);
}
/**
* Deserialize data of WorkflowItem
*/
private function deserialize(WorkflowItem $workflowItem): void
{
// Pass serializer into $workflowItem to make lazy loading of workflow item data.
$workflowItem->setSerializer($this->getSerializer(), $this->format);
// Set related entity
$relatedEntity = $this->doctrineHelper->getEntityReference(
$workflowItem->getDefinition()->getRelatedEntity(),
$workflowItem->getEntityId()
);
$workflowItem->setEntity($relatedEntity);
}
/**
* Serialize data of WorkflowItem
*/
private function serialize(WorkflowItem $workflowItem): void
{
$serializer = $this->getSerializer();
$serializer->setWorkflowName($workflowItem->getWorkflowName());
$serializedData = $serializer->serialize(
$this->getWorkflowData($workflowItem),
$this->format
);
$workflowItem->setSerializedData($serializedData);
$workflowItem->getData()->setModified(false);
}
/**
* @throws WorkflowException
*/
public function updateAclIdentities(WorkflowItem $workflowItem): WorkflowItem
{
$workflow = $this->workflowRegistry->getWorkflow($workflowItem->getWorkflowName());
$definition = $workflowItem->getDefinition();
$currentStepName = $workflowItem->getCurrentStep()->getName();
$aclIdentities = [];
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;
}
protected function transit(WorkflowManager $workflowManager, WorkflowItem $workflowItem, string $transition, array $data)
{
foreach ($data as $key => $value) {
$workflowItem->getData()->set($key, $value);
}
$workflow = $workflowManager->getWorkflow($workflowItem);
$workflow->transit($workflowItem, $transition);
$workflowItem->setUpdated();
}