Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
StructurePlugin.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Paypal\Helper\Backend as BackendHelper;
15 
20 {
24  const REQUEST_PARAM_COUNTRY = 'paypal_country';
25 
29  private $backendHelper;
30 
34  private $scopeDefiner;
35 
39  private $paymentSectionModifier;
40 
44  private static $paypalConfigCountries = [
45  'payment_us',
46  'payment_ca',
47  'payment_au',
48  'payment_gb',
49  'payment_jp',
50  'payment_fr',
51  'payment_it',
52  'payment_es',
53  'payment_hk',
54  'payment_nz',
55  'payment_de',
56  ];
57 
63  public function __construct(
64  ScopeDefiner $scopeDefiner,
65  BackendHelper $backendHelper,
66  PaymentSectionModifier $paymentSectionModifier = null
67  ) {
68  $this->scopeDefiner = $scopeDefiner;
69  $this->backendHelper = $backendHelper;
70  $this->paymentSectionModifier = $paymentSectionModifier
71  ?: ObjectManager::getInstance()->get(PaymentSectionModifier::class);
72  }
73 
80  public static function getPaypalConfigCountries($addOther = false)
81  {
82  $countries = self::$paypalConfigCountries;
83 
84  if ($addOther) {
85  $countries[] = 'payment_other';
86  }
87 
88  return $countries;
89  }
90 
101  public function aroundGetElementByPathParts(Structure $subject, \Closure $proceed, array $pathParts)
102  {
103  $isSectionChanged = $pathParts[0] == 'payment';
104 
105  if ($isSectionChanged) {
106  $requestedCountrySection = 'payment_' . strtolower($this->backendHelper->getConfigurationCountryCode());
107 
108  if (in_array($requestedCountrySection, self::getPaypalConfigCountries())) {
109  $pathParts[0] = $requestedCountrySection;
110  } else {
111  $pathParts[0] = 'payment_other';
112  }
113  }
114 
115  $result = $proceed($pathParts);
116 
117  if ($isSectionChanged && $result) {
118  if ($result instanceof Section) {
119  $this->restructurePayments($result);
120  $result->setData(
121  array_merge(
122  $result->getData(),
123  ['showInDefault' => true, 'showInWebsite' => true, 'showInStore' => true]
124  ),
125  $this->scopeDefiner->getScope()
126  );
127  }
128  }
129 
130  return $result;
131  }
132 
139  private function restructurePayments(Section $result)
140  {
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());
146  }
147 }
__construct(ScopeDefiner $scopeDefiner, BackendHelper $backendHelper, PaymentSectionModifier $paymentSectionModifier=null)
static getPaypalConfigCountries($addOther=false)
aroundGetElementByPathParts(Structure $subject, \Closure $proceed, array $pathParts)