Add workflows PUT endpoint for workflow updating
[MAILPOET-4454]
This commit is contained in:
@ -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()),
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user