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 $SCOPES_BY_OPERATION = null; |
18: | |
19: | |
20: | |
21: | |
22: | private static $ALL_SCOPES = null; |
23: | |
24: | private function __construct() |
25: | { |
26: | } |
27: | |
28: | private static function initializeScopesByOperationIfNeeded() |
29: | { |
30: | if (is_null(OAuth2Scopes::$SCOPES_BY_OPERATION)) { |
31: | OAuth2Scopes::$SCOPES_BY_OPERATION = [ |
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: | ], |
49: | ]; |
50: | OAuth2Scopes::$ALL_SCOPES = []; |
51: | foreach (OAuth2Scopes::$SCOPES_BY_OPERATION as $operations) { |
52: | foreach ($operations as $scopes) { |
53: | OAuth2Scopes::$ALL_SCOPES = array_merge(OAuth2Scopes::$ALL_SCOPES, array_values($scopes)); |
54: | } |
55: | } |
56: | OAuth2Scopes::$ALL_SCOPES = array_unique(OAuth2Scopes::$ALL_SCOPES); |
57: | } |
58: | } |
59: | |
60: | |
61: | |
62: | |
63: | public static function all() |
64: | { |
65: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
66: | return OAuth2Scopes::$ALL_SCOPES; |
67: | } |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | public static function forApiVersion($apiVersion) |
74: | { |
75: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
76: | $operations = OAuth2Scopes::$SCOPES_BY_OPERATION[$apiVersion] ?? []; |
77: | $result = []; |
78: | foreach ($operations as $scopes) { |
79: | $result = array_merge($result, $scopes); |
80: | } |
81: | return array_unique($result); |
82: | } |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | public static function forOperation($apiVersion, $operationId) |
90: | { |
91: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
92: | $operations = OAuth2Scopes::$SCOPES_BY_OPERATION[$apiVersion] ?? []; |
93: | return $operations[$operationId] ?? []; |
94: | } |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | public static function forOperations($apiVersion, ...$operationIds) |
102: | { |
103: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
104: | $operations = OAuth2Scopes::$SCOPES_BY_OPERATION[$apiVersion] ?? []; |
105: | if (count($operationIds) === 1) { |
106: | return $operations[$operationIds[0]] ?? []; |
107: | } |
108: | $result = []; |
109: | foreach ($operationIds as $operationId) { |
110: | $scopes = $operations[$operationId] ?? []; |
111: | $result = array_merge($result, $scopes); |
112: | } |
113: | return array_unique($result); |
114: | } |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | public static function forFilteredOperations(callable $filter) { |
124: | OAuth2Scopes::initializeScopesByOperationIfNeeded(); |
125: | $result = []; |
126: | foreach (OAuth2Scopes::$SCOPES_BY_OPERATION as $apiVersion => $operations) { |
127: | foreach ($operations as $operationId => $scopes) { |
128: | if (call_user_func($filter, $apiVersion, $operationId)) { |
129: | $result = array_merge($result, $scopes); |
130: | } |
131: | } |
132: | } |
133: | return array_unique($result); |
134: | } |
135: | } |
136: | |