10
10
use Magento \Config \Model \Config \Structure \Element \Section ;
11
11
use Magento \Config \Model \Config \Structure \ElementInterface ;
12
12
use Magento \Paypal \Helper \Backend as BackendHelper ;
13
+ use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Paypal \Model \Config \Structure \PaymentSectionModifier ;
13
15
14
16
/**
15
17
* Plugin for \Magento\Config\Model\Config\Structure
@@ -31,6 +33,11 @@ class StructurePlugin
31
33
*/
32
34
private $ scopeDefiner ;
33
35
36
+ /**
37
+ * @var PaymentSectionModifier
38
+ */
39
+ private $ paymentSectionModifier ;
40
+
34
41
/**
35
42
* @var string[]
36
43
*/
@@ -50,12 +57,18 @@ class StructurePlugin
50
57
51
58
/**
52
59
* @param ScopeDefiner $scopeDefiner
53
- * @param BackendHelper $helper
60
+ * @param BackendHelper $backendHelper
61
+ * @param PaymentSectionModifier|null $paymentSectionModifier
54
62
*/
55
- public function __construct (ScopeDefiner $ scopeDefiner , BackendHelper $ helper )
56
- {
63
+ public function __construct (
64
+ ScopeDefiner $ scopeDefiner ,
65
+ BackendHelper $ backendHelper ,
66
+ PaymentSectionModifier $ paymentSectionModifier = null
67
+ ) {
57
68
$ this ->scopeDefiner = $ scopeDefiner ;
58
- $ this ->backendHelper = $ helper ;
69
+ $ this ->backendHelper = $ backendHelper ;
70
+ $ this ->paymentSectionModifier = $ paymentSectionModifier
71
+ ?: ObjectManager::getInstance ()->get (PaymentSectionModifier::class);
59
72
}
60
73
61
74
/**
@@ -118,62 +131,17 @@ public function aroundGetElementByPathParts(Structure $subject, \Closure $procee
118
131
}
119
132
120
133
/**
121
- * Change payment config structure
122
- *
123
- * Groups which have `displayIn` element, transfer to appropriate group.
124
- * Groups without `displayIn` transfer to other payment methods group.
134
+ * Changes payment config structure.
125
135
*
126
136
* @param Section $result
127
137
* @return void
128
138
*/
129
139
private function restructurePayments (Section $ result )
130
140
{
131
- $ sectionMap = [
132
- 'account ' => [],
133
- 'recommended_solutions ' => [],
134
- 'other_paypal_payment_solutions ' => [],
135
- 'other_payment_methods ' => []
136
- ];
137
-
138
- $ configuration = $ result ->getData ();
139
-
140
- foreach ($ configuration ['children ' ] as $ section => $ data ) {
141
- if (array_key_exists ($ section , $ sectionMap )) {
142
- $ sectionMap [$ section ] = $ data ;
143
- } elseif ($ displayIn = $ this ->getDisplayInSection ($ section , $ data )) {
144
- $ sectionMap [$ displayIn ['parent ' ]]['children ' ][$ displayIn ['section ' ]] = $ displayIn ['data ' ];
145
- } else {
146
- $ sectionMap ['other_payment_methods ' ]['children ' ][$ section ] = $ data ;
147
- }
148
- }
149
-
150
- $ configuration ['children ' ] = $ sectionMap ;
151
- $ result ->setData ($ configuration , $ this ->scopeDefiner ->getScope ());
152
- }
153
-
154
- /**
155
- * Recursive search of `displayIn` element in node children
156
- *
157
- * @param string $section
158
- * @param array $data
159
- * @return array|null
160
- */
161
- private function getDisplayInSection ($ section , $ data )
162
- {
163
- if (is_array ($ data ) && array_key_exists ('displayIn ' , $ data )) {
164
- return [
165
- 'parent ' => $ data ['displayIn ' ],
166
- 'section ' => $ section ,
167
- 'data ' => $ data
168
- ];
169
- }
170
-
171
- if (array_key_exists ('children ' , $ data )) {
172
- foreach ($ data ['children ' ] as $ childSection => $ childData ) {
173
- return $ this ->getDisplayInSection ($ childSection , $ childData );
174
- }
175
- }
176
-
177
- return null ;
141
+ $ sectionData = $ result ->getData ();
142
+ $ sectionInitialStructure = isset ($ sectionData ['children ' ]) ? $ sectionData ['children ' ] : [];
143
+ $ sectionChangedStructure = $ this ->paymentSectionModifier ->modify ($ sectionInitialStructure );
144
+ $ sectionData ['children ' ] = $ sectionChangedStructure ;
145
+ $ result ->setData ($ sectionData , $ this ->scopeDefiner ->getScope ());
178
146
}
179
147
}
0 commit comments