10
10
use Magento \Framework \App \Config \ConfigResource \ConfigInterface ;
11
11
use Magento \Framework \App \Config \ScopeConfigInterface ;
12
12
use Magento \Framework \Encryption \EncryptorInterface ;
13
- use Magento \Framework \Setup \InstallDataInterface ;
14
- use Magento \Framework \Setup \ModuleContextInterface ;
15
13
use Magento \Framework \Setup \ModuleDataSetupInterface ;
16
14
use Magento \Framework \Setup \Patch \DataPatchInterface ;
17
- use Magento \Framework \Setup \Patch \PatchInterface ;
18
15
use Magento \Store \Model \ScopeInterface ;
16
+ use Magento \Store \Model \StoreManagerInterface ;
17
+ use Magento \Store \Api \Data \WebsiteInterface ;
19
18
20
19
/**
21
20
* Copies the Authorize.net DirectPost configuration values to the new Accept.js module.
@@ -46,6 +45,11 @@ class CopyCurrentConfig implements DataPatchInterface
46
45
*/
47
46
private $ moduleDataSetup ;
48
47
48
+ /**
49
+ * @var StoreManagerInterface
50
+ */
51
+ private $ storeManager ;
52
+
49
53
/**
50
54
* @var array
51
55
*/
@@ -76,26 +80,40 @@ class CopyCurrentConfig implements DataPatchInterface
76
80
* @param ScopeConfigInterface $scopeConfig
77
81
* @param ConfigInterface $resourceConfig
78
82
* @param EncryptorInterface $encryptor
83
+ * @param StoreManagerInterface $storeManager
79
84
*/
80
85
public function __construct (
81
86
ModuleDataSetupInterface $ moduleDataSetup ,
82
87
ScopeConfigInterface $ scopeConfig ,
83
88
ConfigInterface $ resourceConfig ,
84
- EncryptorInterface $ encryptor
89
+ EncryptorInterface $ encryptor ,
90
+ StoreManagerInterface $ storeManager
85
91
) {
86
92
$ this ->scopeConfig = $ scopeConfig ;
87
93
$ this ->resourceConfig = $ resourceConfig ;
88
94
$ this ->encryptor = $ encryptor ;
89
95
$ this ->moduleDataSetup = $ moduleDataSetup ;
96
+ $ this ->storeManager = $ storeManager ;
90
97
}
91
98
92
99
/**
93
100
* @inheritdoc
94
101
*/
95
- public function apply ()
102
+ public function apply (): void
96
103
{
97
104
$ this ->moduleDataSetup ->startSetup ();
105
+ $ this ->migrateDefaultValues ();
106
+ $ this ->migrateWebsiteValues ();
107
+ $ this ->moduleDataSetup ->endSetup ();
108
+ }
98
109
110
+ /**
111
+ * Migrate configuration values from DirectPost to Accept.js on default scope
112
+ *
113
+ * @return void
114
+ */
115
+ private function migrateDefaultValues (): void
116
+ {
99
117
foreach ($ this ->configFieldsToMigrate as $ field ) {
100
118
$ configValue = $ this ->getOldConfigValue ($ field );
101
119
@@ -108,24 +126,62 @@ public function apply()
108
126
$ configValue = $ this ->getOldConfigValue ($ field );
109
127
110
128
if (!empty ($ configValue )) {
111
- $ this ->saveNewConfigValue ($ field , $ configValue , true );
129
+ $ this ->saveNewConfigValue (
130
+ $ field ,
131
+ $ configValue ,
132
+ ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
133
+ 0 ,
134
+ true
135
+ );
112
136
}
113
137
}
138
+ }
114
139
115
- $ this ->moduleDataSetup ->endSetup ();
140
+ /**
141
+ * Migrate configuration values from DirectPost to Accept.js on all website scopes
142
+ *
143
+ * @return void
144
+ */
145
+ private function migrateWebsiteValues (): void
146
+ {
147
+ foreach ($ this ->storeManager ->getWebsites () as $ website ) {
148
+ $ websiteID = (int ) $ website ->getId ();
149
+
150
+ foreach ($ this ->configFieldsToMigrate as $ field ) {
151
+ $ configValue = $ this ->getOldConfigValue ($ field , ScopeInterface::SCOPE_WEBSITES , $ websiteID );
152
+
153
+ if (!empty ($ configValue )) {
154
+ $ this ->saveNewConfigValue ($ field , $ configValue , ScopeInterface::SCOPE_WEBSITES , $ websiteID );
155
+ }
156
+ }
157
+
158
+ foreach ($ this ->encryptedConfigFieldsToMigrate as $ field ) {
159
+ $ configValue = $ this ->getOldConfigValue ($ field , ScopeInterface::SCOPE_WEBSITES , $ websiteID );
160
+
161
+ if (!empty ($ configValue )) {
162
+ $ this ->saveNewConfigValue ($ field , $ configValue , ScopeInterface::SCOPE_WEBSITES , $ websiteID , true );
163
+ }
164
+ }
165
+ }
116
166
}
117
167
118
168
/**
119
169
* Get old configuration value from the DirectPost module's configuration on the store scope
120
170
*
121
171
* @param string $field
172
+ * @param string $scope
173
+ * @param int $scopeID
122
174
* @return mixed
123
175
*/
124
- private function getOldConfigValue (string $ field )
125
- {
176
+ private function getOldConfigValue (
177
+ string $ field ,
178
+ string $ scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
179
+ int $ scopeID = null
180
+ ) {
126
181
return $ this ->scopeConfig ->getValue (
127
182
sprintf (self ::PAYMENT_PATH_FORMAT , self ::DIRECTPOST_PATH , $ field ),
128
- ScopeInterface::SCOPE_STORE
183
+ $ scope ,
184
+ $ scopeID
129
185
);
130
186
}
131
187
@@ -134,15 +190,25 @@ private function getOldConfigValue(string $field)
134
190
*
135
191
* @param string $field
136
192
* @param mixed $value
193
+ * @param string $scope
194
+ * @param int $scopeID
137
195
* @param bool $isEncrypted
196
+ * @return void
138
197
*/
139
- private function saveNewConfigValue (string $ field , $ value , $ isEncrypted = false ): void
140
- {
198
+ private function saveNewConfigValue (
199
+ string $ field ,
200
+ $ value ,
201
+ string $ scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
202
+ int $ scopeID = 0 ,
203
+ bool $ isEncrypted = false
204
+ ): void {
141
205
$ value = $ isEncrypted ? $ this ->encryptor ->encrypt ($ value ) : $ value ;
206
+
142
207
$ this ->resourceConfig ->saveConfig (
143
208
sprintf (self ::PAYMENT_PATH_FORMAT , self ::ACCEPTJS_PATH , $ field ),
144
209
$ value ,
145
- ScopeInterface::SCOPE_STORE
210
+ $ scope ,
211
+ $ scopeID
146
212
);
147
213
}
148
214
0 commit comments