diff --git a/mailpoet/lib/Automation/Engine/Builder/DeleteWorkflowController.php b/mailpoet/lib/Automation/Engine/Builder/DeleteWorkflowController.php new file mode 100644 index 0000000000..3b26ce5d3c --- /dev/null +++ b/mailpoet/lib/Automation/Engine/Builder/DeleteWorkflowController.php @@ -0,0 +1,27 @@ +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; + } +} diff --git a/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsDeleteEndpoint.php b/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsDeleteEndpoint.php index 4fc42a52c2..dd80cbcab1 100644 --- a/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsDeleteEndpoint.php +++ b/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsDeleteEndpoint.php @@ -5,32 +5,22 @@ namespace MailPoet\Automation\Engine\Endpoints\Workflows; use MailPoet\API\REST\Request; use MailPoet\API\REST\Response; use MailPoet\Automation\Engine\API\Endpoint; -use MailPoet\Automation\Engine\Data\Workflow; -use MailPoet\Automation\Engine\Exceptions\InvalidStateException; -use MailPoet\Automation\Engine\Storage\WorkflowStorage; +use MailPoet\Automation\Engine\Builder\DeleteWorkflowController; use MailPoet\Validator\Builder; class WorkflowsDeleteEndpoint extends Endpoint { - /** @var WorkflowStorage */ - private $workflowStorage; + /** @var DeleteWorkflowController */ + private $deleteController; public function __construct( - WorkflowStorage $workflowStorage + DeleteWorkflowController $deleteController ) { - $this->workflowStorage = $workflowStorage; + $this->deleteController = $deleteController; } public function handle(Request $request): Response { - $workflowId = $request->getParam('id'); - if (!is_int($workflowId)) { - throw InvalidStateException::create(); - } - $existingWorkflow = $this->workflowStorage->getWorkflow($workflowId); - if (!$existingWorkflow instanceof Workflow) { - throw InvalidStateException::create(); - } - $this->workflowStorage->deleteWorkflow($existingWorkflow); - + $workflowId = intval($request->getParam('id')); + $this->deleteController->deleteWorkflow($workflowId); return new Response(null); } diff --git a/mailpoet/lib/DI/ContainerConfigurator.php b/mailpoet/lib/DI/ContainerConfigurator.php index 504a25ca8b..c244549304 100644 --- a/mailpoet/lib/DI/ContainerConfigurator.php +++ b/mailpoet/lib/DI/ContainerConfigurator.php @@ -112,6 +112,7 @@ class ContainerConfigurator implements IContainerConfigurator { // Automation $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\DeleteWorkflowController::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\UpdateWorkflowController::class)->setPublic(true);