CurrencyConfig.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Area;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\State;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. /**
  13. * Provide config values for allowed, base and default currencies.
  14. */
  15. class CurrencyConfig
  16. {
  17. /**
  18. * @var State
  19. */
  20. private $appState;
  21. /**
  22. * @var ScopeConfigInterface
  23. */
  24. private $config;
  25. /**
  26. * @var StoreManagerInterface
  27. */
  28. private $storeManager;
  29. /**
  30. * CurrencyConfig constructor.
  31. *
  32. * @param State $appState
  33. * @param ScopeConfigInterface $config
  34. * @param StoreManagerInterface $storeManager
  35. */
  36. public function __construct(
  37. State $appState,
  38. ScopeConfigInterface $config,
  39. StoreManagerInterface $storeManager
  40. ) {
  41. $this->appState = $appState;
  42. $this->config = $config;
  43. $this->storeManager = $storeManager;
  44. }
  45. /**
  46. * Retrieve config currency data by config path.
  47. *
  48. * @param string $path
  49. * @return array
  50. */
  51. public function getConfigCurrencies(string $path)
  52. {
  53. $result = in_array($this->appState->getAreaCode(), [Area::AREA_ADMINHTML, Area::AREA_CRONTAB])
  54. ? $this->getConfigForAllStores($path)
  55. : $this->getConfigForCurrentStore($path);
  56. sort($result);
  57. return array_unique($result);
  58. }
  59. /**
  60. * Get allowed, base and default currency codes for all stores.
  61. *
  62. * @param string $path
  63. * @return array
  64. */
  65. private function getConfigForAllStores(string $path)
  66. {
  67. $storesResult = [[]];
  68. foreach ($this->storeManager->getStores() as $store) {
  69. $storesResult[] = explode(
  70. ',',
  71. $this->config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode())
  72. );
  73. }
  74. return array_merge(...$storesResult);
  75. }
  76. /**
  77. * Get allowed, base and default currency codes for current store.
  78. *
  79. * @param string $path
  80. * @return mixed
  81. */
  82. private function getConfigForCurrentStore(string $path)
  83. {
  84. $store = $this->storeManager->getStore();
  85. return explode(',', $this->config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode()));
  86. }
  87. }