Block updating workflows with active runs in the backend
[MAILPOET-4744]
This commit is contained in:
committed by
John Oleksowicz
parent
c8cf00a151
commit
902eeccea1
@ -7,6 +7,7 @@ use MailPoet\Automation\Engine\Data\Workflow;
|
|||||||
use MailPoet\Automation\Engine\Exceptions;
|
use MailPoet\Automation\Engine\Exceptions;
|
||||||
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
||||||
use MailPoet\Automation\Engine\Hooks;
|
use MailPoet\Automation\Engine\Hooks;
|
||||||
|
use MailPoet\Automation\Engine\Storage\WorkflowStatisticsStorage;
|
||||||
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
||||||
use MailPoet\Automation\Engine\Validation\WorkflowValidator;
|
use MailPoet\Automation\Engine\Validation\WorkflowValidator;
|
||||||
|
|
||||||
@ -17,6 +18,9 @@ class UpdateWorkflowController {
|
|||||||
/** @var WorkflowStorage */
|
/** @var WorkflowStorage */
|
||||||
private $storage;
|
private $storage;
|
||||||
|
|
||||||
|
/** @var WorkflowStatisticsStorage */
|
||||||
|
private $statisticsStorage;
|
||||||
|
|
||||||
/** @var WorkflowValidator */
|
/** @var WorkflowValidator */
|
||||||
private $workflowValidator;
|
private $workflowValidator;
|
||||||
|
|
||||||
@ -26,11 +30,13 @@ class UpdateWorkflowController {
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
Hooks $hooks,
|
Hooks $hooks,
|
||||||
WorkflowStorage $storage,
|
WorkflowStorage $storage,
|
||||||
|
WorkflowStatisticsStorage $statisticsStorage,
|
||||||
WorkflowValidator $workflowValidator,
|
WorkflowValidator $workflowValidator,
|
||||||
UpdateStepsController $updateStepsController
|
UpdateStepsController $updateStepsController
|
||||||
) {
|
) {
|
||||||
$this->hooks = $hooks;
|
$this->hooks = $hooks;
|
||||||
$this->storage = $storage;
|
$this->storage = $storage;
|
||||||
|
$this->statisticsStorage = $statisticsStorage;
|
||||||
$this->workflowValidator = $workflowValidator;
|
$this->workflowValidator = $workflowValidator;
|
||||||
$this->updateStepsController = $updateStepsController;
|
$this->updateStepsController = $updateStepsController;
|
||||||
}
|
}
|
||||||
@ -40,6 +46,7 @@ class UpdateWorkflowController {
|
|||||||
if (!$workflow) {
|
if (!$workflow) {
|
||||||
throw Exceptions::workflowNotFound($id);
|
throw Exceptions::workflowNotFound($id);
|
||||||
}
|
}
|
||||||
|
$this->validateIfWorkflowCanBeUpdated($workflow, $data);
|
||||||
|
|
||||||
if (array_key_exists('name', $data)) {
|
if (array_key_exists('name', $data)) {
|
||||||
$workflow->setName($data['name']);
|
$workflow->setName($data['name']);
|
||||||
@ -71,6 +78,34 @@ class UpdateWorkflowController {
|
|||||||
return $workflow;
|
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 {
|
private function checkWorkflowStatus(string $status): void {
|
||||||
if (!in_array($status, Workflow::STATUS_ALL, true)) {
|
if (!in_array($status, Workflow::STATUS_ALL, true)) {
|
||||||
// translators: %s is the status.
|
// translators: %s is the status.
|
||||||
|
@ -32,6 +32,7 @@ class Exceptions {
|
|||||||
private const MISSING_REQUIRED_SUBJECTS = 'mailpoet_automation_missing_required_subjects';
|
private const MISSING_REQUIRED_SUBJECTS = 'mailpoet_automation_missing_required_subjects';
|
||||||
private const WORKFLOW_NOT_TRASHED = 'mailpoet_automation_workflow_not_trashed';
|
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_TEMPLATE_NOT_FOUND = 'mailpoet_automation_workflow_template_not_found';
|
||||||
|
private const WORKFLOW_HAS_ACTIVE_RUNS = 'mailpoet_automation_workflow_has_active_runs';
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
throw new InvalidStateException(
|
throw new InvalidStateException(
|
||||||
@ -228,4 +229,14 @@ class Exceptions {
|
|||||||
// translators: %d is the ID of the automation template.
|
// translators: %d is the ID of the automation template.
|
||||||
->withMessage(sprintf(__("Automation template with ID '%d' not found.", 'mailpoet'), $id));
|
->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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user