From c97476e8c32e10fc489b58b495b74fbcb5cdea13 Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Mon, 7 Mar 2022 11:35:39 +0100 Subject: [PATCH] Add controller to create workflows via REST API [MAILPOET-4136] --- .../API/Endpoints/WorkflowsEndpoint.php | 17 +++++++++ .../Builder/CreateWorkflowController.php | 36 +++++++++++++++++++ mailpoet/lib/DI/ContainerConfigurator.php | 1 + 3 files changed, 54 insertions(+) create mode 100644 mailpoet/lib/Automation/Engine/Builder/CreateWorkflowController.php diff --git a/mailpoet/lib/Automation/Engine/API/Endpoints/WorkflowsEndpoint.php b/mailpoet/lib/Automation/Engine/API/Endpoints/WorkflowsEndpoint.php index 0a91fdf4e8..bd9f183e96 100644 --- a/mailpoet/lib/Automation/Engine/API/Endpoints/WorkflowsEndpoint.php +++ b/mailpoet/lib/Automation/Engine/API/Endpoints/WorkflowsEndpoint.php @@ -5,9 +5,26 @@ namespace MailPoet\Automation\Engine\API\Endpoints; use MailPoet\Automation\Engine\API\Endpoint; use MailPoet\Automation\Engine\API\Request; use MailPoet\Automation\Engine\API\Response; +use MailPoet\Automation\Engine\Builder\CreateWorkflowController; class WorkflowsEndpoint extends Endpoint { + /** @var CreateWorkflowController */ + private $createController; + + public function __construct( + CreateWorkflowController $createController + ) { + $this->createController = $createController; + } + public function get(Request $request): Response { return new Response(['message' => 'Hello world.']); } + + public function post(Request $request): Response { + // TODO: validation + $body = $request->getBody(); + $this->createController->createWorkflow($body); + return new Response(); + } } diff --git a/mailpoet/lib/Automation/Engine/Builder/CreateWorkflowController.php b/mailpoet/lib/Automation/Engine/Builder/CreateWorkflowController.php new file mode 100644 index 0000000000..5dcd7af584 --- /dev/null +++ b/mailpoet/lib/Automation/Engine/Builder/CreateWorkflowController.php @@ -0,0 +1,36 @@ +storage = $storage; + } + + public function createWorkflow(array $data): Workflow { + // TODO: data & workflow validation (trigger existence, graph consistency, etc.) + $steps = []; + foreach ($data['steps'] as $step) { + $steps[] = new Step( + $step['id'], + $step['type'], + $step['key'], + $step['next_step_id'] ?? null, + $step['args'] ?? [] + ); + } + $workflow = new Workflow($data['name'], $steps); + + $this->storage->createWorkflow($workflow); + return $workflow; + } +} diff --git a/mailpoet/lib/DI/ContainerConfigurator.php b/mailpoet/lib/DI/ContainerConfigurator.php index 1958a72958..eca688600f 100644 --- a/mailpoet/lib/DI/ContainerConfigurator.php +++ b/mailpoet/lib/DI/ContainerConfigurator.php @@ -107,6 +107,7 @@ class ContainerConfigurator implements IContainerConfigurator { ->setArgument('$container', new Reference(ContainerWrapper::class)); $container->autowire(\MailPoet\Automation\Engine\API\Endpoints\SystemDatabaseEndpoint::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\API\Endpoints\WorkflowsEndpoint::class)->setPublic(true); + $container->autowire(\MailPoet\Automation\Engine\Builder\CreateWorkflowController::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Engine::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Migrations\Migrator::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Storage\WorkflowStorage::class)->setPublic(true);