3
3
namespace Algolia \AlgoliaSearch \Model \Backend ;
4
4
5
5
use Algolia \AlgoliaSearch \Helper \Configuration \ConfigChecker ;
6
- use Algolia \AlgoliaSearch \Helper \Data ;
7
- use Algolia \AlgoliaSearch \Helper \Entity \ProductHelper ;
8
6
use Algolia \AlgoliaSearch \Registry \ReplicaState ;
9
7
use Magento \Config \Model \Config \Backend \Serialized \ArraySerialized ;
10
8
use Magento \Framework \App \Cache \TypeListInterface ;
16
14
use Magento \Framework \Model \ResourceModel \AbstractResource ;
17
15
use Magento \Framework \Registry ;
18
16
use Magento \Framework \Serialize \Serializer \Json ;
19
- use Magento \Store \Api \WebsiteRepositoryInterface ;
20
- use Magento \Store \Model \ScopeInterface ;
21
- use Magento \Store \Model \StoreManagerInterface ;
22
17
23
18
class Sorts extends ArraySerialized
24
19
{
25
20
public function __construct (
26
- Context $ context ,
27
- Registry $ registry ,
28
- ScopeConfigInterface $ config ,
29
- TypeListInterface $ cacheTypeList ,
30
- protected StoreManagerInterface $ storeManager ,
31
- protected Data $ helper ,
32
- protected ProductHelper $ productHelper ,
33
- protected WebsiteRepositoryInterface $ websiteRepository ,
34
- protected ReplicaState $ replicaState ,
35
- protected ConfigChecker $ configChecker ,
36
- AbstractResource $ resource = null ,
37
- AbstractDb $ resourceCollection = null ,
38
- array $ data = [],
39
- Json $ serializer = null
21
+ Context $ context ,
22
+ Registry $ registry ,
23
+ ScopeConfigInterface $ config ,
24
+ TypeListInterface $ cacheTypeList ,
25
+ protected ReplicaState $ replicaState ,
26
+ protected ConfigChecker $ configChecker ,
27
+ AbstractResource $ resource = null ,
28
+ AbstractDb $ resourceCollection = null ,
29
+ array $ data = [],
30
+ Json $ serializer = null
40
31
)
41
32
{
42
33
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
@@ -57,7 +48,12 @@ public function __construct(
57
48
*/
58
49
public function afterSave (): \Magento \Framework \App \Config \Value
59
50
{
60
- $ storeIds = $ this ->getAffectedStoreIds ();
51
+ $ storeIds = $ this ->configChecker ->getAffectedStoreIds (
52
+ $ this ->getPath (),
53
+ $ this ->getScope (),
54
+ $ this ->getScopeId ()
55
+ );
56
+
61
57
foreach ($ storeIds as $ storeId ) {
62
58
if ($ this ->isValueChanged ()) {
63
59
$ this ->replicaState ->setChangeState (ReplicaState::REPLICA_STATE_CHANGED , $ storeId );
@@ -70,73 +66,4 @@ public function afterSave(): \Magento\Framework\App\Config\Value
70
66
71
67
return parent ::afterSave ();
72
68
}
73
-
74
- /**
75
- * For the current operation's scope determine which stores need to be updated
76
- * @return int[]
77
- * @throws NoSuchEntityException
78
- */
79
- public function getAffectedStoreIds (): array
80
- {
81
- $ scopeId = $ this ->getScopeId ();
82
- $ scope = $ this ->getScope ();
83
- $ storeIds = [];
84
-
85
- switch ($ scope ) {
86
- // check and find all stores that are not overridden
87
- case ScopeConfigInterface::SCOPE_TYPE_DEFAULT :
88
- foreach ($ this ->storeManager ->getStores () as $ store ) {
89
- if (!$ this ->configChecker ->isSettingAppliedForScopeAndCode (
90
- $ this ->getPath (),
91
- ScopeInterface::SCOPE_STORES ,
92
- $ store ->getId ()
93
- )) {
94
- $ storeIds [] = $ store ->getId ();
95
- }
96
- }
97
- break ;
98
-
99
- // website config applied - check and find all stores under that website that are not overridden
100
- case ScopeInterface::SCOPE_WEBSITES :
101
- $ website = $ this ->websiteRepository ->getById ($ scopeId );
102
- foreach ($ website ->getStores () as $ store ) {
103
- if (!$ this ->configChecker ->isSettingAppliedForScopeAndCode (
104
- $ this ->getPath (),
105
- ScopeInterface::SCOPE_STORES ,
106
- $ store ->getId ()
107
- )) {
108
- $ storeIds [] = $ store ->getId ();
109
- }
110
- }
111
- break ;
112
-
113
- // simple store specific config
114
- case ScopeInterface::SCOPE_STORES :
115
- $ storeIds [] = $ scopeId ;
116
- }
117
- return $ storeIds ;
118
- }
119
-
120
- /**
121
- * For a given website perform an operation on each of the corresponding stores
122
- * based on whether the store has overridden the config.
123
- *
124
- * @param int $websiteId
125
- * @param callable $callback Callback to execute for a given store
126
- * Signature: function(int $storeId, bool $isConfigOverridden)
127
- * @return void
128
- * @throws NoSuchEntityException
129
- */
130
- protected function handleStoresForWebsite (int $ websiteId , callable $ callback ): void
131
- {
132
- $ website = $ this ->websiteRepository ->getById ($ websiteId );
133
- foreach ($ website ->getStores () as $ store ) {
134
- $ callback (
135
- $ store ->getId (),
136
- $ this ->configChecker ->isSettingAppliedForScopeAndCode (
137
- $ this ->getPath (),
138
- ScopeInterface::SCOPE_STORES , $ store ->getId ())
139
- );
140
- }
141
- }
142
69
}
0 commit comments