public function create($connection) { if (!array_key_exists('type', $connection)) { return $this->apiProblem(422, "No type given for the connection"); } if (!array_key_exists('message_handler', $connection)) { return $this->apiProblem(422, "No message_handler given for the connection"); } if (!array_key_exists('workflow_id', $connection)) { return $this->apiProblem(422, "No workflow_id given for the connection"); } if ($connection['type'] == "start_connection") { if (!array_key_exists('start_message', $connection)) { return $this->apiProblem(422, "No start_message given for the connection"); } $this->commandBus->dispatch(ScheduleFirstTasksForWorkflow::withData($connection['workflow_id'], $connection['start_message'], $connection['message_handler'])); return $this->accepted(); } elseif ($connection['type'] == "source_target_connection") { if (!array_key_exists('previous_task', $connection)) { return $this->apiProblem(422, "No previous_task given for the connection"); } $this->commandBus->dispatch(ScheduleNextTasksForWorkflow::withData($connection['workflow_id'], $connection['previous_task'], $connection['message_handler'])); return $this->accepted(); } else { return $this->apiProblem(422, "Unknown connection type"); } }
public function handle(ScheduleNextTasksForWorkflow $command) { $workflow = $this->workflowCollection->get($command->workflowId()); if (is_null($workflow)) { throw WorkflowNotFound::withId($command->workflowId()); } $previousTask = $this->taskCollection->get($command->previousTaskId()); if (is_null($previousTask)) { throw TaskNotFound::withId($command->previousTaskId()); } $previousMessageHandler = $this->messageHandlerCollection->get($previousTask->messageHandlerId()); if (is_null($previousMessageHandler)) { throw MessageHandlerNotFound::withId($previousTask->messageHandlerId()); } $nextMessageHandler = $this->messageHandlerCollection->get($command->nextMessageHandlerId()); if (is_null($nextMessageHandler)) { throw MessageHandlerNotFound::withId($command->nextMessageHandlerId()); } $nextTasks = $workflow->determineNextTasks($previousTask, $previousMessageHandler, $nextMessageHandler); foreach ($nextTasks as $nextTask) { $this->taskCollection->add($nextTask); } }