ContextPlugin.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Model\App\Action;
  7. /**
  8. * Class ContextPlugin
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class ContextPlugin
  12. {
  13. /**
  14. * @var \Magento\Customer\Model\Session
  15. */
  16. protected $customerSession;
  17. /**
  18. * @var \Magento\Framework\App\Http\Context
  19. */
  20. protected $httpContext;
  21. /**
  22. * @var \Magento\Tax\Helper\Data
  23. */
  24. protected $taxHelper;
  25. /**
  26. * @var \Magento\Weee\Helper\Data
  27. */
  28. protected $weeeHelper;
  29. /**
  30. * @var \Magento\Framework\Module\Manager
  31. */
  32. protected $moduleManager;
  33. /**
  34. * @var \Magento\Weee\Model\Tax
  35. */
  36. protected $weeeTax;
  37. /**
  38. * @var \Magento\PageCache\Model\Config
  39. */
  40. protected $cacheConfig;
  41. /**
  42. * @var \Magento\Store\Model\StoreManagerInterface
  43. */
  44. protected $storeManager;
  45. /**
  46. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  47. */
  48. protected $scopeConfig;
  49. /**
  50. * @param \Magento\Customer\Model\Session $customerSession
  51. * @param \Magento\Framework\App\Http\Context $httpContext
  52. * @param \Magento\Weee\Model\Tax $weeeTax
  53. * @param \Magento\Tax\Helper\Data $taxHelper
  54. * @param \Magento\Weee\Helper\Data $weeeHelper
  55. * @param \Magento\Framework\Module\Manager $moduleManager
  56. * @param \Magento\PageCache\Model\Config $cacheConfig
  57. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  58. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  59. */
  60. public function __construct(
  61. \Magento\Customer\Model\Session $customerSession,
  62. \Magento\Framework\App\Http\Context $httpContext,
  63. \Magento\Weee\Model\Tax $weeeTax,
  64. \Magento\Tax\Helper\Data $taxHelper,
  65. \Magento\Weee\Helper\Data $weeeHelper,
  66. \Magento\Framework\Module\Manager $moduleManager,
  67. \Magento\PageCache\Model\Config $cacheConfig,
  68. \Magento\Store\Model\StoreManagerInterface $storeManager,
  69. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  70. ) {
  71. $this->customerSession = $customerSession;
  72. $this->httpContext = $httpContext;
  73. $this->weeeTax = $weeeTax;
  74. $this->taxHelper = $taxHelper;
  75. $this->weeeHelper = $weeeHelper;
  76. $this->moduleManager = $moduleManager;
  77. $this->cacheConfig = $cacheConfig;
  78. $this->storeManager = $storeManager;
  79. $this->scopeConfig = $scopeConfig;
  80. }
  81. /**
  82. * @param \Magento\Framework\App\ActionInterface $subject
  83. * @param \Magento\Framework\App\RequestInterface $request
  84. * @return void
  85. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  86. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  87. * @SuppressWarnings(PHPMD.NPathComplexity)
  88. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  89. */
  90. public function beforeDispatch(
  91. \Magento\Framework\App\ActionInterface $subject,
  92. \Magento\Framework\App\RequestInterface $request
  93. ) {
  94. if (!$this->weeeHelper->isEnabled() ||
  95. !$this->customerSession->isLoggedIn() ||
  96. !$this->moduleManager->isEnabled('Magento_PageCache') ||
  97. !$this->cacheConfig->isEnabled()) {
  98. return;
  99. }
  100. $basedOn = $this->taxHelper->getTaxBasedOn();
  101. if ($basedOn != 'shipping' && $basedOn != 'billing') {
  102. return;
  103. }
  104. $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn);
  105. $websiteId = $this->storeManager->getStore()->getWebsiteId();
  106. $countryId = $weeeTaxRegion['countryId'];
  107. $regionId = $weeeTaxRegion['regionId'];
  108. if (!$countryId && !$regionId) {
  109. // country and region does not exist
  110. return;
  111. } elseif ($countryId && !$regionId) {
  112. // country exist and region does not exist
  113. $regionId = 0;
  114. $exist = $this->weeeTax->isWeeeInLocation(
  115. $countryId,
  116. $regionId,
  117. $websiteId
  118. );
  119. } else {
  120. // country and region exist
  121. $exist = $this->weeeTax->isWeeeInLocation(
  122. $countryId,
  123. $regionId,
  124. $websiteId
  125. );
  126. if (!$exist) {
  127. // just check the country for weee
  128. $regionId = 0;
  129. $exist = $this->weeeTax->isWeeeInLocation(
  130. $countryId,
  131. $regionId,
  132. $websiteId
  133. );
  134. }
  135. }
  136. if ($exist) {
  137. $this->httpContext->setValue(
  138. 'weee_tax_region',
  139. ['countryId' => $countryId, 'regionId' => $regionId],
  140. 0
  141. );
  142. }
  143. }
  144. /**
  145. * @param string $basedOn
  146. * @return array
  147. */
  148. protected function getWeeeTaxRegion($basedOn)
  149. {
  150. $countryId = null;
  151. $regionId = null;
  152. $defaultCountryId = $this->scopeConfig->getValue(
  153. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
  154. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  155. null
  156. );
  157. $defaultRegionId = $this->scopeConfig->getValue(
  158. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION,
  159. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  160. null
  161. );
  162. if ($basedOn == 'shipping') {
  163. $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress();
  164. if (empty($defaultShippingAddress)) {
  165. $countryId = $defaultCountryId;
  166. $regionId = $defaultRegionId;
  167. } else {
  168. $countryId = $defaultShippingAddress['country_id'];
  169. $regionId = $defaultShippingAddress['region_id'];
  170. }
  171. } elseif ($basedOn == 'billing') {
  172. $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress();
  173. if (empty($defaultBillingAddress)) {
  174. $countryId = $defaultCountryId;
  175. $regionId = $defaultRegionId;
  176. } else {
  177. $countryId = $defaultBillingAddress['country_id'];
  178. $regionId = $defaultBillingAddress['region_id'];
  179. }
  180. }
  181. return ['countryId' => $countryId, 'regionId' => $regionId];
  182. }
  183. }