Skip to content

Commit 49a3076

Browse files
⏫ Forwardport of #11809 to 2.3-develop branch
Applied pull request patch https://github.com/magento/magento2/pull/11809.patch (created by @nmalevanec) based on commit(s): 1. 38735ac 2. b63ceb9 3. 3e0c785 Fixed GitHub Issues in 2.3-develop branch: - #8003: Using System Value for Base Currency Results in Config Error (reported by @tsmith1985)
1 parent 8e77e2f commit 49a3076

File tree

7 files changed

+696
-6
lines changed

7 files changed

+696
-6
lines changed

app/code/Magento/Directory/Model/Currency.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Directory\Model;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\Exception\InputException;
1011
use Magento\Directory\Model\Currency\Filter;
1112

@@ -65,6 +66,11 @@ class Currency extends \Magento\Framework\Model\AbstractModel
6566
*/
6667
protected $_localeCurrency;
6768

69+
/**
70+
* @var CurrencyConfig
71+
*/
72+
private $currencyConfig;
73+
6874
/**
6975
* @param \Magento\Framework\Model\Context $context
7076
* @param \Magento\Framework\Registry $registry
@@ -76,6 +82,7 @@ class Currency extends \Magento\Framework\Model\AbstractModel
7682
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
7783
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
7884
* @param array $data
85+
* @param CurrencyConfig $currencyConfig
7986
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8087
*/
8188
public function __construct(
@@ -88,7 +95,8 @@ public function __construct(
8895
\Magento\Framework\Locale\CurrencyInterface $localeCurrency,
8996
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
9097
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
91-
array $data = []
98+
array $data = [],
99+
CurrencyConfig $currencyConfig = null
92100
) {
93101
parent::__construct(
94102
$context,
@@ -102,6 +110,7 @@ public function __construct(
102110
$this->_directoryHelper = $directoryHelper;
103111
$this->_currencyFilterFactory = $currencyFilterFactory;
104112
$this->_localeCurrency = $localeCurrency;
113+
$this->currencyConfig = $currencyConfig ?: ObjectManager::getInstance()->get(CurrencyConfig::class);
105114
}
106115

107116
/**
@@ -347,7 +356,7 @@ public function getOutputFormat()
347356
*/
348357
public function getConfigAllowCurrencies()
349358
{
350-
$allowedCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_ALLOW);
359+
$allowedCurrencies = $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_ALLOW);
351360
$appBaseCurrencyCode = $this->_directoryHelper->getBaseCurrencyCode();
352361
if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
353362
$allowedCurrencies[] = $appBaseCurrencyCode;
@@ -369,17 +378,15 @@ public function getConfigAllowCurrencies()
369378
*/
370379
public function getConfigDefaultCurrencies()
371380
{
372-
$defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_DEFAULT);
373-
return $defaultCurrencies;
381+
return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_DEFAULT);
374382
}
375383

376384
/**
377385
* @return array
378386
*/
379387
public function getConfigBaseCurrencies()
380388
{
381-
$defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_BASE);
382-
return $defaultCurrencies;
389+
return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_BASE);
383390
}
384391

385392
/**
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Directory\Model;
8+
9+
use Magento\Framework\App\Area;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\State;
12+
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
15+
/**
16+
* Provide config values for allowed, base and default currencies.
17+
*/
18+
class CurrencyConfig
19+
{
20+
/**
21+
* @var State
22+
*/
23+
private $appState;
24+
25+
/**
26+
* @var ScopeConfigInterface
27+
*/
28+
private $config;
29+
30+
/**
31+
* @var StoreManagerInterface
32+
*/
33+
private $storeManager;
34+
35+
/**
36+
* CurrencyConfig constructor.
37+
*
38+
* @param State $appState
39+
* @param ScopeConfigInterface $config
40+
* @param StoreManagerInterface $storeManager
41+
*/
42+
public function __construct(
43+
State $appState,
44+
ScopeConfigInterface $config,
45+
StoreManagerInterface $storeManager
46+
) {
47+
$this->appState = $appState;
48+
$this->config = $config;
49+
$this->storeManager = $storeManager;
50+
}
51+
52+
/**
53+
* Retrieve config currency data by config path.
54+
*
55+
* @param string $path
56+
* @return array
57+
*/
58+
public function getConfigCurrencies(string $path)
59+
{
60+
$result = $this->appState->getAreaCode() === Area::AREA_ADMINHTML
61+
? $this->getConfigForAllStores($path)
62+
: $this->getConfigForCurrentStore($path);
63+
sort($result);
64+
65+
return array_unique($result);
66+
}
67+
68+
/**
69+
* Get allowed, base and default currency codes for all stores.
70+
*
71+
* @param string $path
72+
* @return array
73+
*/
74+
private function getConfigForAllStores(string $path)
75+
{
76+
$storesResult = [[]];
77+
foreach ($this->storeManager->getStores() as $store) {
78+
$storesResult[] = explode(
79+
',',
80+
$this->config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode())
81+
);
82+
}
83+
84+
return array_merge(...$storesResult);
85+
}
86+
87+
/**
88+
* Get allowed, base and default currency codes for current store.
89+
*
90+
* @param string $path
91+
* @return mixed
92+
*/
93+
private function getConfigForCurrentStore(string $path)
94+
{
95+
$store = $this->storeManager->getStore();
96+
97+
return explode(',', $this->config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode()));
98+
}
99+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Directory\Model;
8+
9+
use Magento\Config\App\Config\Type\System;
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
/**
14+
* Provide system config values for allowed, base and default currencies.
15+
*/
16+
class CurrencySystemConfig
17+
{
18+
/**
19+
* @var System
20+
*/
21+
private $systemConfig;
22+
23+
/**
24+
* @var StoreManagerInterface
25+
*/
26+
private $storeManager;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $path;
32+
33+
/**
34+
* Currency constructor.
35+
*
36+
* @param System $systemConfig
37+
* @param StoreManagerInterface $storeManager
38+
*/
39+
public function __construct(
40+
System $systemConfig,
41+
StoreManagerInterface $storeManager,
42+
ResourceConnection $resources
43+
) {
44+
$this->systemConfig = $systemConfig;
45+
$this->storeManager = $storeManager;
46+
}
47+
48+
/**
49+
* Retrieve config currency data by config path.
50+
*
51+
* @param string $path
52+
* @return array
53+
*/
54+
public function getConfigCurrencies(string $path)
55+
{
56+
$this->path = $path;
57+
$result = array_merge(
58+
$this->getDefaultConfigCurrencies(),
59+
$this->getWebsiteConfigCurrencies(),
60+
$this->getStoreConfigCurrencies()
61+
);
62+
sort($result);
63+
64+
return array_unique($result);
65+
}
66+
67+
/**
68+
* Get system config values as array for default scope.
69+
*
70+
* @return array
71+
*/
72+
private function getDefaultConfigCurrencies()
73+
{
74+
return $this->getConfig($this->path, 'default');
75+
}
76+
77+
/**
78+
* Get system config values as array for website scope.
79+
*
80+
* @return array
81+
*/
82+
private function getWebsiteConfigCurrencies()
83+
{
84+
$websiteResult = [[]];
85+
foreach ($this->storeManager->getWebsites() as $website) {
86+
$websiteResult[] = $this->getConfig($this->path, 'websites', $website->getId());
87+
}
88+
$websiteResult = array_merge(...$websiteResult);
89+
90+
return $websiteResult;
91+
}
92+
93+
/**
94+
* Get system config values as array for store scope.
95+
*
96+
* @return array
97+
*/
98+
private function getStoreConfigCurrencies()
99+
{
100+
$storeResult = [[]];
101+
foreach ($this->storeManager->getStores() as $store) {
102+
$storeResult[] = $this->getConfig($this->path, 'stores', $store->getId());
103+
}
104+
$storeResult = array_merge(...$storeResult);
105+
106+
return $storeResult;
107+
}
108+
109+
/**
110+
* Get system config values as array for specified scope.
111+
*
112+
* @param string $scope
113+
* @param string $scopeId
114+
* @param string $path
115+
* @return array
116+
*/
117+
private function getConfig(string $path, string $scope, string $scopeId = null)
118+
{
119+
$configPath = $scopeId ? sprintf('%s/%s/%s', $scope, $scopeId, $path) : sprintf('%s/%s', $scope, $path);
120+
121+
return explode(',', $this->systemConfig->get($configPath));
122+
}
123+
}

app/code/Magento/Directory/Model/ResourceModel/Currency.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ public function saveRates($rates)
165165
* @param string $path
166166
* @return array
167167
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
168+
* @deprecated because doesn't take into consideration scopes and system config values.
169+
* @see \Magento\Directory\Model\CurrencyConfig::getConfigCurrencies()
168170
*/
169171
public function getConfigCurrencies($model, $path)
170172
{

0 commit comments

Comments
 (0)