Skip to content

Commit e557674

Browse files
committed
refactor: add status validation methods to SequentialSigningService
Move status-related validation logic to SequentialSigningService where it belongs, improving code cohesion and testability. Changes: - Add hasPendingLowerOrderSigners() method to check for incomplete lower-order signers - Add isStatusUpgrade() method to validate status transitions - Add validateStatusByOrder() method to encapsulate ordering validation logic for status transitions These methods are now public and easily testable, centralizing all sequential signing validation logic in a single specialized service. Signed-off-by: Vitor Mattos <[email protected]>
1 parent 9c6cebf commit e557674

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

lib/Service/SequentialSigningService.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,66 @@ private function getSignatureFlow(): SignatureFlow {
170170

171171
return SignatureFlow::from($value);
172172
}
173+
174+
/**
175+
* Check if there are signers with lower signing order that haven't signed yet
176+
*/
177+
public function hasPendingLowerOrderSigners(int $fileId, int $currentOrder): bool {
178+
$signRequests = $this->signRequestMapper->getByFileId($fileId);
179+
180+
foreach ($signRequests as $signRequest) {
181+
$order = $signRequest->getSigningOrder();
182+
$status = $signRequest->getStatusEnum();
183+
184+
// If a signer with lower order hasn't signed yet, return true
185+
if ($order < $currentOrder && $status !== SignRequestStatus::SIGNED) {
186+
return true;
187+
}
188+
}
189+
190+
return false;
191+
}
192+
193+
/**
194+
* Check if changing from currentStatus to desiredStatus is an upgrade (or same level)
195+
* Status hierarchy: DRAFT (0) < ABLE_TO_SIGN (1) < SIGNED (2)
196+
*/
197+
public function isStatusUpgrade(
198+
SignRequestStatus $currentStatus,
199+
SignRequestStatus $desiredStatus
200+
): bool {
201+
return $desiredStatus->value >= $currentStatus->value;
202+
}
203+
204+
/**
205+
* Validate if a signer can transition to ABLE_TO_SIGN status based on signing order
206+
* In ordered numeric flow, prevents skipping ahead if lower-order signers haven't signed
207+
*
208+
* @param SignRequestStatus $desiredStatus The status being requested
209+
* @param int $signingOrder The signer's order
210+
* @param int $fileId The file ID
211+
* @return SignRequestStatus The validated status (may return DRAFT if validation fails)
212+
*/
213+
public function validateStatusByOrder(
214+
SignRequestStatus $desiredStatus,
215+
int $signingOrder,
216+
int $fileId
217+
): SignRequestStatus {
218+
// Only validate for ordered numeric flow
219+
if (!$this->isOrderedNumericFlow()) {
220+
return $desiredStatus;
221+
}
222+
223+
// Only validate when trying to set ABLE_TO_SIGN and not the first signer
224+
if ($desiredStatus !== SignRequestStatus::ABLE_TO_SIGN || $signingOrder <= 1) {
225+
return $desiredStatus;
226+
}
227+
228+
// Check if any lower order signers haven't signed yet
229+
if ($this->hasPendingLowerOrderSigners($fileId, $signingOrder)) {
230+
return SignRequestStatus::DRAFT;
231+
}
232+
233+
return $desiredStatus;
234+
}
173235
}

0 commit comments

Comments
 (0)