99use Magento \Config \Model \Config \Structure \Element \Group ;
1010use Magento \Config \Model \Config \Structure \Element \Field ;
1111use Magento \Framework \App \ObjectManager ;
12+ use Magento \Framework \App \ScopeInterface ;
13+ use Magento \Framework \App \ScopeResolverPool ;
14+ use Magento \Store \Model \ScopeInterface as StoreScopeInterface ;
15+ use Magento \Store \Model \ScopeTypeNormalizer ;
1216
1317/**
1418 * Backend config model
1519 *
1620 * Used to save configuration
21+ *
1722 * @author Magento Core Team <[email protected] > 1823 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1924 * @api
2025 * @since 100.0.2
26+ * @method string getSection()
27+ * @method void setSection(string $section)
28+ * @method string getWebsite()
29+ * @method void setWebsite(string $website)
30+ * @method string getStore()
31+ * @method void setStore(string $store)
32+ * @method string getScope()
33+ * @method void setScope(string $scope)
34+ * @method int getScopeId()
35+ * @method void setScopeId(int $scopeId)
36+ * @method string getScopeCode()
37+ * @method void setScopeCode(string $scopeCode)
2138 */
2239class Config extends \Magento \Framework \DataObject
2340{
@@ -87,6 +104,16 @@ class Config extends \Magento\Framework\DataObject
87104 */
88105 private $ settingChecker ;
89106
107+ /**
108+ * @var ScopeResolverPool
109+ */
110+ private $ scopeResolverPool ;
111+
112+ /**
113+ * @var ScopeTypeNormalizer
114+ */
115+ private $ scopeTypeNormalizer ;
116+
90117 /**
91118 * @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
92119 * @param \Magento\Framework\Event\ManagerInterface $eventManager
@@ -97,6 +124,9 @@ class Config extends \Magento\Framework\DataObject
97124 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
98125 * @param Config\Reader\Source\Deployed\SettingChecker|null $settingChecker
99126 * @param array $data
127+ * @param ScopeResolverPool|null $scopeResolverPool
128+ * @param ScopeTypeNormalizer|null $scopeTypeNormalizer
129+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
100130 */
101131 public function __construct (
102132 \Magento \Framework \App \Config \ReinitableConfigInterface $ config ,
@@ -107,7 +137,9 @@ public function __construct(
107137 \Magento \Framework \App \Config \ValueFactory $ configValueFactory ,
108138 \Magento \Store \Model \StoreManagerInterface $ storeManager ,
109139 SettingChecker $ settingChecker = null ,
110- array $ data = []
140+ array $ data = [],
141+ ScopeResolverPool $ scopeResolverPool = null ,
142+ ScopeTypeNormalizer $ scopeTypeNormalizer = null
111143 ) {
112144 parent ::__construct ($ data );
113145 $ this ->_eventManager = $ eventManager ;
@@ -117,7 +149,12 @@ public function __construct(
117149 $ this ->_configLoader = $ configLoader ;
118150 $ this ->_configValueFactory = $ configValueFactory ;
119151 $ this ->_storeManager = $ storeManager ;
120- $ this ->settingChecker = $ settingChecker ?: ObjectManager::getInstance ()->get (SettingChecker::class);
152+ $ this ->settingChecker = $ settingChecker
153+ ?? ObjectManager::getInstance ()->get (SettingChecker::class);
154+ $ this ->scopeResolverPool = $ scopeResolverPool
155+ ?? ObjectManager::getInstance ()->get (ScopeResolverPool::class);
156+ $ this ->scopeTypeNormalizer = $ scopeTypeNormalizer
157+ ?? ObjectManager::getInstance ()->get (ScopeTypeNormalizer::class);
121158 }
122159
123160 /**
@@ -505,41 +542,75 @@ public function setDataByPath($path, $value)
505542 }
506543
507544 /**
508- * Get scope name and scopeId
545+ * Set scope data
509546 *
510- * @todo refactor to scope resolver
511547 * @return void
512548 */
513549 private function initScope ()
514550 {
515551 if ($ this ->getSection () === null ) {
516552 $ this ->setSection ('' );
517553 }
554+
555+ $ scope = $ this ->retrieveScope ();
556+ $ this ->setScope ($ this ->scopeTypeNormalizer ->normalize ($ scope ->getScopeType ()));
557+ $ this ->setScopeCode ($ scope ->getCode ());
558+ $ this ->setScopeId ($ scope ->getId ());
559+
518560 if ($ this ->getWebsite () === null ) {
519- $ this ->setWebsite ('' );
561+ $ this ->setWebsite (StoreScopeInterface:: SCOPE_WEBSITES === $ this -> getScope () ? $ scope -> getId () : '' );
520562 }
521563 if ($ this ->getStore () === null ) {
522- $ this ->setStore ('' );
564+ $ this ->setStore (StoreScopeInterface:: SCOPE_STORES === $ this -> getScope () ? $ scope -> getId () : '' );
523565 }
566+ }
524567
525- if ($ this ->getStore ()) {
526- $ scope = 'stores ' ;
527- $ store = $ this ->_storeManager ->getStore ($ this ->getStore ());
528- $ scopeId = (int )$ store ->getId ();
529- $ scopeCode = $ store ->getCode ();
530- } elseif ($ this ->getWebsite ()) {
531- $ scope = 'websites ' ;
532- $ website = $ this ->_storeManager ->getWebsite ($ this ->getWebsite ());
533- $ scopeId = (int )$ website ->getId ();
534- $ scopeCode = $ website ->getCode ();
568+ /**
569+ * Retrieve scope from initial data
570+ *
571+ * @return ScopeInterface
572+ */
573+ private function retrieveScope (): ScopeInterface
574+ {
575+ $ scopeType = $ this ->getScope ();
576+ if (!$ scopeType ) {
577+ switch (true ) {
578+ case $ this ->getStore ():
579+ $ scopeType = StoreScopeInterface::SCOPE_STORES ;
580+ $ scopeIdentifier = $ this ->getStore ();
581+ break ;
582+ case $ this ->getWebsite ():
583+ $ scopeType = StoreScopeInterface::SCOPE_WEBSITES ;
584+ $ scopeIdentifier = $ this ->getWebsite ();
585+ break ;
586+ default :
587+ $ scopeType = ScopeInterface::SCOPE_DEFAULT ;
588+ $ scopeIdentifier = null ;
589+ break ;
590+ }
535591 } else {
536- $ scope = 'default ' ;
537- $ scopeId = 0 ;
538- $ scopeCode = '' ;
592+ switch (true ) {
593+ case $ this ->getScopeId () !== null :
594+ $ scopeIdentifier = $ this ->getScopeId ();
595+ break ;
596+ case $ this ->getScopeCode () !== null :
597+ $ scopeIdentifier = $ this ->getScopeCode ();
598+ break ;
599+ case $ this ->getStore () !== null :
600+ $ scopeIdentifier = $ this ->getStore ();
601+ break ;
602+ case $ this ->getWebsite () !== null :
603+ $ scopeIdentifier = $ this ->getWebsite ();
604+ break ;
605+ default :
606+ $ scopeIdentifier = null ;
607+ break ;
608+ }
539609 }
540- $ this ->setScope ($ scope );
541- $ this ->setScopeId ($ scopeId );
542- $ this ->setScopeCode ($ scopeCode );
610+ $ scope = $ this ->scopeResolverPool ->get ($ scopeType )
611+ ->getScope ($ scopeIdentifier );
612+
613+ return $ scope ;
543614 }
544615
545616 /**
0 commit comments