Extract workflow delete logic to a controller

[MAILPOET-4540]
This commit is contained in:
Jan Jakes
2022-10-13 15:06:47 +02:00
committed by David Remer
parent 89c43a5cb9
commit b01d050f1d
3 changed files with 35 additions and 17 deletions

View File

@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Builder;
use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
class DeleteWorkflowController {
/** @var WorkflowStorage */
private $workflowStorage;
public function __construct(
WorkflowStorage $workflowStorage
) {
$this->workflowStorage = $workflowStorage;
}
public function deleteWorkflow(int $id): Workflow {
$workflow = $this->workflowStorage->getWorkflow($id);
if (!$workflow) {
throw Exceptions::workflowNotFound($id);
}
$this->workflowStorage->deleteWorkflow($workflow);
return $workflow;
}
}

View File

@@ -5,32 +5,22 @@ namespace MailPoet\Automation\Engine\Endpoints\Workflows;
use MailPoet\API\REST\Request; use MailPoet\API\REST\Request;
use MailPoet\API\REST\Response; use MailPoet\API\REST\Response;
use MailPoet\Automation\Engine\API\Endpoint; use MailPoet\Automation\Engine\API\Endpoint;
use MailPoet\Automation\Engine\Data\Workflow; use MailPoet\Automation\Engine\Builder\DeleteWorkflowController;
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
use MailPoet\Validator\Builder; use MailPoet\Validator\Builder;
class WorkflowsDeleteEndpoint extends Endpoint { class WorkflowsDeleteEndpoint extends Endpoint {
/** @var WorkflowStorage */ /** @var DeleteWorkflowController */
private $workflowStorage; private $deleteController;
public function __construct( public function __construct(
WorkflowStorage $workflowStorage DeleteWorkflowController $deleteController
) { ) {
$this->workflowStorage = $workflowStorage; $this->deleteController = $deleteController;
} }
public function handle(Request $request): Response { public function handle(Request $request): Response {
$workflowId = $request->getParam('id'); $workflowId = intval($request->getParam('id'));
if (!is_int($workflowId)) { $this->deleteController->deleteWorkflow($workflowId);
throw InvalidStateException::create();
}
$existingWorkflow = $this->workflowStorage->getWorkflow($workflowId);
if (!$existingWorkflow instanceof Workflow) {
throw InvalidStateException::create();
}
$this->workflowStorage->deleteWorkflow($existingWorkflow);
return new Response(null); return new Response(null);
} }

View File

@@ -112,6 +112,7 @@ class ContainerConfigurator implements IContainerConfigurator {
// Automation // Automation
$container->autowire(\MailPoet\Automation\Engine\API\API::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\API\API::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\Builder\CreateWorkflowFromTemplateController::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Builder\CreateWorkflowFromTemplateController::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\Builder\DeleteWorkflowController::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\Builder\DuplicateWorkflowController::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Builder\DuplicateWorkflowController::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\Builder\UpdateStepsController::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Builder\UpdateStepsController::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\Builder\UpdateWorkflowController::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Builder\UpdateWorkflowController::class)->setPublic(true);