Last active
December 11, 2015 09:06
-
-
Save ignat-s/36d844ac0cbb063edfb7 to your computer and use it in GitHub Desktop.
Example how to make transition of workflow using processes in Oro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
definitions: | |
order_create: | |
label: 'Trigger "final_step_transition" transition for new FooBar entity if FooBar->isSubscribed() === true' | |
entity: ACME\Bundle\DemoBundle\Entity\FooBar | |
actions_configuration: | |
- @transit_workflow: | |
conditions: | |
@true: $subscribed | |
parameters: [$.data, 'final_step_transition'] | |
triggers: | |
order_create: | |
- | |
event: create | |
priority: 5 | |
queued: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
acme_demo.action.transit_workflow: | |
class: ACME\Bundle\DemoBundle\Model\Action\TransitWorkflow | |
arguments: | |
- @oro_workflow.context_accessor | |
- @oro_workflow.manager | |
tags: | |
- { name: oro_workflow.action, alias: transit_workflow } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ACME\Bundle\DemoBundle\Model\Action; | |
use Oro\Bundle\WorkflowBundle\Exception\ActionException; | |
use Oro\Bundle\WorkflowBundle\Exception\InvalidParameterException; | |
use Oro\Bundle\WorkflowBundle\Model\Action\AbstractAction; | |
use Oro\Bundle\WorkflowBundle\Model\ContextAccessor; | |
use Oro\Bundle\WorkflowBundle\Model\WorkflowManager; | |
/** | |
* Performs workflow transition | |
*/ | |
class TransitWorkflow extends AbstractAction | |
{ | |
/** | |
* @var WorkflowManager | |
*/ | |
protected $workflowManager; | |
/** | |
* @var string | |
*/ | |
protected $entity; | |
/** | |
* @var string | |
*/ | |
protected $transition; | |
/** | |
* @param ContextAccessor $contextAccessor | |
* @param WorkflowManager $workflowManager | |
*/ | |
public function __construct(ContextAccessor $contextAccessor, WorkflowManager $workflowManager) | |
{ | |
parent::__construct($contextAccessor); | |
$this->workflowManager = $workflowManager; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function executeAction($context) | |
{ | |
$entity = $this->contextAccessor->getValue($context, $this->entity); | |
$transition = $this->contextAccessor->getValue($context, $this->transition); | |
$workflowItem = $this->workflowManager->getWorkflowItemByEntity($entity); | |
if (!$workflowItem) { | |
throw new ActionException('Cannot transit workflow, entity doesn\'t have workflow.'); | |
} | |
$this->workflowManager->transit($workflowItem, $transition); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function initialize(array $options) | |
{ | |
if (isset($options['entity'])) { | |
$this->entity = $options['entity']; | |
} elseif (isset($options[0])) { | |
$this->entity = $options[0]; | |
} else { | |
throw new InvalidParameterException('Option "entity" is required.'); | |
} | |
if (isset($options['transition'])) { | |
$this->transition = $options['transition']; | |
} elseif (isset($options[1])) { | |
$this->transition = $options[1]; | |
} else { | |
throw new InvalidParameterException('Option "transition" is required.'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment