Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit b63ceb9

Browse files
nmalevanecdmanners
authored andcommitted
8003: Using System Value for Base Currency Results in Config Error.
1 parent 38735ac commit b63ceb9

File tree

7 files changed

+368
-313
lines changed

7 files changed

+368
-313
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Currency extends \Magento\Framework\Model\AbstractModel
6767
protected $_localeCurrency;
6868

6969
/**
70-
* @var CurrencySystemConfig
70+
* @var CurrencyConfig
7171
*/
7272
private $currencyConfig;
7373

@@ -82,7 +82,7 @@ class Currency extends \Magento\Framework\Model\AbstractModel
8282
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
8383
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
8484
* @param array $data
85-
* @param CurrencySystemConfig|null $currencyConfig
85+
* @param CurrencyConfig|null $currencyConfig
8686
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8787
*/
8888
public function __construct(
@@ -96,7 +96,7 @@ public function __construct(
9696
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
9797
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
9898
array $data = [],
99-
CurrencySystemConfig $currencyConfig = null
99+
CurrencyConfig $currencyConfig = null
100100
) {
101101
parent::__construct(
102102
$context,
@@ -110,7 +110,7 @@ public function __construct(
110110
$this->_directoryHelper = $directoryHelper;
111111
$this->_currencyFilterFactory = $currencyFilterFactory;
112112
$this->_localeCurrency = $localeCurrency;
113-
$this->currencyConfig = $currencyConfig ?: ObjectManager::getInstance()->get(CurrencySystemConfig::class);
113+
$this->currencyConfig = $currencyConfig ?: ObjectManager::getInstance()->get(CurrencyConfig::class);
114114
}
115115

116116
/**
@@ -356,8 +356,7 @@ public function getOutputFormat()
356356
*/
357357
public function getConfigAllowCurrencies()
358358
{
359-
$allowedCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_ALLOW) ?:
360-
$this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_ALLOW);
359+
$allowedCurrencies = $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_ALLOW);
361360
$appBaseCurrencyCode = $this->_directoryHelper->getBaseCurrencyCode();
362361
if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
363362
$allowedCurrencies[] = $appBaseCurrencyCode;
@@ -379,20 +378,15 @@ public function getConfigAllowCurrencies()
379378
*/
380379
public function getConfigDefaultCurrencies()
381380
{
382-
$defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_DEFAULT) ?:
383-
$this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_DEFAULT);
384-
385-
return $defaultCurrencies;
381+
return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_DEFAULT);
386382
}
387383

388384
/**
389385
* @return array
390386
*/
391387
public function getConfigBaseCurrencies()
392388
{
393-
$defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_BASE) ?:
394-
$this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_BASE);
395-
return $defaultCurrencies;
389+
return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_BASE);
396390
}
397391

398392
/**
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+
}

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

Lines changed: 0 additions & 123 deletions
This file was deleted.

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)