Skip to content

Commit

Permalink
- Fixed issue with WorkflowStartListener where only the last order id…
Browse files Browse the repository at this point in the history
… (which came in batches) was getting a workflow start
  • Loading branch information
24198 committed Jul 24, 2020
1 parent 1f318b4 commit 87716bd
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Oro\Bundle\WorkflowBundle\Model\Workflow;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\WorkflowBundle\Model\WorkflowManager;
use Oro\Bundle\WorkflowBundle\Model\WorkflowStartArguments;

use Marello\Bundle\OrderBundle\Entity\Order;
use Marello\Bundle\OrderBundle\Model\WorkflowNameProviderInterface;
Expand All @@ -19,12 +20,12 @@ class OrderWorkflowStartListener
/** @var WorkflowManager $workflowManager */
private $workflowManager;

/** @var string $orderId*/
private $orderId;

/** @var DoctrineHelper $doctrineHelper */
private $doctrineHelper;

/** @var array $entitiesScheduledForWorkflowStart */
protected $entitiesScheduledForWorkflowStart = [];

/**
* @param WorkflowManager $workflowManager
*/
Expand All @@ -36,31 +37,30 @@ public function __construct(WorkflowManager $workflowManager)
/**
* @param LifecycleEventArgs $args
*/
public function postPersist(LifecycleEventArgs $args): void
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof Order) {
$this->orderId = $entity->getId();
if ($workflow = $this->getApplicableWorkflow($entity)) {
$this->entitiesScheduledForWorkflowStart[] = new WorkflowStartArguments(
$workflow->getName(),
$entity,
[],
self::TRANSIT_TO_STEP
);
}
}
}

/**
* @param PostFlushEventArgs $args
* @throws \Oro\Bundle\WorkflowBundle\Exception\WorkflowRecordGroupException
*/
public function postFlush(PostFlushEventArgs $args): void
public function postFlush(PostFlushEventArgs $args)
{
if ($this->orderId) {
$entityManager = $args->getEntityManager();
/** @var Order $entity */
$entity = $this->doctrineHelper
->getEntityManagerForClass(Order::class)
->getRepository(Order::class)
->find($this->orderId);
if ($entity && $workflow = $this->getApplicableWorkflow($entity)) {
$this->orderId = null;
$this->workflowManager->startWorkflow($workflow->getName(), $entity, self::TRANSIT_TO_STEP);
}
if (!empty($this->entitiesScheduledForWorkflowStart)) {
$massStartData = $this->entitiesScheduledForWorkflowStart;
unset($this->entitiesScheduledForWorkflowStart);
$this->workflowManager->massStartWorkflow($massStartData);
}
}

Expand Down Expand Up @@ -102,6 +102,7 @@ protected function getDefaultWorkflowNames(): array
}

/**
* @deprecated
* @param DoctrineHelper $doctrineHelper
*/
public function setDoctrineHelper(DoctrineHelper $doctrineHelper)
Expand Down

0 comments on commit 87716bd

Please sign in to comment.