Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PreparedValueFactory.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Config\Model;
7 
10 use Magento\Config\Model\Config\StructureFactory;
18 
27 {
33  private $scopeResolverPool;
34 
40  private $structureFactory;
41 
48  private $valueFactory;
49 
55  private $config;
56 
62  private $scopeTypeNormalizer;
63 
71  public function __construct(
72  ScopeResolverPool $scopeResolverPool,
73  StructureFactory $structureFactory,
74  BackendFactory $valueFactory,
75  ScopeConfigInterface $config,
76  ScopeTypeNormalizer $scopeTypeNormalizer
77  ) {
78  $this->scopeResolverPool = $scopeResolverPool;
79  $this->structureFactory = $structureFactory;
80  $this->valueFactory = $valueFactory;
81  $this->config = $config;
82  $this->scopeTypeNormalizer = $scopeTypeNormalizer;
83  }
84 
97  public function create($path, $value, $scope, $scopeCode = null)
98  {
99  try {
101  $structure = $this->structureFactory->create();
103  $field = $structure->getElementByConfigPath($path);
104  $configPath = $path;
106  if ($field instanceof Structure\Element\Field && $field->hasBackendModel()) {
107  $backendModelName = $field->getData()['backend_model'];
108  $configPath = $field->getConfigPath() ?: $path;
109  } else {
110  $backendModelName = ValueInterface::class;
111  }
113  $backendModel = $this->valueFactory->create(
114  $backendModelName,
115  ['config' => $this->config]
116  );
117 
118  if ($backendModel instanceof Value) {
119  $scopeId = 0;
120 
121  $scope = $this->scopeTypeNormalizer->normalize($scope);
122 
123  if ($scope !== ScopeInterface::SCOPE_DEFAULT) {
124  $scopeId = $this->scopeResolverPool->get($scope)
125  ->getScope($scopeCode)
126  ->getId();
127  }
128 
129  if ($field instanceof Structure\Element\Field) {
130  $groupPath = $field->getGroupPath();
131  $group = $structure->getElement($groupPath);
132  $backendModel->setField($field->getId());
133  $backendModel->setGroupId($group->getId());
134  $backendModel->setFieldConfig($field->getData());
135  }
136 
137  $backendModel->setPath($configPath);
138  $backendModel->setScope($scope);
139  $backendModel->setScopeId($scopeId);
140  $backendModel->setScopeCode($scopeCode);
141  $backendModel->setValue($value);
142  }
143 
144  return $backendModel;
145  } catch (\Exception $exception) {
146  throw new RuntimeException(__('%1', $exception->getMessage()), $exception);
147  }
148  }
149 }
$config
Definition: fraud_order.php:17
$group
Definition: sections.phtml:16
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
__construct(ScopeResolverPool $scopeResolverPool, StructureFactory $structureFactory, BackendFactory $valueFactory, ScopeConfigInterface $config, ScopeTypeNormalizer $scopeTypeNormalizer)