Add workflows PUT endpoint for workflow updating

[MAILPOET-4454]
This commit is contained in:
Jan Jakes
2022-06-29 15:50:16 +02:00
committed by Jan Jakeš
parent bb90ae97d0
commit d6633efef2
3 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,57 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
use DateTimeImmutable;
use MailPoet\Automation\Engine\API\Endpoint;
use MailPoet\Automation\Engine\API\Request;
use MailPoet\Automation\Engine\API\Response;
use MailPoet\Automation\Engine\Builder\UpdateWorkflowController;
use MailPoet\Automation\Engine\Workflows\Step;
use MailPoet\Automation\Engine\Workflows\Workflow;
use MailPoet\Validator\Builder;
use stdClass;
class WorkflowsPutEndpoint extends Endpoint {
/** @var UpdateWorkflowController */
private $updateController;
public function __construct(
UpdateWorkflowController $updateController
) {
$this->updateController = $updateController;
}
public function handle(Request $request): Response {
$data = $request->getParams();
$workflow = $this->updateController->updateWorkflow(intval($request->getParam('id')), $data);
return new Response($this->buildWorkflow($workflow));
}
public static function getRequestSchema(): array {
return [
'id' => Builder::integer()->required(),
'name' => Builder::string(),
'status' => Builder::string(),
];
}
private function buildWorkflow(Workflow $workflow): array {
return [
'id' => $workflow->getId(),
'name' => $workflow->getName(),
'status' => $workflow->getStatus(),
'created_at' => $workflow->getCreatedAt()->format(DateTimeImmutable::W3C),
'updated_at' => $workflow->getUpdatedAt()->format(DateTimeImmutable::W3C),
'steps' => array_map(function (Step $step) {
return [
'id' => $step->getId(),
'type' => $step->getType(),
'key' => $step->getKey(),
'next_step_id' => $step->getNextStepId(),
'args' => $step->getArgs() ?: new stdClass(),
];
}, $workflow->getSteps()),
];
}
}

View File

@ -9,6 +9,7 @@ use MailPoet\Automation\Engine\Endpoints\System\DatabaseDeleteEndpoint;
use MailPoet\Automation\Engine\Endpoints\System\DatabasePostEndpoint; use MailPoet\Automation\Engine\Endpoints\System\DatabasePostEndpoint;
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsCreateFromTemplateEndpoint; use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsCreateFromTemplateEndpoint;
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsGetEndpoint; use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsGetEndpoint;
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsPutEndpoint;
use MailPoet\Automation\Engine\Storage\WorkflowStorage; use MailPoet\Automation\Engine\Storage\WorkflowStorage;
use MailPoet\Automation\Integrations\Core\CoreIntegration; use MailPoet\Automation\Integrations\Core\CoreIntegration;
@ -70,6 +71,7 @@ class Engine {
private function registerApiRoutes(): void { private function registerApiRoutes(): void {
$this->wordPress->addAction(Hooks::API_INITIALIZE, function (API $api) { $this->wordPress->addAction(Hooks::API_INITIALIZE, function (API $api) {
$api->registerGetRoute('workflows', WorkflowsGetEndpoint::class); $api->registerGetRoute('workflows', WorkflowsGetEndpoint::class);
$api->registerPutRoute('workflows/(?P<id>\d+)', WorkflowsPutEndpoint::class);
$api->registerPostRoute('workflows/create-from-template', WorkflowsCreateFromTemplateEndpoint::class); $api->registerPostRoute('workflows/create-from-template', WorkflowsCreateFromTemplateEndpoint::class);
$api->registerPostRoute('system/database', DatabasePostEndpoint::class); $api->registerPostRoute('system/database', DatabasePostEndpoint::class);
$api->registerDeleteRoute('system/database', DatabaseDeleteEndpoint::class); $api->registerDeleteRoute('system/database', DatabaseDeleteEndpoint::class);

View File

@ -123,6 +123,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\Automation\Engine\WordPress::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\WordPress::class)->setPublic(true);
// Automation - API endpoints // Automation - API endpoints
$container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsGetEndpoint::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsGetEndpoint::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsPutEndpoint::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsCreateFromTemplateEndpoint::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsCreateFromTemplateEndpoint::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\Endpoints\System\DatabasePostEndpoint::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Endpoints\System\DatabasePostEndpoint::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\Endpoints\System\DatabaseDeleteEndpoint::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Endpoints\System\DatabaseDeleteEndpoint::class)->setPublic(true);