Add controller to create workflows via REST API

[MAILPOET-4136]
This commit is contained in:
Jan Jakes
2022-03-07 11:35:39 +01:00
committed by Veljko V
parent 41cb2c4034
commit c97476e8c3
3 changed files with 54 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,36 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Builder;
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
use MailPoet\Automation\Engine\Workflows\Step;
use MailPoet\Automation\Engine\Workflows\Workflow;
class CreateWorkflowController {
/** @var WorkflowStorage */
private $storage;
public function __construct(
WorkflowStorage $storage
) {
$this->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;
}
}

View File

@ -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);