Add controller to create workflows via REST API
[MAILPOET-4136]
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
Reference in New Issue
Block a user