Skip to content

Commit f188594

Browse files
Merge remote-tracking branch 'origin/MAGETWO-62667' into pr-mpi-bugfix-ce
2 parents a041d35 + 20f485e commit f188594

File tree

5 files changed

+451
-56
lines changed

5 files changed

+451
-56
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Paypal\Model\Config\Structure;
7+
8+
/**
9+
* PayPal change structure of payment methods configuration in admin panel.
10+
*/
11+
class PaymentSectionModifier
12+
{
13+
/**
14+
* Identifiers of special payment method configuration groups
15+
*
16+
* @var array
17+
*/
18+
private static $specialGroups = [
19+
'account',
20+
'recommended_solutions',
21+
'other_paypal_payment_solutions',
22+
'other_payment_methods',
23+
];
24+
25+
/**
26+
* Returns changed section structure.
27+
*
28+
* Payment configuration has predefined special blocks:
29+
* - Account information (id = account),
30+
* - Recommended Solutions (id = recommended_solutions),
31+
* - Other PayPal paymnt solution (id = other_paypal_payment_solutions),
32+
* - Other payment methods (id = other_payment_methods).
33+
* All payment methods configuration should be moved to one of this group.
34+
* To move payment method to specific configuration group specify "displayIn"
35+
* attribute in system.xml file equals to any id of predefined special group.
36+
* If "displayIn" attribute is not specified then payment method moved to "Other payment methods" group
37+
*
38+
* @param array $initialStructure
39+
* @return array
40+
*/
41+
public function modify(array $initialStructure)
42+
{
43+
$changedStructure = array_fill_keys(self::$specialGroups, []);
44+
45+
foreach ($initialStructure as $childSection => $childData) {
46+
if (in_array($childSection, self::$specialGroups)) {
47+
if (isset($changedStructure[$childSection]['children'])) {
48+
$children = $changedStructure[$childSection]['children'];
49+
if (isset($childData['children'])) {
50+
$children += $childData['children'];
51+
}
52+
$childData['children'] = $children;
53+
unset($children);
54+
}
55+
$changedStructure[$childSection] = $childData;
56+
} else {
57+
$moveInstructions = $this->getMoveInstructions($childSection, $childData);
58+
if (!empty($moveInstructions)) {
59+
foreach ($moveInstructions as $moveInstruction) {
60+
unset($childData['children'][$moveInstruction['section']]);
61+
unset($moveInstruction['data']['displayIn']);
62+
$changedStructure
63+
[$moveInstruction['parent']]
64+
['children']
65+
[$moveInstruction['section']] = $moveInstruction['data'];
66+
}
67+
}
68+
if (!isset($moveInstructions[$childSection])) {
69+
$changedStructure['other_payment_methods']['children'][$childSection] = $childData;
70+
}
71+
}
72+
}
73+
74+
return $changedStructure;
75+
}
76+
77+
/**
78+
* Recursively collect groups that should be moved to special section
79+
*
80+
* @param string $section
81+
* @param array $data
82+
* @return array
83+
*/
84+
private function getMoveInstructions($section, $data)
85+
{
86+
$moved = [];
87+
88+
if (array_key_exists('children', $data)) {
89+
foreach ($data['children'] as $childSection => $childData) {
90+
$movedChildren = $this->getMoveInstructions($childSection, $childData);
91+
if (isset($movedChildren[$childSection])) {
92+
unset($data['children'][$childSection]);
93+
}
94+
$moved = array_merge($moved, $movedChildren);
95+
}
96+
}
97+
98+
if (isset($data['displayIn']) && in_array($data['displayIn'], self::$specialGroups)) {
99+
$moved = array_merge(
100+
[
101+
$section => [
102+
'parent' => $data['displayIn'],
103+
'section' => $section,
104+
'data' => $data
105+
]
106+
],
107+
$moved
108+
);
109+
}
110+
111+
return $moved;
112+
}
113+
}

app/code/Magento/Paypal/Model/Config/StructurePlugin.php

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Config\Model\Config\Structure\Element\Section;
1111
use Magento\Config\Model\Config\Structure\ElementInterface;
1212
use Magento\Paypal\Helper\Backend as BackendHelper;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Paypal\Model\Config\Structure\PaymentSectionModifier;
1315

1416
/**
1517
* Plugin for \Magento\Config\Model\Config\Structure
@@ -31,6 +33,11 @@ class StructurePlugin
3133
*/
3234
private $scopeDefiner;
3335

36+
/**
37+
* @var PaymentSectionModifier
38+
*/
39+
private $paymentSectionModifier;
40+
3441
/**
3542
* @var string[]
3643
*/
@@ -50,12 +57,18 @@ class StructurePlugin
5057

5158
/**
5259
* @param ScopeDefiner $scopeDefiner
53-
* @param BackendHelper $helper
60+
* @param BackendHelper $backendHelper
61+
* @param PaymentSectionModifier|null $paymentSectionModifier
5462
*/
55-
public function __construct(ScopeDefiner $scopeDefiner, BackendHelper $helper)
56-
{
63+
public function __construct(
64+
ScopeDefiner $scopeDefiner,
65+
BackendHelper $backendHelper,
66+
PaymentSectionModifier $paymentSectionModifier = null
67+
) {
5768
$this->scopeDefiner = $scopeDefiner;
58-
$this->backendHelper = $helper;
69+
$this->backendHelper = $backendHelper;
70+
$this->paymentSectionModifier = $paymentSectionModifier
71+
?: ObjectManager::getInstance()->get(PaymentSectionModifier::class);
5972
}
6073

6174
/**
@@ -118,62 +131,17 @@ public function aroundGetElementByPathParts(Structure $subject, \Closure $procee
118131
}
119132

120133
/**
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.
125135
*
126136
* @param Section $result
127137
* @return void
128138
*/
129139
private function restructurePayments(Section $result)
130140
{
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());
178146
}
179147
}

0 commit comments

Comments
 (0)