StructurePlugin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\Config;
  7. use Magento\Config\Model\Config\ScopeDefiner;
  8. use Magento\Config\Model\Config\Structure;
  9. use Magento\Config\Model\Config\Structure\Element\Section;
  10. use Magento\Config\Model\Config\Structure\ElementInterface;
  11. use Magento\Paypal\Helper\Backend as BackendHelper;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Paypal\Model\Config\Structure\PaymentSectionModifier;
  14. /**
  15. * Plugin for \Magento\Config\Model\Config\Structure
  16. */
  17. class StructurePlugin
  18. {
  19. /**
  20. * Request parameter name
  21. */
  22. const REQUEST_PARAM_COUNTRY = 'paypal_country';
  23. /**
  24. * @var BackendHelper
  25. */
  26. private $backendHelper;
  27. /**
  28. * @var ScopeDefiner
  29. */
  30. private $scopeDefiner;
  31. /**
  32. * @var PaymentSectionModifier
  33. */
  34. private $paymentSectionModifier;
  35. /**
  36. * @var string[]
  37. */
  38. private static $paypalConfigCountries = [
  39. 'payment_us',
  40. 'payment_ca',
  41. 'payment_au',
  42. 'payment_gb',
  43. 'payment_jp',
  44. 'payment_fr',
  45. 'payment_it',
  46. 'payment_es',
  47. 'payment_hk',
  48. 'payment_nz',
  49. 'payment_de',
  50. ];
  51. /**
  52. * @param ScopeDefiner $scopeDefiner
  53. * @param BackendHelper $backendHelper
  54. * @param PaymentSectionModifier|null $paymentSectionModifier
  55. */
  56. public function __construct(
  57. ScopeDefiner $scopeDefiner,
  58. BackendHelper $backendHelper,
  59. PaymentSectionModifier $paymentSectionModifier = null
  60. ) {
  61. $this->scopeDefiner = $scopeDefiner;
  62. $this->backendHelper = $backendHelper;
  63. $this->paymentSectionModifier = $paymentSectionModifier
  64. ?: ObjectManager::getInstance()->get(PaymentSectionModifier::class);
  65. }
  66. /**
  67. * Get paypal configuration countries
  68. *
  69. * @param bool $addOther
  70. * @return string[]
  71. */
  72. public static function getPaypalConfigCountries($addOther = false)
  73. {
  74. $countries = self::$paypalConfigCountries;
  75. if ($addOther) {
  76. $countries[] = 'payment_other';
  77. }
  78. return $countries;
  79. }
  80. /**
  81. * Substitute payment section with PayPal configs
  82. *
  83. * @param Structure $subject
  84. * @param \Closure $proceed
  85. * @param array $pathParts
  86. * @return ElementInterface|null
  87. *
  88. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  89. */
  90. public function aroundGetElementByPathParts(Structure $subject, \Closure $proceed, array $pathParts)
  91. {
  92. $isSectionChanged = $pathParts[0] == 'payment';
  93. if ($isSectionChanged) {
  94. $requestedCountrySection = 'payment_' . strtolower($this->backendHelper->getConfigurationCountryCode());
  95. if (in_array($requestedCountrySection, self::getPaypalConfigCountries())) {
  96. $pathParts[0] = $requestedCountrySection;
  97. } else {
  98. $pathParts[0] = 'payment_other';
  99. }
  100. }
  101. $result = $proceed($pathParts);
  102. if ($isSectionChanged && $result) {
  103. if ($result instanceof Section) {
  104. $this->restructurePayments($result);
  105. $result->setData(
  106. array_merge(
  107. $result->getData(),
  108. ['showInDefault' => true, 'showInWebsite' => true, 'showInStore' => true]
  109. ),
  110. $this->scopeDefiner->getScope()
  111. );
  112. }
  113. }
  114. return $result;
  115. }
  116. /**
  117. * Changes payment config structure.
  118. *
  119. * @param Section $result
  120. * @return void
  121. */
  122. private function restructurePayments(Section $result)
  123. {
  124. $sectionData = $result->getData();
  125. $sectionInitialStructure = isset($sectionData['children']) ? $sectionData['children'] : [];
  126. $sectionChangedStructure = $this->paymentSectionModifier->modify($sectionInitialStructure);
  127. $sectionData['children'] = $sectionChangedStructure;
  128. $result->setData($sectionData, $this->scopeDefiner->getScope());
  129. }
  130. }