AllowedCountries.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Store\Model\ScopeInterface;
  9. use Magento\Store\Model\StoreManagerInterface;
  10. /**
  11. * Provider of allowed countries based on configuration settings
  12. *
  13. * @api
  14. * @since 100.1.2
  15. */
  16. class AllowedCountries
  17. {
  18. const ALLOWED_COUNTRIES_PATH = 'general/country/allow';
  19. /**
  20. * @var ScopeConfigInterface
  21. */
  22. private $scopeConfig;
  23. /**
  24. * @var StoreManagerInterface
  25. */
  26. private $storeManager;
  27. /**
  28. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  29. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  30. * @return void
  31. */
  32. public function __construct(
  33. ScopeConfigInterface $scopeConfig,
  34. StoreManagerInterface $storeManager
  35. ) {
  36. $this->scopeConfig = $scopeConfig;
  37. $this->storeManager = $storeManager;
  38. }
  39. /**
  40. * Retrieve all allowed countries for scope or scopes
  41. *
  42. * @param string $scope
  43. * @param string|null $scopeCode
  44. * @return array
  45. * @since 100.1.2
  46. */
  47. public function getAllowedCountries(
  48. $scope = ScopeInterface::SCOPE_WEBSITE,
  49. $scopeCode = null
  50. ) {
  51. if ($scopeCode === null) {
  52. $scopeCode = $this->getDefaultScopeCode($scope);
  53. }
  54. switch ($scope) {
  55. case ScopeInterface::SCOPE_WEBSITES:
  56. case ScopeInterface::SCOPE_STORES:
  57. $allowedCountries = [];
  58. foreach ($scopeCode as $singleFilter) {
  59. $allowedCountries = array_merge(
  60. $allowedCountries,
  61. $this->getCountriesFromConfig($this->getSingleScope($scope), $singleFilter)
  62. );
  63. }
  64. break;
  65. default:
  66. $allowedCountries = $this->getCountriesFromConfig($scope, $scopeCode);
  67. }
  68. return $this->makeCountriesUnique($allowedCountries);
  69. }
  70. /**
  71. * Resolve scope code by scope
  72. *
  73. * @throws \InvalidArgumentException
  74. * @param string $scope
  75. * @return array|int
  76. */
  77. private function getDefaultScopeCode($scope)
  78. {
  79. switch ($scope) {
  80. case ScopeInterface::SCOPE_WEBSITE:
  81. return $this->storeManager->getWebsite()->getId();
  82. case ScopeInterface::SCOPE_STORE:
  83. return $this->storeManager->getStore()->getId();
  84. case ScopeInterface::SCOPE_GROUP:
  85. return $this->storeManager->getGroup()->getId();
  86. case ScopeInterface::SCOPE_WEBSITES:
  87. return [$this->storeManager->getWebsite()->getId()];
  88. case ScopeInterface::SCOPE_STORES:
  89. return [$this->storeManager->getStore()->getId()];
  90. default:
  91. throw new \InvalidArgumentException("Invalid scope is specified");
  92. }
  93. }
  94. /**
  95. * Return Unique Countries by merging them by keys
  96. *
  97. * @param array $allowedCountries
  98. * @return array
  99. * @since 100.1.2
  100. */
  101. public function makeCountriesUnique(array $allowedCountries)
  102. {
  103. return array_combine($allowedCountries, $allowedCountries);
  104. }
  105. /**
  106. * Takes countries from Countries Config data
  107. *
  108. * @param string $scope
  109. * @param int $scopeCode
  110. * @return array
  111. * @since 100.1.2
  112. */
  113. public function getCountriesFromConfig($scope, $scopeCode)
  114. {
  115. return explode(
  116. ',',
  117. (string) $this->scopeConfig->getValue(
  118. self::ALLOWED_COUNTRIES_PATH,
  119. $scope,
  120. $scopeCode
  121. )
  122. );
  123. }
  124. /**
  125. * Return Single Scope
  126. *
  127. * @param string $scope
  128. * @return string
  129. */
  130. private function getSingleScope($scope)
  131. {
  132. if ($scope == ScopeInterface::SCOPE_WEBSITES) {
  133. return ScopeInterface::SCOPE_WEBSITE;
  134. }
  135. if ($scope == ScopeInterface::SCOPE_STORES) {
  136. return ScopeInterface::SCOPE_STORE;
  137. }
  138. return $scope;
  139. }
  140. }