22
33namespace ProcessMaker \Services ;
44
5+ use Illuminate \Support \Facades \Log ;
56use InvalidArgumentException ;
67use ProcessMaker \Contracts \ConditionalRedirectServiceInterface ;
78use ProcessMaker \Managers \DataManager ;
8- use ProcessMaker \Models \Comment ;
99use ProcessMaker \Models \FormalExpression ;
1010use ProcessMaker \Models \ProcessRequest ;
1111use ProcessMaker \Models \ProcessRequestToken ;
1212
1313/**
1414 * ConditionalRedirectService
15- *
15+ *
1616 * This service handles the evaluation of conditional redirects in ProcessMaker workflows.
1717 * It processes a set of conditions and returns the first condition that evaluates to true,
1818 * along with its associated redirect configuration.
19- *
19+ *
2020 * The service uses FEEL (Friendly Enough Expression Language) expressions to evaluate
2121 * conditions against process data, allowing for dynamic routing based on runtime data.
22- *
23- * @package ProcessMaker\Services
22+ *
2423 * @since 4.0.0
2524 */
2625class ConditionalRedirectService implements ConditionalRedirectServiceInterface
@@ -29,7 +28,7 @@ class ConditionalRedirectService implements ConditionalRedirectServiceInterface
2928 * @var FormalExpression
3029 */
3130 private FormalExpression $ feel ;
32-
31+
3332 /**
3433 * @var DataManager
3534 */
@@ -39,7 +38,7 @@ class ConditionalRedirectService implements ConditionalRedirectServiceInterface
3938
4039 /**
4140 * Constructor
42- *
41+ *
4342 * Initializes the service with required dependencies for expression evaluation
4443 * and data management.
4544 */
@@ -51,11 +50,11 @@ public function __construct()
5150
5251 /**
5352 * Process a set of conditional redirects and return the first condition that evaluates to true
54- *
53+ *
5554 * This method iterates through an array of conditional redirect configurations,
5655 * evaluating each condition using FEEL expressions against the provided data.
5756 * Returns the first condition that evaluates to true, or null if no conditions match.
58- *
57+ *
5958 * @param array $conditionalRedirect Array of conditional redirect configurations
6059 * Each item must contain a 'condition' key with a FEEL expression
6160 * Example: [
@@ -73,15 +72,15 @@ public function __construct()
7372 * @param array $data Process data to evaluate conditions against
7473 * Contains variables from the process instance
7574 * Example: ['amount' => 1500, 'status' => 'urgent', 'user' => 'john']
76- *
75+ *
7776 * @return array|null The first matching conditional redirect configuration, or null if none match
78- *
77+ *
7978 * @throws InvalidArgumentException When a condition item is missing the required 'condition' key
80- *
79+ *
8180 * @example
8281 * ```php
8382 * $service = new ConditionalRedirectService();
84- *
83+ *
8584 * $conditionalRedirect = [
8685 * [
8786 * 'condition' => 'amount > 1000',
@@ -94,9 +93,9 @@ public function __construct()
9493 * 'value' => null
9594 * ]
9695 * ];
97- *
96+ *
9897 * $data = ['amount' => 1500, 'status' => 'pending'];
99- *
98+ *
10099 * $result = $service->resolve($conditionalRedirect, $data);
101100 * // Returns: ['condition' => 'amount > 1000', 'type' => 'externalURL', 'value' => 'https://example.com/approval']
102101 * ```
@@ -128,13 +127,13 @@ public function resolve(array $conditionalRedirect, array $data): ?array
128127
129128 /**
130129 * Process conditional redirects for a specific process request token
131- *
130+ *
132131 * This method is a convenience wrapper that automatically retrieves process data
133132 * from a ProcessRequestToken and evaluates conditional redirects against that data.
134133 * It's commonly used when you have a token and want to determine the appropriate
135134 * redirect based on the current process state and data, it also considers
136135 * multi-instance tasks.
137- *
136+ *
138137 * @param array $conditionalRedirect Array of conditional redirect configurations
139138 * Each item must contain a 'condition' key with a FEEL expression
140139 * Example: [
@@ -151,16 +150,16 @@ public function resolve(array $conditionalRedirect, array $data): ?array
151150 * ]
152151 * @param ProcessRequestToken $token The process request token to evaluate conditions against
153152 * The token contains the process instance data and context
154- *
153+ *
155154 * @return array|null The first matching conditional redirect configuration, or null if none match
156- *
155+ *
157156 * @throws InvalidArgumentException When a condition item is missing the required 'condition' key
158- *
157+ *
159158 * @example
160159 * ```php
161160 * $service = new ConditionalRedirectService();
162161 * $token = ProcessRequestToken::find(123);
163- *
162+ *
164163 * $conditionalRedirect = [
165164 * [
166165 * 'condition' => 'taskStatus = "completed"',
@@ -173,11 +172,11 @@ public function resolve(array $conditionalRedirect, array $data): ?array
173172 * 'value' => null
174173 * ]
175174 * ];
176- *
175+ *
177176 * $result = $service->resolveForToken($conditionalRedirect, $token);
178177 * // Returns the appropriate redirect configuration based on the token's data
179178 * ```
180- *
179+ *
181180 * @see resolve() For detailed parameter documentation
182181 */
183182 public function resolveForToken (array $ conditionalRedirect , ProcessRequestToken $ token ): ?array
@@ -187,9 +186,10 @@ public function resolveForToken(array $conditionalRedirect, ProcessRequestToken
187186 if ($ this ->errors ) {
188187 $ case_number = $ this ->getCaseNumber ($ token );
189188 foreach ($ this ->errors as $ error ) {
190- $ this ->addLogComment ($ token , $ error , $ case_number );
189+ $ this ->logError ($ token , $ error , $ case_number );
191190 }
192191 }
192+
193193 return $ result ;
194194 }
195195
@@ -206,16 +206,15 @@ private function getCaseNumber(ProcessRequestToken $token): ?int
206206 return $ case_number ;
207207 }
208208
209- private function addLogComment (ProcessRequestToken $ token , string $ error , string $ case_number )
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 )
210217 {
211- Comment::create ([
212- 'body ' => $ error ,
213- 'user_id ' => null ,
214- 'subject ' => $ error ,
215- 'type ' => 'LOG ' ,
216- 'case_number ' => $ case_number ,
217- 'commentable_type ' => ProcessRequest::class,
218- 'commentable_id ' => $ token ->process_request_id ,
219- ]);
218+ Log::error ('Conditional Redirect: ' , ['error ' => $ error , 'case_number ' => $ case_number , 'token ' => $ token ->id ]);
220219 }
221220}
0 commit comments