Skip to content

Commit a529382

Browse files
committed
B2B-2258: Add caching capability to the storeConfig GraphQl query
1 parent 4da87b0 commit a529382

File tree

7 files changed

+163
-49
lines changed

7 files changed

+163
-49
lines changed

app/code/Magento/DirectoryGraphQl/Model/Cache/Tag/Strategy/CurrencyConfig.php renamed to app/code/Magento/DirectoryGraphQl/Model/Cache/Tag/Strategy/Config/CurrencyTagGenerator.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy;
8+
namespace Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\Config;
99

1010
use Magento\DirectoryGraphQl\Model\Resolver\Currency\Identity;
11-
use Magento\Framework\App\Cache\Tag\StrategyInterface;
1211
use Magento\Framework\App\Config\ValueInterface;
1312
use Magento\Store\Model\ScopeInterface;
1413
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\Store\Model\Config\Cache\Tag\Strategy\TagGeneratorInterface;
1515

1616
/**
17-
* Produce cache tags for currency config.
17+
* Generator that generates cache tags for currency configuration
1818
*/
19-
class CurrencyConfig implements StrategyInterface
19+
class CurrencyTagGenerator implements TagGeneratorInterface
2020
{
2121
/**
2222
* @var string[]
@@ -45,31 +45,23 @@ public function __construct(
4545
/**
4646
* @inheritdoc
4747
*/
48-
public function getTags($object): array
48+
public function generateTags(ValueInterface $config): array
4949
{
50-
if (!is_object($object)) {
51-
throw new \InvalidArgumentException('Provided argument is not an object');
52-
}
53-
54-
if ($object instanceof ValueInterface
55-
&& in_array($object->getPath(), $this->currencyConfigPaths)
56-
&& $object->isValueChanged()
57-
) {
58-
if ($object->getScope() == ScopeInterface::SCOPE_WEBSITES) {
59-
$website = $this->storeManager->getWebsite($object->getScopeId());
50+
if (in_array($config->getPath(), $this->currencyConfigPaths)) {
51+
if ($config->getScope() == ScopeInterface::SCOPE_WEBSITES) {
52+
$website = $this->storeManager->getWebsite($config->getScopeId());
6053
$storeIds = $website->getStoreIds();
61-
} elseif ($object->getScope() == ScopeInterface::SCOPE_STORES) {
62-
$storeIds = [$object->getScopeId()];
54+
} elseif ($config->getScope() == ScopeInterface::SCOPE_STORES) {
55+
$storeIds = [$config->getScopeId()];
6356
} else {
6457
$storeIds = array_keys($this->storeManager->getStores());
6558
}
66-
$ids = [];
59+
$tags = [];
6760
foreach ($storeIds as $storeId) {
68-
$ids[] = sprintf('%s_%s', Identity::CACHE_TAG, $storeId);
61+
$tags[] = sprintf('%s_%s', Identity::CACHE_TAG, $storeId);
6962
}
70-
return $ids;
63+
return $tags;
7164
}
72-
7365
return [];
7466
}
7567
}

app/code/Magento/DirectoryGraphQl/etc/di.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9-
<type name="Magento\Framework\App\Cache\Tag\Strategy\Factory">
9+
<type name="Magento\Store\Model\Config\Cache\Tag\Strategy\CompositeTagGenerator">
1010
<arguments>
11-
<argument name="customStrategies" xsi:type="array">
12-
<item name="Magento\Framework\App\Config\ValueInterface" xsi:type="object">
13-
Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\CurrencyConfig
11+
<argument name="tagGenerators" xsi:type="array">
12+
<item name="currency_tag_generator" xsi:type="object">
13+
Magento\DirectoryGraphQl\Model\Cache\Tag\Strategy\Config\CurrencyTagGenerator
1414
</item>
1515
</argument>
1616
</arguments>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Store\Model\Config\Cache\Tag\Strategy;
9+
10+
use Magento\Framework\App\Config\ValueInterface;
11+
12+
/**
13+
* Composite tag generator that generates cache tags for store configurations.
14+
*/
15+
class CompositeTagGenerator implements TagGeneratorInterface
16+
{
17+
/**
18+
* @var TagGeneratorInterface[]
19+
*/
20+
private $tagGenerators;
21+
22+
/**
23+
* @param TagGeneratorInterface[] $tagGenerators
24+
*/
25+
public function __construct(
26+
array $tagGenerators
27+
) {
28+
$this->tagGenerators = $tagGenerators;
29+
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function generateTags(ValueInterface $config): array
35+
{
36+
$tags = [];
37+
foreach ($this->tagGenerators as $tagGenerator) {
38+
$tags = array_merge($tags, $tagGenerator->generateTags($config));
39+
}
40+
return $tags;
41+
}
42+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Store\Model\Config\Cache\Tag\Strategy;
9+
10+
use Magento\Framework\App\Config\ValueInterface;
11+
12+
/**
13+
* Store configuration cache tag generator interface
14+
*/
15+
interface TagGeneratorInterface
16+
{
17+
/**
18+
* Generate cache tags with given store configuration
19+
*
20+
* @param ValueInterface $config
21+
* @return array
22+
*/
23+
public function generateTags(ValueInterface $config): array;
24+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\StoreGraphQl\Model\Cache\Tag\Strategy;
9+
10+
use Magento\Framework\App\Config\ValueInterface;
11+
use Magento\Store\Model\Config\Cache\Tag\Strategy\TagGeneratorInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\StoreGraphQl\Model\Resolver\Store\ConfigIdentity;
15+
16+
/**
17+
* Generator that generates cache tags for store configuration.
18+
*/
19+
class ConfigTagGenerator implements TagGeneratorInterface
20+
{
21+
/**
22+
* @var StoreManagerInterface
23+
*/
24+
private $storeManager;
25+
26+
/**
27+
* @param StoreManagerInterface $storeManager
28+
*/
29+
public function __construct(
30+
StoreManagerInterface $storeManager
31+
) {
32+
$this->storeManager = $storeManager;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function generateTags(ValueInterface $config): array
39+
{
40+
if ($config->getScope() == ScopeInterface::SCOPE_WEBSITES) {
41+
$website = $this->storeManager->getWebsite($config->getScopeId());
42+
$storeIds = $website->getStoreIds();
43+
} elseif ($config->getScope() == ScopeInterface::SCOPE_STORES) {
44+
$storeIds = [$config->getScopeId()];
45+
} else {
46+
$storeIds = array_keys($this->storeManager->getStores());
47+
}
48+
$tags = [];
49+
foreach ($storeIds as $storeId) {
50+
$tags[] = sprintf('%s_%s', ConfigIdentity::CACHE_TAG, $storeId);
51+
}
52+
return $tags;
53+
}
54+
}

app/code/Magento/StoreGraphQl/Model/Cache/Tag/Strategy/StoreConfig.php

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,24 @@
99

1010
use Magento\Framework\App\Cache\Tag\StrategyInterface;
1111
use Magento\Framework\App\Config\ValueInterface;
12-
use Magento\Store\Model\ScopeInterface;
13-
use Magento\Store\Model\StoreManagerInterface;
14-
use Magento\StoreGraphQl\Model\Resolver\Store\ConfigIdentity;
12+
use Magento\Store\Model\Config\Cache\Tag\Strategy\TagGeneratorInterface;
1513

1614
/**
1715
* Produce cache tags for store config.
1816
*/
1917
class StoreConfig implements StrategyInterface
2018
{
2119
/**
22-
* @var StoreManagerInterface
20+
* @var \Magento\Store\Model\Config\Cache\Tag\Strategy\TagGeneratorInterface
2321
*/
24-
private $storeManager;
22+
private $tagGenerator;
2523

26-
/**
27-
* @param StoreManagerInterface $storeManager
28-
*/
2924
public function __construct(
30-
StoreManagerInterface $storeManager
25+
TagGeneratorInterface $tagGenerator
3126
) {
32-
$this->storeManager = $storeManager;
27+
$this->tagGenerator = $tagGenerator;
3328
}
29+
3430
/**
3531
* @inheritdoc
3632
*/
@@ -41,19 +37,7 @@ public function getTags($object): array
4137
}
4238

4339
if ($object instanceof ValueInterface && $object->isValueChanged()) {
44-
if ($object->getScope() == ScopeInterface::SCOPE_WEBSITES) {
45-
$website = $this->storeManager->getWebsite($object->getScopeId());
46-
$storeIds = $website->getStoreIds();
47-
} elseif ($object->getScope() == ScopeInterface::SCOPE_STORES) {
48-
$storeIds = [$object->getScopeId()];
49-
} else {
50-
$storeIds = array_keys($this->storeManager->getStores());
51-
}
52-
$ids = [];
53-
foreach ($storeIds as $storeId) {
54-
$ids[] = sprintf('%s_%s', ConfigIdentity::CACHE_TAG, $storeId);
55-
}
56-
return $ids;
40+
return $this->tagGenerator->generateTags($object);
5741
}
5842

5943
return [];

app/code/Magento/StoreGraphQl/etc/di.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,25 @@
99
<type name="Magento\Framework\App\Cache\Tag\Strategy\Factory">
1010
<arguments>
1111
<argument name="customStrategies" xsi:type="array">
12-
<item name="Magento\Framework\App\Config\ValueInterface" xsi:type="object">Magento\StoreGraphQl\Model\Cache\Tag\Strategy\StoreConfig</item>
12+
<item name="Magento\Framework\App\Config\ValueInterface" xsi:type="object">
13+
Magento\StoreGraphQl\Model\Cache\Tag\Strategy\StoreConfig
14+
</item>
15+
</argument>
16+
</arguments>
17+
</type>
18+
<type name="Magento\StoreGraphQl\Model\Cache\Tag\Strategy\StoreConfig">
19+
<arguments>
20+
<argument name="tagGenerator" xsi:type="object">
21+
Magento\Store\Model\Config\Cache\Tag\Strategy\CompositeTagGenerator
22+
</argument>
23+
</arguments>
24+
</type>
25+
<type name="Magento\Store\Model\Config\Cache\Tag\Strategy\CompositeTagGenerator">
26+
<arguments>
27+
<argument name="tagGenerators" xsi:type="array">
28+
<item name="store_config_tag_generator" xsi:type="object">
29+
Magento\StoreGraphQl\Model\Cache\Tag\Strategy\ConfigTagGenerator
30+
</item>
1331
</argument>
1432
</arguments>
1533
</type>

0 commit comments

Comments
 (0)