9
9
use Magento \Config \Model \Config \Structure \Element \Group ;
10
10
use Magento \Config \Model \Config \Structure \Element \Field ;
11
11
use 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 ;
12
16
13
17
/**
14
18
* Backend config model
15
19
*
16
20
* Used to save configuration
21
+ *
17
22
* @author Magento Core Team <core@magentocommerce.com>
18
23
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
24
* @api
20
25
* @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)
21
38
*/
22
39
class Config extends \Magento \Framework \DataObject
23
40
{
@@ -87,6 +104,16 @@ class Config extends \Magento\Framework\DataObject
87
104
*/
88
105
private $ settingChecker ;
89
106
107
+ /**
108
+ * @var ScopeResolverPool
109
+ */
110
+ private $ scopeResolverPool ;
111
+
112
+ /**
113
+ * @var ScopeTypeNormalizer
114
+ */
115
+ private $ scopeTypeNormalizer ;
116
+
90
117
/**
91
118
* @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
92
119
* @param \Magento\Framework\Event\ManagerInterface $eventManager
@@ -97,6 +124,9 @@ class Config extends \Magento\Framework\DataObject
97
124
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
98
125
* @param Config\Reader\Source\Deployed\SettingChecker|null $settingChecker
99
126
* @param array $data
127
+ * @param ScopeResolverPool|null $scopeResolverPool
128
+ * @param ScopeTypeNormalizer|null $scopeTypeNormalizer
129
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
100
130
*/
101
131
public function __construct (
102
132
\Magento \Framework \App \Config \ReinitableConfigInterface $ config ,
@@ -107,7 +137,9 @@ public function __construct(
107
137
\Magento \Framework \App \Config \ValueFactory $ configValueFactory ,
108
138
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
109
139
SettingChecker $ settingChecker = null ,
110
- array $ data = []
140
+ array $ data = [],
141
+ ScopeResolverPool $ scopeResolverPool = null ,
142
+ ScopeTypeNormalizer $ scopeTypeNormalizer = null
111
143
) {
112
144
parent ::__construct ($ data );
113
145
$ this ->_eventManager = $ eventManager ;
@@ -117,7 +149,12 @@ public function __construct(
117
149
$ this ->_configLoader = $ configLoader ;
118
150
$ this ->_configValueFactory = $ configValueFactory ;
119
151
$ 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);
121
158
}
122
159
123
160
/**
@@ -505,41 +542,75 @@ public function setDataByPath($path, $value)
505
542
}
506
543
507
544
/**
508
- * Get scope name and scopeId
545
+ * Set scope data
509
546
*
510
- * @todo refactor to scope resolver
511
547
* @return void
512
548
*/
513
549
private function initScope ()
514
550
{
515
551
if ($ this ->getSection () === null ) {
516
552
$ this ->setSection ('' );
517
553
}
554
+
555
+ $ scope = $ this ->retrieveScope ();
556
+ $ this ->setScope ($ this ->scopeTypeNormalizer ->normalize ($ scope ->getScopeType ()));
557
+ $ this ->setScopeCode ($ scope ->getCode ());
558
+ $ this ->setScopeId ($ scope ->getId ());
559
+
518
560
if ($ this ->getWebsite () === null ) {
519
- $ this ->setWebsite ('' );
561
+ $ this ->setWebsite (StoreScopeInterface:: SCOPE_WEBSITES === $ this -> getScope () ? $ scope -> getId () : '' );
520
562
}
521
563
if ($ this ->getStore () === null ) {
522
- $ this ->setStore ('' );
564
+ $ this ->setStore (StoreScopeInterface:: SCOPE_STORES === $ this -> getScope () ? $ scope -> getId () : '' );
523
565
}
566
+ }
524
567
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
+ }
535
591
} 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
+ }
539
609
}
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 ;
543
614
}
544
615
545
616
/**
0 commit comments