Skip to content

Commit 8bef8f5

Browse files
MAGETWO-66969: PreparedValueFactory issues
1 parent c7ad941 commit 8bef8f5

File tree

2 files changed

+205
-107
lines changed

2 files changed

+205
-107
lines changed

app/code/Magento/Config/Model/PreparedValueFactory.php

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
use Magento\Config\Model\Config\Structure;
99
use Magento\Config\Model\Config\StructureFactory;
10+
use Magento\Framework\App\Config\ValueInterface;
1011
use Magento\Framework\App\Config\Value;
1112
use Magento\Framework\App\Config\ValueFactory;
12-
use Magento\Framework\App\Config\ValueInterface;
13-
use Magento\Framework\App\DeploymentConfig;
13+
use Magento\Framework\App\ScopeInterface;
14+
use Magento\Framework\App\ScopeResolverPool;
15+
use Magento\Framework\Exception\RuntimeException;
1416

1517
/**
1618
* Creates a prepared instance of Value.
@@ -20,11 +22,11 @@
2022
class PreparedValueFactory
2123
{
2224
/**
23-
* The deployment configuration reader.
25+
* The scope resolver pool.
2426
*
25-
* @var DeploymentConfig
27+
* @var ScopeResolverPool
2628
*/
27-
private $deploymentConfig;
29+
private $scopeResolverPool;
2830

2931
/**
3032
* The manager for system configuration structure.
@@ -42,16 +44,16 @@ class PreparedValueFactory
4244
private $valueFactory;
4345

4446
/**
45-
* @param DeploymentConfig $deploymentConfig The deployment configuration reader
47+
* @param ScopeResolverPool $scopeResolverPool The scope resolver pool
4648
* @param StructureFactory $structureFactory The manager for system configuration structure
4749
* @param ValueFactory $valueFactory The factory for configuration value objects
4850
*/
4951
public function __construct(
50-
DeploymentConfig $deploymentConfig,
52+
ScopeResolverPool $scopeResolverPool,
5153
StructureFactory $structureFactory,
5254
ValueFactory $valueFactory
5355
) {
54-
$this->deploymentConfig = $deploymentConfig;
56+
$this->scopeResolverPool = $scopeResolverPool;
5557
$this->structureFactory = $structureFactory;
5658
$this->valueFactory = $valueFactory;
5759
}
@@ -62,31 +64,41 @@ public function __construct(
6264
* @param string $path The configuration path in format group/section/field_name
6365
* @param string $value The configuration value
6466
* @param string $scope The configuration scope (default, website, or store)
65-
* @param string $scopeCode The scope code
67+
* @param string|int|null $scopeCode The scope code
6668
* @return ValueInterface
69+
* @throws RuntimeException If Value can not be created
6770
* @see ValueInterface
6871
* @see Value
6972
*/
70-
public function create($path, $value, $scope, $scopeCode)
73+
public function create($path, $value, $scope, $scopeCode = null)
7174
{
72-
/** @var Structure $structure */
73-
$structure = $this->structureFactory->create();
74-
/** @var Structure\ElementInterface $field */
75-
$field = $this->deploymentConfig->isAvailable()
76-
? $structure->getElement($path)
77-
: null;
78-
/** @var ValueInterface $backendModel */
79-
$backendModel = $field instanceof Structure\Element\Field && $field->hasBackendModel()
80-
? $field->getBackendModel()
81-
: $this->valueFactory->create();
75+
try {
76+
/** @var Structure $structure */
77+
$structure = $this->structureFactory->create();
78+
/** @var Structure\ElementInterface $field */
79+
$field = $structure->getElement($path);
80+
/** @var ValueInterface $backendModel */
81+
$backendModel = $field instanceof Structure\Element\Field && $field->hasBackendModel()
82+
? $field->getBackendModel()
83+
: $this->valueFactory->create();
8284

83-
if ($backendModel instanceof Value) {
84-
$backendModel->setPath($path);
85-
$backendModel->setScope($scope);
86-
$backendModel->setScopeId($scopeCode);
87-
$backendModel->setValue($value);
88-
}
85+
if ($backendModel instanceof Value) {
86+
$scopeId = 0;
8987

90-
return $backendModel;
88+
if ($scope !== ScopeInterface::SCOPE_DEFAULT) {
89+
$scopeResolver = $this->scopeResolverPool->get($scope);
90+
$scopeId = $scopeResolver->getScope($scopeCode)->getId();
91+
}
92+
93+
$backendModel->setPath($path);
94+
$backendModel->setScope($scope);
95+
$backendModel->setScopeId($scopeId);
96+
$backendModel->setValue($value);
97+
}
98+
99+
return $backendModel;
100+
} catch (\Exception $exception) {
101+
throw new RuntimeException(__('%1', $exception->getMessage()), $exception);
102+
}
91103
}
92104
}

0 commit comments

Comments
 (0)