Skip to content

Commit c5a3676

Browse files
committed
MAGE-839 Move affected store ID logic to ConfigChecker
1 parent 9a64c54 commit c5a3676

File tree

2 files changed

+71
-92
lines changed

2 files changed

+71
-92
lines changed

Helper/Configuration/ConfigChecker.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
namespace Algolia\AlgoliaSearch\Helper\Configuration;
44

55
use Magento\Framework\App\Config\ScopeConfigInterface;
6+
use Magento\Framework\Exception\NoSuchEntityException;
7+
use Magento\Store\Api\WebsiteRepositoryInterface;
68
use Magento\Store\Model\ScopeInterface;
79
use Magento\Store\Model\StoreManagerInterface;
810

911
class ConfigChecker
1012
{
1113
public function __construct(
12-
protected ScopeConfigInterface $scopeConfig,
13-
protected StoreManagerInterface $storeManager
14+
protected ScopeConfigInterface $scopeConfig,
15+
protected StoreManagerInterface $storeManager,
16+
protected WebsiteRepositoryInterface $websiteRepository
1417
) {}
1518

1619
public function isSettingAppliedForScopeAndCode(string $path, string $scope, string $code): bool
@@ -31,7 +34,8 @@ public function isSettingAppliedForScopeAndCode(string $path, string $scope, str
3134
*
3235
* @return void
3336
*/
34-
public function checkAndApplyAllScopes(string $path, callable $callback, bool $includeDefault = true) {
37+
public function checkAndApplyAllScopes(string $path, callable $callback, bool $includeDefault = true): void
38+
{
3539
// First update all the possible scoped configurations
3640
foreach ($this->storeManager->getWebsites() as $website) {
3741
if ($this->isSettingAppliedForScopeAndCode(
@@ -58,4 +62,52 @@ public function checkAndApplyAllScopes(string $path, callable $callback, bool $i
5862
$callback(ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
5963
}
6064
}
65+
66+
67+
/**
68+
* For a given path and scope determine which stores are affected
69+
* @param string $path The configuration path
70+
* @param string $scope The scope: `default`, `websites` or `stores`
71+
* @param int $scopeId The entity referenced by the corresponding scope
72+
* @return int[]
73+
* @throws NoSuchEntityException
74+
*/
75+
public function getAffectedStoreIds(string $path, string $scope, int $scopeId): array
76+
{
77+
$storeIds = [];
78+
79+
switch ($scope) {
80+
// check and find all stores that are not overridden
81+
case ScopeConfigInterface::SCOPE_TYPE_DEFAULT:
82+
foreach ($this->storeManager->getStores() as $store) {
83+
if (!$this->isSettingAppliedForScopeAndCode(
84+
$path,
85+
ScopeInterface::SCOPE_STORES,
86+
$store->getId()
87+
)) {
88+
$storeIds[] = $store->getId();
89+
}
90+
}
91+
break;
92+
93+
// website config applied - check and find all stores under that website that are not overridden
94+
case ScopeInterface::SCOPE_WEBSITES:
95+
$website = $this->websiteRepository->getById($scopeId);
96+
foreach ($website->getStores() as $store) {
97+
if (!$this->isSettingAppliedForScopeAndCode(
98+
$path,
99+
ScopeInterface::SCOPE_STORES,
100+
$store->getId()
101+
)) {
102+
$storeIds[] = $store->getId();
103+
}
104+
}
105+
break;
106+
107+
// simple store specific config
108+
case ScopeInterface::SCOPE_STORES:
109+
$storeIds[] = $scopeId;
110+
}
111+
return $storeIds;
112+
}
61113
}

Model/Backend/Sorts.php

Lines changed: 16 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace Algolia\AlgoliaSearch\Model\Backend;
44

55
use Algolia\AlgoliaSearch\Helper\Configuration\ConfigChecker;
6-
use Algolia\AlgoliaSearch\Helper\Data;
7-
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
86
use Algolia\AlgoliaSearch\Registry\ReplicaState;
97
use Magento\Config\Model\Config\Backend\Serialized\ArraySerialized;
108
use Magento\Framework\App\Cache\TypeListInterface;
@@ -16,27 +14,20 @@
1614
use Magento\Framework\Model\ResourceModel\AbstractResource;
1715
use Magento\Framework\Registry;
1816
use Magento\Framework\Serialize\Serializer\Json;
19-
use Magento\Store\Api\WebsiteRepositoryInterface;
20-
use Magento\Store\Model\ScopeInterface;
21-
use Magento\Store\Model\StoreManagerInterface;
2217

2318
class Sorts extends ArraySerialized
2419
{
2520
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
4031
)
4132
{
4233
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
@@ -57,7 +48,12 @@ public function __construct(
5748
*/
5849
public function afterSave(): \Magento\Framework\App\Config\Value
5950
{
60-
$storeIds = $this->getAffectedStoreIds();
51+
$storeIds = $this->configChecker->getAffectedStoreIds(
52+
$this->getPath(),
53+
$this->getScope(),
54+
$this->getScopeId()
55+
);
56+
6157
foreach ($storeIds as $storeId) {
6258
if ($this->isValueChanged()) {
6359
$this->replicaState->setChangeState(ReplicaState::REPLICA_STATE_CHANGED, $storeId);
@@ -70,73 +66,4 @@ public function afterSave(): \Magento\Framework\App\Config\Value
7066

7167
return parent::afterSave();
7268
}
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-
}
14269
}

0 commit comments

Comments
 (0)