| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | namespace Worldline\Acquiring\Sdk\Authentication; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | class OAuth2Scopes |
| 13: | { |
| 14: | |
| 15: | |
| 16: | |
| 17: | private static ?array $scopesByOperation = null; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | private static ?array $allScopes = null; |
| 23: | |
| 24: | private function __construct() |
| 25: | { |
| 26: | } |
| 27: | |
| 28: | private static function initializeScopesByOperationIfNeeded(): void |
| 29: | { |
| 30: | if (is_null(OAuth2Scopes::$scopesByOperation)) { |
| 31: | OAuth2Scopes::$scopesByOperation = [ |
| 32: | "v1" => [ |
| 33: | "processPayment" => ["processing_payment"], |
| 34: | "getPaymentStatus" => ["processing_payment"], |
| 35: | "simpleCaptureOfPayment" => ["processing_payment"], |
| 36: | "reverseAuthorization" => ["processing_payment"], |
| 37: | "incrementPayment" => ["processing_payment"], |
| 38: | "createRefund" => ["processing_refund"], |
| 39: | "processStandaloneRefund" => ["processing_refund"], |
| 40: | "getRefund" => ["processing_refund"], |
| 41: | "captureRefund" => ["processing_refund"], |
| 42: | "reverseRefundAuthorization" => ["processing_refund"], |
| 43: | "processAccountVerification" => ["processing_accountverification"], |
| 44: | "processBalanceInquiry" => ["processing_balanceinquiry"], |
| 45: | "technicalReversal" => ["processing_operation_reverse"], |
| 46: | "requestDccRate" => ["processing_dcc_rate"], |
| 47: | "ping" => ["services_ping"], |
| 48: | "searchDisputes" => ["disputes_retrieve"], |
| 49: | "getDispute" => ["disputes_retrieve"], |
| 50: | "acceptDisputeLiability" => ["disputes_manage"], |
| 51: | "submitEvidence" => ["disputes_manage"], |
| 52: | "uploadDisputeDocument" => ["disputes_manage"], |
| 53: | "getDisputeDocument" => ["disputes_retrieve"], |
| 54: | "searchDisputeEntries" => ["disputes_retrieve"], |
| 55: | ], |
| 56: | ]; |
| 57: | OAuth2Scopes::$allScopes = []; |
| 58: | foreach (OAuth2Scopes::$scopesByOperation as $operations) { |
| 59: | foreach ($operations as $scopes) { |
| 60: | OAuth2Scopes::$allScopes = array_merge(OAuth2Scopes::$allScopes, array_values($scopes)); |
| 61: | } |
| 62: | } |
| 63: | OAuth2Scopes::$allScopes = array_unique(OAuth2Scopes::$allScopes); |
| 64: | } |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | public static function all(): array |
| 71: | { |
| 72: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
| 73: | return OAuth2Scopes::$allScopes; |
| 74: | } |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | public static function forApiVersion(string $apiVersion): array |
| 82: | { |
| 83: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
| 84: | $operations = OAuth2Scopes::$scopesByOperation[$apiVersion] ?? []; |
| 85: | $result = []; |
| 86: | foreach ($operations as $scopes) { |
| 87: | $result = array_merge($result, $scopes); |
| 88: | } |
| 89: | return array_unique($result); |
| 90: | } |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | public static function forOperation(string $apiVersion, string $operationId): array |
| 99: | { |
| 100: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
| 101: | $operations = OAuth2Scopes::$scopesByOperation[$apiVersion] ?? []; |
| 102: | return $operations[$operationId] ?? []; |
| 103: | } |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | public static function forOperations(string $apiVersion, string ...$operationIds): array |
| 112: | { |
| 113: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
| 114: | $operations = OAuth2Scopes::$scopesByOperation[$apiVersion] ?? []; |
| 115: | if (count($operationIds) === 1) { |
| 116: | return $operations[$operationIds[0]] ?? []; |
| 117: | } |
| 118: | $result = []; |
| 119: | foreach ($operationIds as $operationId) { |
| 120: | $scopes = $operations[$operationId] ?? []; |
| 121: | $result = array_merge($result, $scopes); |
| 122: | } |
| 123: | return array_unique($result); |
| 124: | } |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | public static function forFilteredOperations(callable $filter): array |
| 134: | { |
| 135: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
| 136: | $result = []; |
| 137: | foreach (OAuth2Scopes::$scopesByOperation as $apiVersion => $operations) { |
| 138: | foreach ($operations as $operationId => $scopes) { |
| 139: | if (call_user_func($filter, $apiVersion, $operationId)) { |
| 140: | $result = array_merge($result, $scopes); |
| 141: | } |
| 142: | } |
| 143: | } |
| 144: | return array_unique($result); |
| 145: | } |
| 146: | } |
| 147: | |