From 902eeccea15c84c803e00377f41e7edb176c8a53 Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 2 Nov 2022 15:28:22 +0200 Subject: [PATCH] Block updating workflows with active runs in the backend [MAILPOET-4744] --- .../Builder/UpdateWorkflowController.php | 35 +++++++++++++++++++ mailpoet/lib/Automation/Engine/Exceptions.php | 11 ++++++ 2 files changed, 46 insertions(+) diff --git a/mailpoet/lib/Automation/Engine/Builder/UpdateWorkflowController.php b/mailpoet/lib/Automation/Engine/Builder/UpdateWorkflowController.php index 23877d1695..82b208089a 100644 --- a/mailpoet/lib/Automation/Engine/Builder/UpdateWorkflowController.php +++ b/mailpoet/lib/Automation/Engine/Builder/UpdateWorkflowController.php @@ -7,6 +7,7 @@ use MailPoet\Automation\Engine\Data\Workflow; use MailPoet\Automation\Engine\Exceptions; use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException; use MailPoet\Automation\Engine\Hooks; +use MailPoet\Automation\Engine\Storage\WorkflowStatisticsStorage; use MailPoet\Automation\Engine\Storage\WorkflowStorage; use MailPoet\Automation\Engine\Validation\WorkflowValidator; @@ -17,6 +18,9 @@ class UpdateWorkflowController { /** @var WorkflowStorage */ private $storage; + /** @var WorkflowStatisticsStorage */ + private $statisticsStorage; + /** @var WorkflowValidator */ private $workflowValidator; @@ -26,11 +30,13 @@ class UpdateWorkflowController { public function __construct( Hooks $hooks, WorkflowStorage $storage, + WorkflowStatisticsStorage $statisticsStorage, WorkflowValidator $workflowValidator, UpdateStepsController $updateStepsController ) { $this->hooks = $hooks; $this->storage = $storage; + $this->statisticsStorage = $statisticsStorage; $this->workflowValidator = $workflowValidator; $this->updateStepsController = $updateStepsController; } @@ -40,6 +46,7 @@ class UpdateWorkflowController { if (!$workflow) { throw Exceptions::workflowNotFound($id); } + $this->validateIfWorkflowCanBeUpdated($workflow, $data); if (array_key_exists('name', $data)) { $workflow->setName($data['name']); @@ -71,6 +78,34 @@ class UpdateWorkflowController { return $workflow; } + /** + * This is a temporary validation, see MAILPOET-4744 + */ + private function validateIfWorkflowCanBeUpdated(Workflow $workflow, array $data): void { + + if ( + !in_array( + $workflow->getStatus(), + [ + Workflow::STATUS_ACTIVE, + Workflow::STATUS_DEACTIVATING, + ], + true + ) + ) { + return; + } + + $statistics = $this->statisticsStorage->getWorkflowStats($workflow->getId()); + if ($statistics->getInProgress() === 0) { + return; + } + + if (!isset($data['status']) || $data['status'] === $workflow->getStatus()) { + throw Exceptions::workflowHasActiveRuns($workflow->getId()); + } + } + private function checkWorkflowStatus(string $status): void { if (!in_array($status, Workflow::STATUS_ALL, true)) { // translators: %s is the status. diff --git a/mailpoet/lib/Automation/Engine/Exceptions.php b/mailpoet/lib/Automation/Engine/Exceptions.php index 7478cfb52e..be5ea3613f 100644 --- a/mailpoet/lib/Automation/Engine/Exceptions.php +++ b/mailpoet/lib/Automation/Engine/Exceptions.php @@ -32,6 +32,7 @@ class Exceptions { private const MISSING_REQUIRED_SUBJECTS = 'mailpoet_automation_missing_required_subjects'; private const WORKFLOW_NOT_TRASHED = 'mailpoet_automation_workflow_not_trashed'; private const WORKFLOW_TEMPLATE_NOT_FOUND = 'mailpoet_automation_workflow_template_not_found'; + private const WORKFLOW_HAS_ACTIVE_RUNS = 'mailpoet_automation_workflow_has_active_runs'; public function __construct() { throw new InvalidStateException( @@ -228,4 +229,14 @@ class Exceptions { // translators: %d is the ID of the automation template. ->withMessage(sprintf(__("Automation template with ID '%d' not found.", 'mailpoet'), $id)); } + + /** + * This is a temporary block, see MAILPOET-4744 + */ + public static function workflowHasActiveRuns(int $id): InvalidStateException { + return InvalidStateException::create() + ->withErrorCode(self::WORKFLOW_HAS_ACTIVE_RUNS) + // translators: %d is the ID of the workflow. + ->withMessage(sprintf(__("Can not update workflow with ID '%d' because users are currently active.", 'mailpoet'), $id)); + } }