|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Services; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Log; |
| 6 | +use InvalidArgumentException; |
| 7 | +use ProcessMaker\Contracts\ConditionalRedirectServiceInterface; |
| 8 | +use ProcessMaker\Managers\DataManager; |
| 9 | +use ProcessMaker\Models\FormalExpression; |
| 10 | +use ProcessMaker\Models\ProcessRequest; |
| 11 | +use ProcessMaker\Models\ProcessRequestToken; |
| 12 | + |
| 13 | +/** |
| 14 | + * ConditionalRedirectService |
| 15 | + * |
| 16 | + * This service handles the evaluation of conditional redirects in ProcessMaker workflows. |
| 17 | + * It processes a set of conditions and returns the first condition that evaluates to true, |
| 18 | + * along with its associated redirect configuration. |
| 19 | + * |
| 20 | + * The service uses FEEL (Friendly Enough Expression Language) expressions to evaluate |
| 21 | + * conditions against process data, allowing for dynamic routing based on runtime data. |
| 22 | + * |
| 23 | + * @since 4.0.0 |
| 24 | + */ |
| 25 | +class ConditionalRedirectService implements ConditionalRedirectServiceInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var FormalExpression |
| 29 | + */ |
| 30 | + private FormalExpression $feel; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var DataManager |
| 34 | + */ |
| 35 | + private DataManager $dataManager; |
| 36 | + |
| 37 | + private array $errors = []; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructor |
| 41 | + * |
| 42 | + * Initializes the service with required dependencies for expression evaluation |
| 43 | + * and data management. |
| 44 | + */ |
| 45 | + public function __construct() |
| 46 | + { |
| 47 | + $this->feel = new FormalExpression(); |
| 48 | + $this->dataManager = new DataManager(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Process a set of conditional redirects and return the first condition that evaluates to true |
| 53 | + * |
| 54 | + * This method iterates through an array of conditional redirect configurations, |
| 55 | + * evaluating each condition using FEEL expressions against the provided data. |
| 56 | + * Returns the first condition that evaluates to true, or null if no conditions match. |
| 57 | + * |
| 58 | + * @param array $conditionalRedirect Array of conditional redirect configurations |
| 59 | + * Each item must contain a 'condition' key with a FEEL expression |
| 60 | + * Example: [ |
| 61 | + * [ |
| 62 | + * 'condition' => 'amount > 1000', |
| 63 | + * 'type' => 'externalURL', |
| 64 | + * 'value' => 'https://example.com/approval' |
| 65 | + * ], |
| 66 | + * [ |
| 67 | + * 'condition' => 'status = "urgent"', |
| 68 | + * 'type' => 'taskList', |
| 69 | + * 'value' => null |
| 70 | + * ] |
| 71 | + * ] |
| 72 | + * @param array $data Process data to evaluate conditions against |
| 73 | + * Contains variables from the process instance |
| 74 | + * Example: ['amount' => 1500, 'status' => 'urgent', 'user' => 'john'] |
| 75 | + * |
| 76 | + * @return array|null The first matching conditional redirect configuration, or null if none match |
| 77 | + * |
| 78 | + * @throws InvalidArgumentException When a condition item is missing the required 'condition' key |
| 79 | + * |
| 80 | + * @example |
| 81 | + * ```php |
| 82 | + * $service = new ConditionalRedirectService(); |
| 83 | + * |
| 84 | + * $conditionalRedirect = [ |
| 85 | + * [ |
| 86 | + * 'condition' => 'amount > 1000', |
| 87 | + * 'type' => 'externalURL', |
| 88 | + * 'value' => 'https://example.com/approval' |
| 89 | + * ], |
| 90 | + * [ |
| 91 | + * 'condition' => 'amount <= 1000', |
| 92 | + * 'type' => 'taskList', |
| 93 | + * 'value' => null |
| 94 | + * ] |
| 95 | + * ]; |
| 96 | + * |
| 97 | + * $data = ['amount' => 1500, 'status' => 'pending']; |
| 98 | + * |
| 99 | + * $result = $service->resolve($conditionalRedirect, $data); |
| 100 | + * // Returns: ['condition' => 'amount > 1000', 'type' => 'externalURL', 'value' => 'https://example.com/approval'] |
| 101 | + * ``` |
| 102 | + */ |
| 103 | + public function resolve(array $conditionalRedirect, array $data): ?array |
| 104 | + { |
| 105 | + $this->errors = []; |
| 106 | + foreach ($conditionalRedirect as $item) { |
| 107 | + if (!isset($item['condition'])) { |
| 108 | + throw new InvalidArgumentException('Condition is required'); |
| 109 | + } |
| 110 | + |
| 111 | + $condition = $item['condition']; |
| 112 | + |
| 113 | + $this->feel->setBody($condition); |
| 114 | + try { |
| 115 | + $result = ($this->feel)($data); |
| 116 | + } catch (\Throwable $e) { |
| 117 | + $this->errors[] = $e->getMessage(); |
| 118 | + continue; |
| 119 | + } |
| 120 | + if ($result) { |
| 121 | + return $item; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Process conditional redirects for a specific process request token |
| 130 | + * |
| 131 | + * This method is a convenience wrapper that automatically retrieves process data |
| 132 | + * from a ProcessRequestToken and evaluates conditional redirects against that data. |
| 133 | + * It's commonly used when you have a token and want to determine the appropriate |
| 134 | + * redirect based on the current process state and data, it also considers |
| 135 | + * multi-instance tasks. |
| 136 | + * |
| 137 | + * @param array $conditionalRedirect Array of conditional redirect configurations |
| 138 | + * Each item must contain a 'condition' key with a FEEL expression |
| 139 | + * Example: [ |
| 140 | + * [ |
| 141 | + * 'condition' => 'taskStatus = "completed"', |
| 142 | + * 'type' => 'homepageDashboard', |
| 143 | + * 'value' => null |
| 144 | + * ], |
| 145 | + * [ |
| 146 | + * 'condition' => 'taskStatus = "pending"', |
| 147 | + * 'type' => 'taskList', |
| 148 | + * 'value' => null |
| 149 | + * ] |
| 150 | + * ] |
| 151 | + * @param ProcessRequestToken $token The process request token to evaluate conditions against |
| 152 | + * The token contains the process instance data and context |
| 153 | + * |
| 154 | + * @return array|null The first matching conditional redirect configuration, or null if none match |
| 155 | + * |
| 156 | + * @throws InvalidArgumentException When a condition item is missing the required 'condition' key |
| 157 | + * |
| 158 | + * @example |
| 159 | + * ```php |
| 160 | + * $service = new ConditionalRedirectService(); |
| 161 | + * $token = ProcessRequestToken::find(123); |
| 162 | + * |
| 163 | + * $conditionalRedirect = [ |
| 164 | + * [ |
| 165 | + * 'condition' => 'taskStatus = "completed"', |
| 166 | + * 'type' => 'homepageDashboard', |
| 167 | + * 'value' => null |
| 168 | + * ], |
| 169 | + * [ |
| 170 | + * 'condition' => 'taskStatus = "pending"', |
| 171 | + * 'type' => 'taskList', |
| 172 | + * 'value' => null |
| 173 | + * ] |
| 174 | + * ]; |
| 175 | + * |
| 176 | + * $result = $service->resolveForToken($conditionalRedirect, $token); |
| 177 | + * // Returns the appropriate redirect configuration based on the token's data |
| 178 | + * ``` |
| 179 | + * |
| 180 | + * @see resolve() For detailed parameter documentation |
| 181 | + */ |
| 182 | + public function resolveForToken(array $conditionalRedirect, ProcessRequestToken $token): ?array |
| 183 | + { |
| 184 | + $data = $this->dataManager->getData($token); |
| 185 | + $result = $this->resolve($conditionalRedirect, $data); |
| 186 | + if ($this->errors) { |
| 187 | + $case_number = $this->getCaseNumber($token); |
| 188 | + foreach ($this->errors as $error) { |
| 189 | + $this->logError($token, $error, $case_number); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return $result; |
| 194 | + } |
| 195 | + |
| 196 | + private function getCaseNumber(ProcessRequestToken $token): ?int |
| 197 | + { |
| 198 | + // get process request from relationship if loaded, otherwise get from database |
| 199 | + if ($token->relationLoaded('processRequest')) { |
| 200 | + $case_number = $token->processRequest->case_number; |
| 201 | + } else { |
| 202 | + // get case_number only to avoid to hidrate all the process request data |
| 203 | + $case_number = ProcessRequest::where('id', $token->process_request_id)->value('case_number'); |
| 204 | + } |
| 205 | + |
| 206 | + return $case_number; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Log an error when evaluating conditional redirects |
| 211 | + * |
| 212 | + * @param ProcessRequestToken $token |
| 213 | + * @param string $error |
| 214 | + * @param string $case_number |
| 215 | + */ |
| 216 | + private function logError(ProcessRequestToken $token, string $error, int $case_number) |
| 217 | + { |
| 218 | + Log::error('Conditional Redirect: ', ['error' => $error, 'case_number' => $case_number, 'token' => $token->id]); |
| 219 | + } |
| 220 | +} |
0 commit comments