If workflow message handler receives a collect-data message it forwards the message to this
method and uses the returned ProcessingMessage as response

@param WorkflowMessage $workflowMessage
@return ProcessingMessage
963261
protected function handleCollectData(WorkflowMessage $workflowMessage)
    {
        if ($workflowMessage->payload()->getTypeClass() === 'Prooph\ProcessingExample\Type\SourceUser') {
            $userData = include __DIR__ . '/../../data/user-source-data.php';

            if (! $userData) {
                return LogMessage::logErrorMsg(
                    "Could not read user data from examples/data/user-source-data.php. Please check the permissions",
                    $workflowMessage
                );
            }

            $sourceUser = SourceUser::fromNativeValue($userData);

            return $workflowMessage->answerWith($sourceUser);
        } else {
            return LogMessage::logErrorMsg(
                sprintf(
                    '%s: Unknown type %s received', __CLASS__, $workflowMessage->payload()->getTypeClass()),
                $workflowMessage
            );
        }
    }
507040
public function getMetadata()
    {
        if (null === $this->metadata) {
            $errMsg = 'metadata not found';
            throw new Exception\WorkflowDispatchEventException($errMsg);
        }

        return $this->metadata;
    }
282735
public function load(ObjectManager $manager)
    {
        if (true == $this->hasReference('workflow-function-validator-production')) {
            $workflowRight = new WorkflowRight();
            $workflowRight->setUserId($this->getReference('user-admin')->getId());

            $authorization = new Authorization();
            $authorization->setReferenceId(WorkflowRightInterface::NODE);
            $authorization->addWorkflowFunction($this->getReference('workflow-function-validator-production'));

            $workflowRight->addAuthorization($authorization);

            $manager->persist($workflowRight);
            $manager->flush();
        }
    }
271511
public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $resource = new WorkflowRelease();

        $resource->setWorkflowFinder($serviceLocator->getServiceLocator()->get(\Prooph\Link\ProcessManager\Projection\Workflow\WorkflowFinder::class));

        return $resource;
    }
1157630
static function continueWorkflow( $workflowProcessID )
    {
        $operationResult =  null;
        $theProcess      = eZWorkflowProcess::fetch( $workflowProcessID );
        if ( $theProcess != null )
        {
            //restore memento and run it
            $bodyMemento = eZOperationMemento::fetchChild( $theProcess->attribute( 'memento_key' ) );
            if ( $bodyMemento === null )
            {
                eZDebug::writeError( $bodyMemento, "Empty body memento in workflow.php" );
                return $operationResult;
            }
            $bodyMementoData =  $bodyMemento->data();
            $mainMemento     = $bodyMemento->attribute( 'main_memento' );
            if ( !$mainMemento )
            {
                return $operationResult;
            }

            $mementoData = $bodyMemento->data();
            $mainMementoData = $mainMemento->data();
            $mementoData['main_memento'] = $mainMemento;
            $mementoData['skip_trigger'] = false;
            $mementoData['memento_key'] = $theProcess->attribute( 'memento_key' );
            $bodyMemento->remove();

            $operationParameters = array();
            if ( isset( $mementoData['parameters'] ) )
                $operationParameters = $mementoData['parameters'];

            $operationResult = eZOperationHandler::execute( $mementoData['module_name'],
                                                            $mementoData['operation_name'],
                                                            $operationParameters,
                                                            $mementoData );
        }

        return $operationResult;
    }
131437
public function runWorkflow(int $workflowId, array $convos): void
    {
        Assert::notEmpty(
            $convos,
            'Conversations cannot be empty'
        );

        $conversations = new RunWorkflowRequest($convos);

        $uri = sprintf(self::RUN_WORKFLOW_URL, $workflowId);

        foreach ($conversations->getBatch() as $batch) {
            $this->restClient->createResource($batch, $uri);
        }
    }
1217971
public function processQueueItem(Message $msg)
    {
        $fID = (int) $msg->body;
        if ($fID > 0) {
            $file = $this->entityManager->find(File::class, $fID);
            if ($file !== null) {
                $fileVersion = $file->getApprovedVersion();
                if ($fileVersion !== null) {
                    if ($fileVersion->getTypeObject()->supportsThumbnails()) {
                        $imageWidth = (int) $fileVersion->getAttribute('width');
                        $imageHeight = (int) $fileVersion->getAttribute('height');
                        foreach ($this->getThumbnailTypeVersions() as $thumbnailTypeVersion) {
                            if ($thumbnailTypeVersion->shouldExistFor($imageWidth, $imageHeight, $file)) {
                                $this->thumbnailPathResolver->getPath($fileVersion, $thumbnailTypeVersion);
                            }
                        }
                    }
                }
            }
        }
    }
1212178
private function readDocument(\DOMDocument $document, $workflowId = null)
    {
        $errorToExceptionContext = new ErrorToExceptionContext(E_WARNING, function () use ($document) {
            $document->schemaValidate(dirname(__DIR__).'/Resources/config/workflower/schema/BPMN20.xsd');
        });
        $errorToExceptionContext->invoke();

        $workflowBuilder = new WorkflowBuilder($workflowId);

        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'process') as $element) {
            /* @var $element \DOMElement */
            if ($element->hasAttribute('id')) {
                $workflowBuilder->setWorkflowId($element->getAttribute('id'));
            }

            if ($element->hasAttribute('name')) {
                $workflowBuilder->setWorkflowName($element->getAttribute('name'));
            }
        }

        $flowObjectRoles = array();
        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'lane') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $workflowBuilder->addRole(
                $element->getAttribute('id'),
                $element->hasAttribute('name') ? $element->getAttribute('name') : null
            );

            foreach ($element->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'flowNodeRef') as $childElement) {
                $flowObjectRoles[$childElement->nodeValue] = $element->getAttribute('id');
            }
        }

        if (count($flowObjectRoles) == 0) {
            $workflowBuilder->addRole(Workflow::DEFAULT_ROLE_ID);
        }

        $messages = array();
        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'message') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $messages[$element->getAttribute('id')] = $element->getAttribute('name');
        }

        $operations = array();
        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'operation') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $operations[$element->getAttribute('id')] = $element->getAttribute('name');
        }

        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'startEvent') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $workflowBuilder->addStartEvent(
                $element->getAttribute('id'),
                $this->provideRoleIdForFlowObject($flowObjectRoles, $element->getAttribute('id')),
                $element->hasAttribute('name') ? $element->getAttribute('name') : null,
                $element->hasAttribute('default') ? $element->getAttribute('default') : null
            );
        }

        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'task') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $workflowBuilder->addTask(
                $element->getAttribute('id'),
                $this->provideRoleIdForFlowObject($flowObjectRoles, $element->getAttribute('id')),
                $element->hasAttribute('name') ? $element->getAttribute('name') : null,
                $element->hasAttribute('default') ? $element->getAttribute('default') : null
            );
        }

        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'serviceTask') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $workflowBuilder->addServiceTask(
                $element->getAttribute('id'),
                $this->provideRoleIdForFlowObject($flowObjectRoles, $element->getAttribute('id')),
                $element->hasAttribute('operationRef') ? $operations[$element->getAttribute('operationRef')] : null,
                $element->hasAttribute('name') ? $element->getAttribute('name') : null,
                $element->hasAttribute('default') ? $element->getAttribute('default') : null
            );
        }

        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'sendTask') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $workflowBuilder->addSendTask(
                $element->getAttribute('id'),
                $this->provideRoleIdForFlowObject($flowObjectRoles, $element->getAttribute('id')),
                $element->hasAttribute('messageRef') ? $messages[$element->getAttribute('messageRef')] : null,
                $element->hasAttribute('operationRef') ? $operations[$element->getAttribute('operationRef')] : null,
                $element->hasAttribute('name') ? $element->getAttribute('name') : null,
                $element->hasAttribute('default') ? $element->getAttribute('default') : null
            );
        }

        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'exclusiveGateway') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $workflowBuilder->addExclusiveGateway(
                $element->getAttribute('id'),
                $this->provideRoleIdForFlowObject($flowObjectRoles, $element->getAttribute('id')),
                $element->hasAttribute('name') ? $element->getAttribute('name') : null,
                $element->hasAttribute('default') ? $element->getAttribute('default') : null
            );
        }

        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'endEvent') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $workflowBuilder->addEndEvent($element->getAttribute('id'), $this->provideRoleIdForFlowObject($flowObjectRoles, $element->getAttribute('id')), $element->hasAttribute('name') ? $element->getAttribute('name') : null);
        }

        foreach ($document->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'sequenceFlow') as $element) {
            if (!$element->hasAttribute('id')) {
                throw $this->createIdAttributeNotFoundException($element, $workflowId);
            }

            $condition = null;
            foreach ($element->getElementsByTagNameNs('http://www.omg.org/spec/BPMN/20100524/MODEL', 'conditionExpression') as $childElement) {
                $condition = $childElement->nodeValue;
                break;
            }

            $workflowBuilder->addSequenceFlow(
                $element->getAttribute('sourceRef'),
                $element->getAttribute('targetRef'),
                $element->getAttribute('id'),
                $element->hasAttribute('name') ? $element->getAttribute('name') : null,
                $condition === null ? null : $condition
            );
        }

        return $workflowBuilder->build();
    }
349420
public function getStep($stepName)
    {
        if (!$this->getSteps()->offsetExists($stepName)) {
            throw new WorkflowException('Step with name "'.$stepName.'" is not in "'.$this->workflowName.'" workflow');
        }

        return $this->getSteps()->offsetGet($stepName);
    }
1198675
public function startWorkflow(DataObject $object, $workflowID = null)
    {
        $existing = $this->getWorkflowFor($object);
        if ($existing) {
            throw new ExistingWorkflowException(_t(
                'WorkflowService.EXISTING_WORKFLOW_ERROR',
                "That object already has a workflow running"
            ));
        }

        $definition = null;
        if ($workflowID) {
            // Retrieve the workflow definition that has been triggered.

            $definition = $this->getDefinitionByID($object, $workflowID);
        }
        if (is_null($definition)) {
            // Fall back to the main workflow definition.

            $definition = $this->getDefinitionFor($object);
        }

        if ($definition) {
            $instance = new WorkflowInstance();
            $instance->beginWorkflow($definition, $object);
            $instance->execute();
        }
    }
377342
protected function processMessage($message) {
    // Check if the message was forward
    isset($message['forward_from']) ? $index = 'forward_from' : $index = 'from';

    $response = $this->prepareResponse($message[$index]);
    $this->sendMessage($response);
  }
1319880
public function call(Message $message) : Message
    {
        $aggregate = $message->getHeaders()->containsKey(CqrsMessagingModule::INTEGRATION_MESSAGING_CQRS_AGGREGATE_HEADER)
                            ? $message->getHeaders()->get(CqrsMessagingModule::INTEGRATION_MESSAGING_CQRS_AGGREGATE_HEADER)
                            : null;
        $methodInvoker = MethodInvoker::createWith(
            $aggregate ? $aggregate : $message->getHeaders()->get(CqrsMessagingModule::INTEGRATION_MESSAGING_CQRS_AGGREGATE_CLASS_NAME_HEADER),
            $message->getHeaders()->get(CqrsMessagingModule::INTEGRATION_MESSAGING_CQRS_AGGREGATE_METHOD_HEADER),
            $this->messageToParameterConverters
        );

        $resultMessage = MessageBuilder::fromMessage($message);
        try {
            $result = $methodInvoker->processMessage($message);

            if (!$aggregate) {
                $resultMessage = $resultMessage
                    ->setHeader(CqrsMessagingModule::INTEGRATION_MESSAGING_CQRS_AGGREGATE_HEADER, $result);
            }
        } catch (\Throwable $e) {
            throw MessageHandlingException::fromOtherException($e, $message);
        }

        if (!is_null($result)) {
            $resultMessage = $resultMessage
                ->setPayload($result);
        }

        return $resultMessage
                ->build();
    }
882514
public function proceedWorkflow(ProceedWorkflowCommand $workflow_command, StateMachineInterface $state_machine)
    {
        $this->guardCommandPreConditions($workflow_command);

        if ($workflow_command->getCurrentStateName() !== $this->getWorkflowState()) {
            throw new WorkflowStateMismatchError(
                sprintf(
                    'The AR\'s(%s) current state %s does not match the given command state %s.',
                    $this,
                    $this->getWorkflowState(),
                    $workflow_command->getCurrentStateName()
                )
            );
        }

        $workflow_subject = new WorkflowSubject($state_machine->getName(), $this);
        $previous_state = $this->getWorkflowState();
        $state_machine->execute($workflow_subject, $workflow_command->getEventName());

        $workflow_data = [
            'workflow_state' => $workflow_subject->getCurrentStateName(),
            'workflow_parameters' => array_merge(
                $workflow_subject->getWorkflowParameters(),
                [
                    'previous_state' => $previous_state,
                    'workflow_event' => $workflow_command->getEventName()
                ]
            )
        ];

        $proceeded_event = $this->processCommand($workflow_command, [ 'data' => $workflow_data ]);

        if (!$proceeded_event instanceof WorkflowProceededEvent) {
            throw new UnsupportedEventTypeError(
                sprintf(
                    'Corrupt event type detected. Events that reflect workflow transitions must descend from %s.',
                    WorkflowProceededEvent::CLASS
                )
            );
        }

        $this->applyEvent($proceeded_event);
    }
1353963
public function await(QueueClientInterface $queue, ReceiveParametersInterface $params = null)
    {
        $worker = $this;
        $this->await = true;
        $lastResult = [];

        $callback = function(QueueEvent $event) use($worker, $queue, &$lastResult) {

            $messages = $event->getMessages();

            foreach ($messages as $message) {
                $lastResult = $worker->process($message);

                if ($queue->canDeleteMessage()) {
                    $queue->delete($message);
                }
            }

            if ($worker->isAwaitingStopped()) {
                $event->stopAwait(true);
            }

            return !$worker->isAwaitingStopped();
        };


        $callbackHandler = $queue->getEventManager()->attach(QueueEvent::EVENT_RECEIVE, $callback);

        $queue->await($params);

        $queue->getEventManager()->detach($callbackHandler);


        return $lastResult;
    }
946969
public function getWorkflowProgressCurrentStatusNum(WorkflowProgress $wp)
    {
        $req = $wp->getWorkflowRequestObject();
        if (is_object($req)) {
            return $req->getWorkflowRequestStatusNum();
        }
    }
381218
public function receive(QueueClientInterface $queue, $maxMessages = 1, ReceiveParametersInterface $params = null)
    {
        $messages = $queue->receive($maxMessages, $params);
        $lastResult = [];

        foreach ($messages as $message) {
            if ($message instanceof MessageInterface) {

                $lastResult = $this->process($message);

                if ($queue->canDeleteMessage()) {
                    $queue->delete($message);
                }
            }
            // TODO: else?
        }

        return $lastResult;
    }
391781
public function createService(ServiceLocatorInterface $serviceLocator)
    {
        /** @var $env Environment */
        $env = $serviceLocator->getServiceLocator()->get('processing_environment');

        return new Message(
            $env->getWorkflowEngine(),
            $serviceLocator->getServiceLocator()->get('prooph.link.processor_proxy.message_logger')
        );
    }