mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
Analytics
/
Endpoints
/
UpdateRunStatusEndpoint.php
AutomationFlowEndpoint.php
1 month ago
OverviewEndpoint.php
1 year ago
UpdateRunStatusEndpoint.php
2 months ago
index.php
3 years ago
UpdateRunStatusEndpoint.php
179 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use ActionScheduler_Store; |
| 9 | use MailPoet\API\REST\Request; |
| 10 | use MailPoet\API\REST\Response; |
| 11 | use MailPoet\Automation\Engine\API\Endpoint; |
| 12 | use MailPoet\Automation\Engine\Control\ActionScheduler as ASWrapper; |
| 13 | use MailPoet\Automation\Engine\Data\AutomationRun; |
| 14 | use MailPoet\Automation\Engine\Exceptions; |
| 15 | use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException; |
| 16 | use MailPoet\Automation\Engine\Hooks; |
| 17 | use MailPoet\Automation\Engine\Storage\AutomationRunStorage; |
| 18 | use MailPoet\Validator\Builder; |
| 19 | |
| 20 | class UpdateRunStatusEndpoint extends Endpoint { |
| 21 | |
| 22 | /** @var AutomationRunStorage */ |
| 23 | private $automationRunStorage; |
| 24 | |
| 25 | /** @var ASWrapper */ |
| 26 | private $actionScheduler; |
| 27 | |
| 28 | public function __construct( |
| 29 | AutomationRunStorage $automationRunStorage, |
| 30 | ASWrapper $actionScheduler |
| 31 | ) { |
| 32 | $this->automationRunStorage = $automationRunStorage; |
| 33 | $this->actionScheduler = $actionScheduler; |
| 34 | } |
| 35 | |
| 36 | public function handle(Request $request): Response { |
| 37 | /** @var int $runId */ |
| 38 | $runId = $request->getParam('id'); |
| 39 | $runId = intval($runId); |
| 40 | |
| 41 | /** @var string|null $status */ |
| 42 | $status = $request->getParam('status'); |
| 43 | |
| 44 | $run = $this->automationRunStorage->getAutomationRun($runId); |
| 45 | if (!$run) { |
| 46 | throw Exceptions::automationRunNotFound($runId); |
| 47 | } |
| 48 | |
| 49 | $currentStatus = $run->getStatus(); |
| 50 | $targetStatus = $status; |
| 51 | |
| 52 | // Validate allowed status values |
| 53 | $allowedStatuses = [ |
| 54 | AutomationRun::STATUS_RUNNING, |
| 55 | AutomationRun::STATUS_CANCELLED, |
| 56 | ]; |
| 57 | if (!in_array($targetStatus, $allowedStatuses, true)) { |
| 58 | throw UnexpectedValueException::create() |
| 59 | ->withMessage(__('Invalid status value.', 'mailpoet')) |
| 60 | ->withErrors(['status' => __('Status must be "running" or "cancelled".', 'mailpoet')]); |
| 61 | } |
| 62 | |
| 63 | // Validate status transitions |
| 64 | if ($currentStatus === $targetStatus) { |
| 65 | // Same status, no change needed |
| 66 | return new Response([ |
| 67 | 'id' => $run->getId(), |
| 68 | 'status' => $run->getStatus(), |
| 69 | 'updated_at' => $run->getUpdatedAt()->format(\DateTimeImmutable::W3C), |
| 70 | ]); |
| 71 | } |
| 72 | |
| 73 | // Allow transitions: running → cancelled, cancelled → running |
| 74 | $allowedTransitions = [ |
| 75 | AutomationRun::STATUS_RUNNING => [AutomationRun::STATUS_CANCELLED], |
| 76 | AutomationRun::STATUS_CANCELLED => [AutomationRun::STATUS_RUNNING], |
| 77 | ]; |
| 78 | |
| 79 | if ( |
| 80 | !isset($allowedTransitions[$currentStatus]) || |
| 81 | !in_array($targetStatus, $allowedTransitions[$currentStatus], true) |
| 82 | ) { |
| 83 | throw UnexpectedValueException::create() |
| 84 | ->withMessage( |
| 85 | sprintf( |
| 86 | // translators: This is an error message for an invalid status transition for an automation run. %1$s is the current status, %2$s is the target status. |
| 87 | __('Cannot transition run from "%1$s" to "%2$s".', 'mailpoet'), |
| 88 | $currentStatus, |
| 89 | $targetStatus |
| 90 | ) |
| 91 | ) |
| 92 | ->withErrors(['status' => __('Invalid status transition.', 'mailpoet')]); |
| 93 | } |
| 94 | |
| 95 | // Update run status |
| 96 | $this->automationRunStorage->updateStatus($runId, $targetStatus); |
| 97 | |
| 98 | // Schedule/unschedule actions based on status change |
| 99 | if ($targetStatus === AutomationRun::STATUS_CANCELLED) { |
| 100 | $this->unschedulePendingForRun($runId); |
| 101 | } elseif ($targetStatus === AutomationRun::STATUS_RUNNING) { |
| 102 | // If nothing pending for this run, enqueue next step based on stored next_step_id |
| 103 | if (!$this->hasPendingForRun($runId)) { |
| 104 | $nextStepId = $this->automationRunStorage->getNextStepId($runId); |
| 105 | if ($nextStepId) { |
| 106 | $this->enqueueStep($runId, (string)$nextStepId, 1); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Get updated run |
| 112 | $updatedRun = $this->automationRunStorage->getAutomationRun($runId); |
| 113 | if (!$updatedRun) { |
| 114 | throw Exceptions::automationRunNotFound($runId); |
| 115 | } |
| 116 | |
| 117 | // Return updated run data |
| 118 | return new Response([ |
| 119 | 'id' => $updatedRun->getId(), |
| 120 | 'status' => $updatedRun->getStatus(), |
| 121 | 'updated_at' => $updatedRun->getUpdatedAt()->format(\DateTimeImmutable::W3C), |
| 122 | ]); |
| 123 | } |
| 124 | |
| 125 | public static function getRequestSchema(): array { |
| 126 | return [ |
| 127 | 'id' => Builder::integer()->required(), |
| 128 | 'status' => Builder::string()->required(), |
| 129 | ]; |
| 130 | } |
| 131 | |
| 132 | private function unschedulePendingForRun(int $runId): void { |
| 133 | $actions = $this->actionScheduler->getScheduledActions([ |
| 134 | 'hook' => Hooks::AUTOMATION_STEP, |
| 135 | 'status' => ActionScheduler_Store::STATUS_PENDING, |
| 136 | ]); |
| 137 | foreach ($actions as $action) { |
| 138 | $args = $action->get_args(); |
| 139 | if ($this->argsMatchRun($args, $runId)) { |
| 140 | $this->actionScheduler->unscheduleAction(Hooks::AUTOMATION_STEP, $args); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private function hasPendingForRun(int $runId): bool { |
| 146 | $actions = $this->actionScheduler->getScheduledActions([ |
| 147 | 'hook' => Hooks::AUTOMATION_STEP, |
| 148 | 'status' => ActionScheduler_Store::STATUS_PENDING, |
| 149 | ]); |
| 150 | foreach ($actions as $action) { |
| 151 | $args = $action->get_args(); |
| 152 | if ($this->argsMatchRun($args, $runId)) { |
| 153 | return true; |
| 154 | } |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param mixed $args |
| 161 | */ |
| 162 | private function argsMatchRun($args, int $runId): bool { |
| 163 | return is_array($args) |
| 164 | && isset($args[0]) |
| 165 | && is_array($args[0]) |
| 166 | && isset($args[0]['automation_run_id']) |
| 167 | && is_numeric($args[0]['automation_run_id']) |
| 168 | && (int)$args[0]['automation_run_id'] === $runId; |
| 169 | } |
| 170 | |
| 171 | private function enqueueStep(int $runId, string $stepId, int $runNumber = 1): int { |
| 172 | return $this->actionScheduler->enqueue(Hooks::AUTOMATION_STEP, [[ |
| 173 | 'automation_run_id' => $runId, |
| 174 | 'step_id' => $stepId, |
| 175 | 'run_number' => $runNumber, |
| 176 | ]]); |
| 177 | } |
| 178 | } |
| 179 |