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
19
+ *
15
20
* Used to save configuration
16
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,11 +149,17 @@ 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
/**
124
161
* Save config section
162
+ *
125
163
* Require set: section, website, store and groups
126
164
*
127
165
* @throws \Exception
@@ -504,40 +542,75 @@ public function setDataByPath($path, $value)
504
542
}
505
543
506
544
/**
507
- * Get scope name and scopeId
508
- * @todo refactor to scope resolver
545
+ * Set scope data
546
+ *
509
547
* @return void
510
548
*/
511
549
private function initScope ()
512
550
{
513
551
if ($ this ->getSection () === null ) {
514
552
$ this ->setSection ('' );
515
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
+
516
560
if ($ this ->getWebsite () === null ) {
517
- $ this ->setWebsite ('' );
561
+ $ this ->setWebsite (StoreScopeInterface:: SCOPE_WEBSITES === $ this -> getScope () ? $ scope -> getId () : '' );
518
562
}
519
563
if ($ this ->getStore () === null ) {
520
- $ this ->setStore ('' );
564
+ $ this ->setStore (StoreScopeInterface:: SCOPE_STORES === $ this -> getScope () ? $ scope -> getId () : '' );
521
565
}
566
+ }
522
567
523
- if ($ this ->getStore ()) {
524
- $ scope = 'stores ' ;
525
- $ store = $ this ->_storeManager ->getStore ($ this ->getStore ());
526
- $ scopeId = (int )$ store ->getId ();
527
- $ scopeCode = $ store ->getCode ();
528
- } elseif ($ this ->getWebsite ()) {
529
- $ scope = 'websites ' ;
530
- $ website = $ this ->_storeManager ->getWebsite ($ this ->getWebsite ());
531
- $ scopeId = (int )$ website ->getId ();
532
- $ 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
+ }
533
591
} else {
534
- $ scope = 'default ' ;
535
- $ scopeId = 0 ;
536
- $ 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
+ }
537
609
}
538
- $ this ->setScope ($ scope );
539
- $ this ->setScopeId ($ scopeId );
540
- $ this ->setScopeCode ($ scopeCode );
610
+ $ scope = $ this ->scopeResolverPool ->get ($ scopeType )
611
+ ->getScope ($ scopeIdentifier );
612
+
613
+ return $ scope ;
541
614
}
542
615
543
616
/**
0 commit comments