进行了一些小优化
This commit is contained in:
@@ -32,6 +32,16 @@ final class TransitionService {
|
||||
|
||||
/** @param array<string,mixed> $fields @param array<string,mixed> $event */
|
||||
public function transition(array $application, string $next, array $fields = [], array $event = []) {
|
||||
return $this->transition_with_policy($application, $next, $fields, $event, false);
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $fields @param array<string,mixed> $event */
|
||||
public function admin_transition(array $application, string $next, array $fields = [], array $event = []) {
|
||||
return $this->transition_with_policy($application, $next, $fields, $event, true);
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $fields @param array<string,mixed> $event */
|
||||
private function transition_with_policy(array $application, string $next, array $fields, array $event, bool $admin_override) {
|
||||
$next = sanitize_key($next);
|
||||
$dedupe = isset($event['dedupe_key']) ? (string) $event['dedupe_key'] : '';
|
||||
if ($dedupe !== '') {
|
||||
@@ -62,7 +72,7 @@ final class TransitionService {
|
||||
}
|
||||
}
|
||||
|
||||
$changed = $this->transition_locked($locked, $next, $fields, $event);
|
||||
$changed = $this->apply_transition_locked($locked, $next, $fields, $event, $admin_override);
|
||||
if (is_wp_error($changed)) {
|
||||
$wpdb->query('ROLLBACK');
|
||||
return $changed;
|
||||
@@ -76,11 +86,6 @@ final class TransitionService {
|
||||
return $updated;
|
||||
}
|
||||
|
||||
/** @return array<int,string> */
|
||||
public static function allowed_targets(string $status): array {
|
||||
return self::ALLOWED[sanitize_key($status)] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply an identity transition inside an already-owned database transaction.
|
||||
* The caller must lock the application row before invoking this method.
|
||||
@@ -91,14 +96,29 @@ final class TransitionService {
|
||||
* @return true|\WP_Error
|
||||
*/
|
||||
public function transition_locked(array $application, string $next, array $fields = [], array $event = []) {
|
||||
return $this->apply_transition_locked($application, $next, $fields, $event, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $application
|
||||
* @param array<string,mixed> $fields
|
||||
* @param array<string,mixed> $event
|
||||
* @return true|\WP_Error
|
||||
*/
|
||||
private function apply_transition_locked(array $application, string $next, array $fields, array $event, bool $admin_override) {
|
||||
$current = sanitize_key((string) ($application['status'] ?? ''));
|
||||
$next = sanitize_key($next);
|
||||
if (!in_array($next, self::ALLOWED[$current] ?? [], true)) {
|
||||
$allowed = $admin_override
|
||||
? in_array($next, ['manual_pending', 'verified', 'rejected', 'cancelled'], true) && $next !== $current
|
||||
: in_array($next, self::ALLOWED[$current] ?? [], true);
|
||||
if (!$allowed) {
|
||||
return new \WP_Error('aksrt_identity_transition', __('该认证记录当前不可执行此操作', 'aksrt'), ['status' => 409]);
|
||||
}
|
||||
if (in_array($next, ['verified', 'rejected', 'cancelled'], true)) {
|
||||
$fields['completed_at'] = current_time('mysql', true);
|
||||
$fields['sensitive_blob'] = null;
|
||||
} else {
|
||||
$fields['completed_at'] = null;
|
||||
}
|
||||
if (!$this->applications->transition((int) $application['id'], $current, $next, $fields)) {
|
||||
return new \WP_Error('aksrt_identity_conflict', __('认证状态已变化,请刷新后重试', 'aksrt'), ['status' => 409]);
|
||||
|
||||
Reference in New Issue
Block a user