Skip to content

Commit

Permalink
⏫ Forwardport of #11809 to 2.3-develop branch
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
magento-engcom-team committed Jan 24, 2018
1 parent 8e77e2f commit 49a3076
Show file tree
Hide file tree
Showing 7 changed files with 696 additions and 6 deletions.
19 changes: 13 additions & 6 deletions app/code/Magento/Directory/Model/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\Directory\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\InputException;
use Magento\Directory\Model\Currency\Filter;

Expand Down Expand Up @@ -65,6 +66,11 @@ class Currency extends \Magento\Framework\Model\AbstractModel
*/
protected $_localeCurrency;

/**
* @var CurrencyConfig
*/
private $currencyConfig;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -76,6 +82,7 @@ class Currency extends \Magento\Framework\Model\AbstractModel
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param CurrencyConfig $currencyConfig
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -88,7 +95,8 @@ public function __construct(
\Magento\Framework\Locale\CurrencyInterface $localeCurrency,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
CurrencyConfig $currencyConfig = null
) {
parent::__construct(
$context,
Expand All @@ -102,6 +110,7 @@ public function __construct(
$this->_directoryHelper = $directoryHelper;
$this->_currencyFilterFactory = $currencyFilterFactory;
$this->_localeCurrency = $localeCurrency;
$this->currencyConfig = $currencyConfig ?: ObjectManager::getInstance()->get(CurrencyConfig::class);
}

/**
Expand Down Expand Up @@ -347,7 +356,7 @@ public function getOutputFormat()
*/
public function getConfigAllowCurrencies()
{
$allowedCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_ALLOW);
$allowedCurrencies = $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_ALLOW);
$appBaseCurrencyCode = $this->_directoryHelper->getBaseCurrencyCode();
if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
$allowedCurrencies[] = $appBaseCurrencyCode;
Expand All @@ -369,17 +378,15 @@ public function getConfigAllowCurrencies()
*/
public function getConfigDefaultCurrencies()
{
$defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_DEFAULT);
return $defaultCurrencies;
return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_DEFAULT);
}

/**
* @return array
*/
public function getConfigBaseCurrencies()
{
$defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_BASE);
return $defaultCurrencies;
return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_BASE);
}

/**
Expand Down
99 changes: 99 additions & 0 deletions app/code/Magento/Directory/Model/CurrencyConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Directory\Model;

use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Provide config values for allowed, base and default currencies.
*/
class CurrencyConfig
{
/**
* @var State
*/
private $appState;

/**
* @var ScopeConfigInterface
*/
private $config;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* CurrencyConfig constructor.
*
* @param State $appState
* @param ScopeConfigInterface $config
* @param StoreManagerInterface $storeManager
*/
public function __construct(
State $appState,
ScopeConfigInterface $config,
StoreManagerInterface $storeManager
) {
$this->appState = $appState;
$this->config = $config;
$this->storeManager = $storeManager;
}

/**
* Retrieve config currency data by config path.
*
* @param string $path
* @return array
*/
public function getConfigCurrencies(string $path)
{
$result = $this->appState->getAreaCode() === Area::AREA_ADMINHTML
? $this->getConfigForAllStores($path)
: $this->getConfigForCurrentStore($path);
sort($result);

return array_unique($result);
}

/**
* Get allowed, base and default currency codes for all stores.
*
* @param string $path
* @return array
*/
private function getConfigForAllStores(string $path)
{
$storesResult = [[]];
foreach ($this->storeManager->getStores() as $store) {
$storesResult[] = explode(
',',
$this->config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode())
);
}

return array_merge(...$storesResult);
}

/**
* Get allowed, base and default currency codes for current store.
*
* @param string $path
* @return mixed
*/
private function getConfigForCurrentStore(string $path)
{
$store = $this->storeManager->getStore();

return explode(',', $this->config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode()));
}
}
123 changes: 123 additions & 0 deletions app/code/Magento/Directory/Model/CurrencySystemConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Directory\Model;

use Magento\Config\App\Config\Type\System;
use Magento\Framework\App\ResourceConnection;
use Magento\Store\Model\StoreManagerInterface;

/**
* Provide system config values for allowed, base and default currencies.
*/
class CurrencySystemConfig
{
/**
* @var System
*/
private $systemConfig;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var string
*/
private $path;

/**
* Currency constructor.
*
* @param System $systemConfig
* @param StoreManagerInterface $storeManager
*/
public function __construct(
System $systemConfig,
StoreManagerInterface $storeManager,
ResourceConnection $resources
) {
$this->systemConfig = $systemConfig;
$this->storeManager = $storeManager;
}

/**
* Retrieve config currency data by config path.
*
* @param string $path
* @return array
*/
public function getConfigCurrencies(string $path)
{
$this->path = $path;
$result = array_merge(
$this->getDefaultConfigCurrencies(),
$this->getWebsiteConfigCurrencies(),
$this->getStoreConfigCurrencies()
);
sort($result);

return array_unique($result);
}

/**
* Get system config values as array for default scope.
*
* @return array
*/
private function getDefaultConfigCurrencies()
{
return $this->getConfig($this->path, 'default');
}

/**
* Get system config values as array for website scope.
*
* @return array
*/
private function getWebsiteConfigCurrencies()
{
$websiteResult = [[]];
foreach ($this->storeManager->getWebsites() as $website) {
$websiteResult[] = $this->getConfig($this->path, 'websites', $website->getId());
}
$websiteResult = array_merge(...$websiteResult);

return $websiteResult;
}

/**
* Get system config values as array for store scope.
*
* @return array
*/
private function getStoreConfigCurrencies()
{
$storeResult = [[]];
foreach ($this->storeManager->getStores() as $store) {
$storeResult[] = $this->getConfig($this->path, 'stores', $store->getId());
}
$storeResult = array_merge(...$storeResult);

return $storeResult;
}

/**
* Get system config values as array for specified scope.
*
* @param string $scope
* @param string $scopeId
* @param string $path
* @return array
*/
private function getConfig(string $path, string $scope, string $scopeId = null)
{
$configPath = $scopeId ? sprintf('%s/%s/%s', $scope, $scopeId, $path) : sprintf('%s/%s', $scope, $path);

return explode(',', $this->systemConfig->get($configPath));
}
}
2 changes: 2 additions & 0 deletions app/code/Magento/Directory/Model/ResourceModel/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public function saveRates($rates)
* @param string $path
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @deprecated because doesn't take into consideration scopes and system config values.
* @see \Magento\Directory\Model\CurrencyConfig::getConfigCurrencies()
*/
public function getConfigCurrencies($model, $path)
{
Expand Down
Loading

0 comments on commit 49a3076

Please sign in to comment.