Block updating workflows with active runs in the backend

[MAILPOET-4744]
This commit is contained in:
David Remer
2022-11-02 15:28:22 +02:00
committed by John Oleksowicz
parent c8cf00a151
commit 902eeccea1
2 changed files with 46 additions and 0 deletions

View File

@ -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.

View File

@ -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));
}
}