1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Getloy\PaymentProviders\Configuration; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Abstract payment provider config class. |
9
|
|
|
*/ |
10
|
|
|
abstract class PaymentProviderConfig |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $config; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Instantiate a payment provider configuration. |
17
|
|
|
* @param array $config Configuration for the payment method. |
18
|
|
|
*/ |
19
|
|
|
public function __construct(array $config) |
20
|
|
|
{ |
21
|
|
|
$this->config = $this->evaluateConfig($config, $this->configValidation()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get configuration value. |
26
|
|
|
* @param string $configOption Name of the configuration option. |
27
|
|
|
* @return mixed The configuration value, or an empty string if the option is not set. |
28
|
|
|
*/ |
29
|
|
|
public function get(string $option) |
30
|
|
|
{ |
31
|
|
|
if (!array_key_exists($option, $this->config)) { |
32
|
|
|
return ''; |
33
|
|
|
} |
34
|
|
|
return $this->config[$option]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Generate validation configuration for payment method-specific configuration options. |
39
|
|
|
* @return array Validation configuration |
40
|
|
|
*/ |
41
|
|
|
abstract protected function paymentMethodConfigValidation(): array; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Generate validation configuration for general and payment method-specific configuration |
45
|
|
|
* options. |
46
|
|
|
* @return array Validation configuration |
47
|
|
|
*/ |
48
|
|
|
protected function configValidation(): array |
49
|
|
|
{ |
50
|
|
|
$configValidation = $this->paymentMethodConfigValidation(); |
51
|
|
|
if (!array_key_exists('testMode', $configValidation)) { |
52
|
|
|
$configValidation['testMode'] = [ |
53
|
|
|
'type' => 'boolean', |
54
|
|
|
'required' => false, |
55
|
|
|
'default' => true, |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
return $configValidation; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Validates the specified config option against the validation rules and returns the config |
63
|
|
|
* value. |
64
|
|
|
* @param string $configOption Name of the configuration option to evaluate. |
65
|
|
|
* @param array $config Configuration for the payment method. |
66
|
|
|
* @param array $configValidation Validation configuration. |
67
|
|
|
* @return mixed The config value after validation, or an empty string if there is no value for |
68
|
|
|
* the option. |
69
|
|
|
* @throws Exception If the validation fails. |
70
|
|
|
*/ |
71
|
|
|
protected function evaluateConfigValue( |
72
|
|
|
string $configOption, |
73
|
|
|
array $config, |
74
|
|
|
array $configValidation |
75
|
|
|
) { |
76
|
|
|
|
77
|
|
|
if (!array_key_exists($configOption, $configValidation)) { |
78
|
|
|
throw new Exception(sprintf('Unknown configuration option "%s"', $configOption)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$optionValidation = $configValidation[$configOption]; |
82
|
|
|
if (!array_key_exists($configOption, $config)) { |
83
|
|
|
if (array_key_exists('required', $optionValidation) && $optionValidation['required']) { |
84
|
|
|
throw new Exception(sprintf( |
85
|
|
|
'Configuration is missing required option "%s"', |
86
|
|
|
$configOption |
87
|
|
|
)); |
88
|
|
|
} |
89
|
|
|
if (array_key_exists('default', $optionValidation)) { |
90
|
|
|
return $optionValidation['default']; |
91
|
|
|
} |
92
|
|
|
return ''; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$optionValue = $config[$configOption]; |
96
|
|
|
if (array_key_exists('type', $optionValidation) |
97
|
|
|
&& gettype($optionValue) !== $optionValidation['type']) { |
98
|
|
|
throw new Exception(sprintf( |
99
|
|
|
'Validation of option "%s" failed: expected value of type "%s", but found "%s"!', |
100
|
|
|
$configOption, |
101
|
|
|
$optionValidation['type'], |
102
|
|
|
gettype($optionValue) |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (array_key_exists('allowedValues', $optionValidation) |
107
|
|
|
&& !in_array($optionValue, $optionValidation['allowedValues'], true)) { |
108
|
|
|
throw new Exception(sprintf( |
109
|
|
|
'Validation of option "%s" failed: value "%s" is not one of the allowed values "%s"!', |
110
|
|
|
$configOption, |
111
|
|
|
$optionValue, |
112
|
|
|
implode('", "', $optionValidation['allowedValues']) |
113
|
|
|
)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $optionValue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Validates the specified config options against the validation rules and returns evaluated |
121
|
|
|
* config. |
122
|
|
|
* @param array $config Configuration for the payment method. |
123
|
|
|
* @param array $configValidation Validation configuration. |
124
|
|
|
* @return array The config after validation. |
125
|
|
|
* @throws Exception If the validation fails. |
126
|
|
|
*/ |
127
|
|
|
protected function evaluateConfig(array $config, array $configValidation): array |
128
|
|
|
{ |
129
|
|
|
$evaluatedConfig = []; |
130
|
|
|
foreach ($configValidation as $option => $validation) { |
131
|
|
|
$evaldValue = $this->evaluateConfigValue($option, $config, $configValidation); |
132
|
|
|
if (!is_null($evaldValue)) { |
133
|
|
|
$evaluatedConfig[$option] = $evaldValue; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return $evaluatedConfig; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|